📄 conditional_complex_edit.js
字号:
/* Model Functions * * Perform various and sundry operations on the edit-page. */var target_url = '/plugins/ktcore/admin/ajaxComplexConditionals.php';function getTargetUrl() { var base = getElement('kt-core-baseurl'); return base.value + target_url;}// returns the td element representing the row, for use as Parent.function getColumnForField(field_id) { return getElement('md_'+field_id);}// returns a 2d array ("instances" => instance_id, "names" => names)function getActiveLookupsForField(field_id, clear) { var names = Array(); var instances = Array(); var column = getColumnForField(field_id); var sf_source = getElementsByTagAndClassName('select','item_list',column); if (sf_source.length == 0) { simpleLog('ERROR','no item_list found in field '+field_id); return false; } var selectfield = sf_source[0]; for (var i=0; i<selectfield.options.length; i++) { var opt = selectfield.options[i]; if (opt.selected) { names.push(scrapeText(opt)); instances.push(opt.value); if (clear == true) { opt.selected = false; } } } return {"instances":instances, "names":names};}function setMessageForField(field_id, message_type, message_str) { var column = getColumnForField(field_id); var message_opts = getElementsByTagAndClassName('p',message_type,column); if (message_opts.length == 0) { simpleLog('ERROR','no message with identifier '+message_type+' found in field '+field_id); return false; } var message_obj = message_opts[0]; replaceChildNodes(message_obj, message_str);}/* undo stack management. */var fixed_field_stack = Array();var fixed_value = null;// GAH - stupid ie.function addEvent(obj, event, func) { if (obj.attachEvent) { obj.attachEvent('on'+event, func); } else { obj.addEventListener(event, func, false); }}function removeEvent(obj, event, func) { if (obj.detachEvent) { obj.detachEvent('on'+event, func); } else { obj.removeEventListener(event, func, false); }}// called after push or pop, makes sure that there is a "done" button on the right column.function placeFinishButton() { // remove the current button no matter what. var current_button = getElement('global-stack-popper-button'); if (current_button != null) { current_button.parentNode.removeChild(current_button); simpleLog('DEBUG','removing stack-popper.'); } // find the current "fixed" field. var active_field = null; if (fixed_field_stack.length == 0) { return false; // nothing "free" - remove it. } else { active_field = fixed_field_stack[fixed_field_stack.length - 1]; } var column = getColumnForField(active_field); var finish_button = INPUT({'type':'button', 'value':_('Finish with this column\'s behaviours.'),'id':'global-stack-popper-button'}); simpleLog('DEBUG','placing stack-popper with function popFixedStack in field '+active_field); addEvent(finish_button, 'click', popFixedStack); simpleLog('DEBUG','added listener.'); column.appendChild(finish_button);}function pushFixedStack(field_id, value) { var current_last_field = null; if (fixed_field_stack.length != 0) { current_last_field = fixed_field_stack[fixed_field_stack.length-1]; } if ((current_last_field == null) || (current_last_field != field_id)) { simpleLog('DEBUG','pushing onto stack. '+field_id) fixed_field_stack.push(field_id); } fixed_value = value; placeFinishButton(); updateActiveFields();}// pop the fixed stack, and set fixed_value to the appropriate one.function popFixedStack() { var fs_length = fixed_field_stack.length; if (fs_length == 0) { // nothing, ensure that we are clean. fixed_value = null; return false; } else if (fs_length == 1) { // empty the fixed_value, and pop. fixed_value = null; var current_field = fixed_field_stack.pop(); } else { var current_field = fixed_field_stack.pop(); var last_item = fixed_field_stack[fs_length - 2]; // we've popped one subsequently. fixed_value = getFixedValueForField(last_item); } // make sure the old field gets the appropriate classes. removeElementClass(getColumnForField(current_field), 'fixed'); clearFixedValueForField(current_field); placeFinishButton(); updateActiveFields();}function getFixedValueForField(field_id) { var column = getColumnForField(field_id); var fixed_value = null; var fixed_source = getElementsByTagAndClassName('input','fixed_value',column); if (fixed_source.length != 0) { for (var i=0; i<fixed_source.length; i++) { fixed_value = fixed_source[i].value; } } simpleLog('DEBUG','got fixed value for field '+field_id+': '+fixed_value); return fixed_value;}// also needs to pop this field's value from the stack.function clearFixedValueForField(field_id) { var column = getColumnForField(field_id); simpleLog('DEBUG','clearing fixed value for field '+field_id); var fixed_source = getElementsByTagAndClassName('input','fixed_value',column); if (fixed_source.length != 0) { for (var i=0; i<fixed_source.length; i++) { fixed_source[i].parentNode.removeChild(fixed_source[i]); } } removeElementClass(column, 'fixed');}// setup a field to be "fixed" - that is, ensure that the correct hidden inputs are in place.function setFixedValueForField(field_id, value, label) { var column = getColumnForField(field_id); simpleLog('DEBUG','fixing value for field '+field_id+' to '+label+' ('+value+')'); addElementClass(column, 'fixed'); setMessageForField(field_id,'fixed_message', _('Assuming this field has behaviour "')+label+'".'); // now we need to check if this field _is_ fixed. var fixed_source = getElementsByTagAndClassName('input','fixed_value',column); if (fixed_source.length != 0) { for (var i=0; i<fixed_source.length; i++) { fixed_source[i].parentNode.removeChild(fixed_source[i]); } } appendChildNodes(column, INPUT({'class':'fixed_value', 'value':value, 'name':'fixed_value','type':'hidden'})); pushFixedStack(field_id, value); simpleLog('DEBUG','got to end of setFixed.');}// psuedo static.function getFieldsetId() { return getElement('global-fieldset-id').value;}/* * XMLHttpRequest Workers * */function handleError(err) { simpleLog('ERROR','failed on xmlhttpreq. exception: \n'+repr(err)); }// quick helper to send a POST to a url.function getPOSTRequest(fullurl) { var req = getXMLHttpRequest(); req.open('POST',fullurl,true); req.setRequestHeader('Content-Type','application/x-www-form-urlencoded') return req;}function removeFromBehaviour(field_id) { var action = 'removeFromBehaviour'; simpleLog('DEBUG','initiating item list update on field '+field_id); var formKeys = Array(); var formValues = Array(); // action formKeys.push('action'); formValues.push(action); // fieldset-id formKeys.push('fieldset_id'); formValues.push(getFieldsetId()); // field_id formKeys.push('field_id'); formValues.push(field_id); // get the assigned items that are selected. var column = getColumnForField(field_id); var assigned_sources = getElementsByTagAndClassName('SELECT','assigned_item_list', column); if (assigned_sources.length == 0) { simpleLog('ERROR','Unable to locate assigned items.'); return; } var assigned_select = assigned_sources[0]; for (var i=0; i<assigned_select.options.length; i++) { var opt = assigned_select.options[i]; if (opt.selected) { formKeys.push('fieldsToRemove[]'); formValues.push(opt.value); } } // boilerplate. var POSTval = queryString(formKeys, formValues); var req = getPOSTRequest(getTargetUrl()); simpleLog('DEBUG','sending request (to '+getTargetUrl()+'): \n'+repr(map(null, formKeys, formValues))+'\nqueryString: '+POSTval); var deferred = sendXMLHttpRequest(req, POSTval); deferred.addCallback(partial(do_removeItems, field_id)); deferred.addErrback(handleError);}function do_removeItems(field_id, req) { updateItemListForField(field_id);}// updates the item list for a given field to the items which are "free".function updateItemListForField(field_id) { var action = 'getItemList'; simpleLog('DEBUG','initiating item list update on field '+field_id); var formKeys = Array(); var formValues = Array(); // action formKeys.push('action'); formValues.push(action); // we need the fixed parent (or null - both are equivalent). formKeys.push('parent_behaviour'); if (fixed_value == null) { formValues.push(''); } else { formValues.push(fixed_value); } // fieldset-id formKeys.push('fieldset_id'); formValues.push(getFieldsetId()); // field_id formKeys.push('field_id'); formValues.push(field_id); // boilerplate. var POSTval = queryString(formKeys, formValues); var req = getPOSTRequest(getTargetUrl()); simpleLog('DEBUG','sending request (to '+getTargetUrl()+'): \n'+repr(map(null, formKeys, formValues))+'\nqueryString: '+POSTval); var deferred = sendXMLHttpRequest(req, POSTval); deferred.addCallback(partial(do_updateItemList, field_id)); deferred.addErrback(handleError); action = 'getAssignedList'; formKeys = Array(); formValues = Array(); formKeys.push('action'); formValues.push(action); // we need the fixed parent (or null - both are equivalent). formKeys.push('parent_behaviour'); if (fixed_value == null) { formValues.push(''); } else { formValues.push(fixed_value); } // fieldset-id formKeys.push('fieldset_id'); formValues.push(getFieldsetId()); // field_id formKeys.push('field_id'); formValues.push(field_id); // boilerplate. POSTval = queryString(formKeys, formValues); req2 = getPOSTRequest(getTargetUrl()); simpleLog('DEBUG','sending request (to '+getTargetUrl()+'): \n'+repr(map(null, formKeys, formValues))+'\nqueryString: '+POSTval); var deferred2 = sendXMLHttpRequest(req2, POSTval); deferred2.addCallback(partial(do_updateAssignedItemList, field_id)); deferred2.addErrback(handleError); }// updates the available behaviours for a given field.function updateBehaviourListsForField(field_id) { var action = 'getBehaviourList'; simpleLog('DEBUG','initiating behaviour list update on field '+field_id); var formKeys = Array(); var formValues = Array(); // action formKeys.push('action'); formValues.push(action); // we need the fixed parent (or null - both are equivalent). formKeys.push('parent_behaviour'); if (fixed_value == null) { formValues.push(''); } else { formValues.push(fixed_value); } // fieldset-id formKeys.push('fieldset_id'); formValues.push(getFieldsetId()); // field_id formKeys.push('field_id'); formValues.push(field_id); // boilerplate. var POSTval = queryString(formKeys, formValues); var req = getPOSTRequest(getTargetUrl()); simpleLog('DEBUG','sending request (to '+getTargetUrl()+'): \n'+repr(map(null, formKeys, formValues))+'\nqueryString: '+POSTval); var deferred = sendXMLHttpRequest(req, POSTval); deferred.addCallback(partial(do_updateBehaviours, field_id)); deferred.addErrback(handleError);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -