📄 conditional_complex_edit.js
字号:
// updates the set of "active" columns.// sends:// fixed field.// gets:// active fields // (everything else is inactive or fixed.)function updateActiveFields() { simpleLog('DEBUG','initiating active field update.'); var action = 'getActiveFields'; 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()); var POSTval = queryString(formKeys, formValues); var req = getPOSTRequest(getTargetUrl()); simpleLog('DEBUG','sending request (to '+getTargetUrl()+' with action '+action+'): \n'+repr(map(null, formKeys, formValues))+'\nqueryString: '+POSTval); var deferred = sendXMLHttpRequest(req, POSTval); deferred.addCallback(do_updateActiveFields); deferred.addErrback(handleError);}// creates a new behaviour, and adds the appropriate metadata fields to it. function createBehaviourAndAssign(field_id, values, behaviour_name) { var action = 'createBehaviourAndAssign'; simpleLog('DEBUG','initiating behaviour creation 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); // behaviour-name formKeys.push('behaviour_name'); formValues.push(behaviour_name); // all the values. for (var i=0; i<values.length; i++) { formKeys.push('lookups_to_assign[]'); formValues.push(values[i]); } var POSTval = queryString(formKeys, formValues); var req = getPOSTRequest(getTargetUrl()); simpleLog('DEBUG','sending request: \n'+repr(map(null, formKeys, formValues))+'\nqueryString: '+POSTval); var deferred = sendXMLHttpRequest(req, POSTval); deferred.addCallback(partial(do_createBehaviour, field_id)); deferred.addErrback(handleError); // FIXME add an errback.}// variant of createBehaviourAndAssign that uses an existing behaviour.function useBehaviourAndAssign(field_id, values, behaviour_id) { var action='useBehaviourAndAssign'; simpleLog('DEBUG','initiating behaviour creation 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); // behaviour-id formKeys.push('behaviour_id'); formValues.push(behaviour_id); // all the values. for (var i=0; i<values.length; i++) { formKeys.push('lookups_to_assign[]'); formValues.push(values[i]); } var POSTval = queryString(formKeys, formValues); var req = getPOSTRequest(getTargetUrl()); simpleLog('DEBUG','sending request: \n'+repr(map(null, formKeys, formValues))+'\nqueryString: '+POSTval); var deferred = sendXMLHttpRequest(req, POSTval); deferred.addCallback(partial(do_createBehaviour, field_id)); deferred.addErrback(handleError);}/* * XMLHttpRequest Callbacks. * */function do_updateActiveFields (req) { simpleLog('DEBUG','callback for active field update received: \n'+req.responseText); // to handle this, you need to understand the model we're using to a degree. // at this point, fields have 3 states (modelled internally as classes, for various reasons): // "active fixed" - these are the items which are currently controlling what is availble. // "active" - NOT fixed. potentially, some of these may need to be clobbered. // "inactive" - could be made "active" by this change. // so we grab all the fields, and then separate out those we are considering // from those we aren't. we then toggle them on / off as required. xmldoc = req.responseXML; var inactive_fields = getElementsByTagAndClassName('td','inactive'); var active_fields = getElementsByTagAndClassName('td','active'); // create potential fields: all inactive, and non-fixed active fields are on the block. var potential_fields = Array(); for (var i=0; i<inactive_fields.length; i++) { potential_fields[inactive_fields[i].id] = inactive_fields[i]; } for (var i=0; i<active_fields.length; i++) { if (!hasElementClass(active_fields[i], 'fixed')) { potential_fields[active_fields[i].id] = active_fields[i]; } else { simpleLog('DEBUG','discarded active fields '+active_fields[i]+' since its fixed.'); } } simpleLog('DEBUG','identified potential fields as: '+potential_fields); // we use the fact that potential_fields["md_2"] is a reference, so delete is garbage collected (e.g. its still in the DOM). // so we delete items that match, and when we're done, we set everything else to "inactive". var response_active_list = xmldoc.getElementsByTagName('field'); for (var i=0; i<response_active_list.length; i++) { var field_id = response_active_list[i].getAttribute('value'); var td_id = "md_"+field_id; if (potential_fields[td_id]) { setElementClass(potential_fields[td_id], 'active'); updateBehaviourListsForField(field_id); updateItemListForField(field_id); delete potential_fields[td_id]; simpleLog('DEBUG','activating '+td_id); } else { simpleLog('ERROR','no field matching id specified in XML: '+td_id); } } for (key in potential_fields) { setElementClass(potential_fields[key], 'inactive'); }}function do_updateItemList(field_id, req) { simpleLog('DEBUG','entering callback for item-list update.'); var items = req.responseXML.getElementsByTagName('item'); var itemNodes = Array(); for (var i=0; i<items.length; i++) { var item = items[i]; itemNodes.push(createDOM('option',{'value':item.getAttribute('value')},item.getAttribute('label'))); } // now, find the array and replaceChildNodes() it. var column = getColumnForField(field_id); var is_sources = getElementsByTagAndClassName('select','item_list',column); if (is_sources.length == 0) { simpleLog('ERROR','Could not find the item list in field '+field_id); return; } var item_select = is_sources[0]; replaceChildNodes(item_select, itemNodes);}function do_updateAssignedItemList(field_id, req) { simpleLog('DEBUG','entering callback for assigned-item-list update.'); var items = req.responseXML.getElementsByTagName('item'); var itemNodes = Array(); for (var i=0; i<items.length; i++) { var item = items[i]; itemNodes.push(createDOM('option',{'value':item.getAttribute('value')},item.getAttribute('label'))); } // now, find the array and replaceChildNodes() it. var column = getColumnForField(field_id); var assigned_wrapper = getElementsByTagAndClassName('DIV','assigned_items',column)[0]; if (itemNodes.length == 0) { setElementClass(assigned_wrapper, 'assigned_items empty'); } else { setElementClass(assigned_wrapper, 'assigned_items'); } var is_sources = getElementsByTagAndClassName('select','assigned_item_list',column); if (is_sources.length == 0) { simpleLog('ERROR','Could not find the assigned item list in field '+field_id); return; } var item_select = is_sources[0]; replaceChildNodes(item_select, itemNodes);}function do_updateBehaviours(field_id, req) { simpleLog('DEBUG','entering callback for behaviour-lists update.'); // we handle this slightly differently to updateItemList. // particularly, we have a number of different items that // are behaviour lists in a given dropdown. var behaviours = req.responseXML.getElementsByTagName('behaviour'); var behaviourVals = Array(); for (var i=0; i<behaviours.length; i++) { var behaviour = behaviours[i]; behaviourVals.push({'value':behaviour.getAttribute('value'), 'label':behaviour.getAttribute('label')}); } // now, find the selects(!) and replaceChildNodes() them. var column = getColumnForField(field_id); var ab_sources = getElementsByTagAndClassName('select','available_behaviours',column); var eb_sources = getElementsByTagAndClassName('select','edit_behaviours',column); if ((ab_sources.length == 0) || (eb_sources.length == 0)) { simpleLog('ERROR','Could not find all behaviourlists in field '+field_id); return; } var available_select = ab_sources[0]; var edit_select = eb_sources[0]; // we can't use the same set of DOM nodes, since DOM is a tree. var behaviourOptions = Array(); behaviourOptions.push(createDOM('option',null,'Select a behaviour')); for (var i=0; i<behaviourVals.length; i++) { var entry = behaviourVals[i]; behaviourOptions.push(createDOM('option',{'value':entry.value}, entry.label)); } replaceChildNodes(available_select, behaviourOptions); // we can't use the same set of DOM nodes, since DOM is a tree. var behaviourOptions = Array(); behaviourOptions.push(createDOM('option',null,'Select a behaviour')); for (var i=0; i<behaviourVals.length; i++) { var entry = behaviourVals[i]; behaviourOptions.push(createDOM('option',{'value':entry.value}, entry.label)); } replaceChildNodes(edit_select, behaviourOptions);}// trigger a itemlist and behaviourlist update.function do_createBehaviour(field_id, req) { simpleLog('DEBUG','callback for create behaviour (field: '+field_id+') received: \n'+req.responseText); updateItemListForField(field_id); updateBehaviourListsForField(field_id);}// doesn't act on local state directly, since that could clobber any number of things.// rather, purge and go.function do_assignBehaviour(field_id, req) { simpleLog('DEBUG','callback for assign behaviour (field: '+field_id+') received: \n'+req.responseText); updateItemListForField(field_id);}/* * HTML Interface functions. * * contain just-enough-logic, and interfaces with the DOM (hopefully) only through model-functions (see above) */function changeAssignments(field_id) { simpleLog('DEBUG','Change assignments called for field '+field_id);}function assignToBehaviour(select, field_id) { simpleLog('DEBUG','assignToBehaviour called for field '+field_id+'.'); behaviourId=select.value; behaviourName=scrapeText(select.options[select.selectedIndex]); // clear this to allow another lot to be assigned here. select.options[select.selectedIndex].selected = false; var activeValues = getActiveLookupsForField(field_id, true); if (activeValues.instances.length == 0) { simpleLog('DEBUG','no values selected, passing through without acting.'); return ; } var log_message = 'assigning to '+behaviourName+' ('+behaviourId+').\nnames: ' + activeValues["names"].join(',')+'\nvalues: '+activeValues["instances"].join(','); simpleLog('DEBUG',log_message); useBehaviourAndAssign(field_id, activeValues.instances, behaviourId);}function assignToNewBehaviour(field_id) { simpleLog('DEBUG','assignToNewBehavriour called for field '+field_id+'.'); // get the var column = getColumnForField(field_id); var tf_source = getElementsByTagAndClassName('input','new_behaviour',column) if (tf_source.length == 0) { simpleLog('ERROR','no new_behaviour fields found!'); return ; } else { var textfield = tf_source[0]; } var behaviourName = textfield.value; // clear this to prevent confusion. textfield.value = ''; var activeValues = getActiveLookupsForField(field_id, true); if (activeValues.instances.length == 0) { simpleLog('DEBUG','no values selected, passing through without acting.'); return ; } var log_message = 'assigning to '+behaviourName+'.\nnames: ' + activeValues["names"].join(',')+'\nvalues: '+activeValues["instances"].join(','); simpleLog('DEBUG',log_message); createBehaviourAndAssign(field_id, activeValues.instances, behaviourName);}function editBehaviour(selectfield, field_id) { var optObj = selectfield.options[selectfield.selectedIndex]; var behaviourName = scrapeText(optObj); var behaviourValue = optObj.value; simpleLog('DEBUG','editBehaviour called for field '+field_id+' with "'+behaviourName+'" ('+behaviourValue+').'); optObj.selected=false; setFixedValueForField(field_id, behaviourValue, behaviourName);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -