| 1 |
// $Id $
|
| 2 |
/**
|
| 3 |
* $file: addnode support functions to help with the following:
|
| 4 |
* - display popup window and transfer newly created node ids to the select box
|
| 5 |
* - controls to move nodes between the unselected and selected boxes.
|
| 6 |
* - controls to reorder ndoes in the selected boxes
|
| 7 |
* - controls to calculate the order of nodes and send that to the drupal BE
|
| 8 |
**/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* document ready hook - create a submit handler for the node edit form
|
| 12 |
* so that the addnode order fields are intiialized properly before the
|
| 13 |
* submit.
|
| 14 |
**/
|
| 15 |
$(document).ready( function() {
|
| 16 |
$('input.form-submit').click(function() {
|
| 17 |
|
| 18 |
//make sure we initialize all the addnode order fields
|
| 19 |
$('.addnode-order').each( function() {
|
| 20 |
//load the values with the orders.
|
| 21 |
var nm = this.id;
|
| 22 |
|
| 23 |
//field names contain a '-' but variables contain underscores
|
| 24 |
//so need to swap the two.
|
| 25 |
nm = nm.substring(5); //strip the "edit-" portion
|
| 26 |
nm = nm.substring(0, nm.length-5); //strip the "-nids" part
|
| 27 |
nm = nm.replace(/-/g, '_');
|
| 28 |
addnode_update_order(nm);
|
| 29 |
});
|
| 30 |
|
| 31 |
//cannot send an unselected select box back - just select all the nodes
|
| 32 |
//in the "to_" select box. - else validation will fail.
|
| 33 |
$('.addnode-select').each( function() {
|
| 34 |
var o = this.options;
|
| 35 |
for(i=0;i<o.length;i++) {
|
| 36 |
this.options[i].selected = true;
|
| 37 |
}
|
| 38 |
});
|
| 39 |
});
|
| 40 |
});
|
| 41 |
|
| 42 |
/**
|
| 43 |
* call back to help nodeassist insert the newly created node intou our select
|
| 44 |
* boxes.
|
| 45 |
* just identifies the select box and adds the newly created node to it.
|
| 46 |
*/
|
| 47 |
function updateNewNodes(doc, args, id, title) {
|
| 48 |
if(id != '') {
|
| 49 |
//Add new nids to select - will it let us?
|
| 50 |
var u = doc.getElementById(args);
|
| 51 |
var option = new Option(title,id);
|
| 52 |
var len = u.options.length;
|
| 53 |
u.options[len] = option;
|
| 54 |
u.options[len].selected = true;
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* for multi-select addnode fields - move the node from the selected list to
|
| 60 |
* the unselected list.
|
| 61 |
**/
|
| 62 |
function addnode_remove_item(field_name) {
|
| 63 |
var to_obj = $('#from_' + field_name).get(0);
|
| 64 |
var from_obj = $('#to_' + field_name).get(0);
|
| 65 |
addnode_move_item(from_obj, to_obj);
|
| 66 |
addnode_fix_empty(field_name);
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* for multi-select addnode fields - move the node to the selected list from
|
| 71 |
* the unselected list.
|
| 72 |
**/
|
| 73 |
function addnode_add_item(field_name) {
|
| 74 |
var from_obj = $('#from_' + field_name).get(0);
|
| 75 |
var to_obj = $('#to_' + field_name).get(0);
|
| 76 |
addnode_move_item(from_obj, to_obj);
|
| 77 |
addnode_fix_empty(field_name);
|
| 78 |
}
|
| 79 |
|
| 80 |
/**
|
| 81 |
* helper function to move the items from one select list to another.
|
| 82 |
**/
|
| 83 |
function addnode_move_item(from_obj, to_obj) {
|
| 84 |
var i = 0;
|
| 85 |
while(i < from_obj.options.length) {
|
| 86 |
if(from_obj.options[i].selected && from_obj.options[i].value !=0) {
|
| 87 |
var o = from_obj.options[i];
|
| 88 |
len = to_obj.options.length;
|
| 89 |
to_obj.options[len] = new Option(o.text, o.value, o.defaultSelected, o.selected);
|
| 90 |
from_obj.options[i] = null;
|
| 91 |
}
|
| 92 |
else {
|
| 93 |
i++;
|
| 94 |
}
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
/**
|
| 99 |
* don't let our lists become empty - this adds a "None" entry to the list
|
| 100 |
* if there are no entries in the list.
|
| 101 |
**/
|
| 102 |
function addnode_fix_empty_list(obj) {
|
| 103 |
if(obj.options.length == 0) {
|
| 104 |
obj.options[0] = new Option('None', 0);
|
| 105 |
obj.options[0].disabled = true;
|
| 106 |
}
|
| 107 |
|
| 108 |
if(obj.options.length > 1 && obj.options[0].value == 0) {
|
| 109 |
obj.options[0] = null;
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
| 113 |
/**
|
| 114 |
* fix empty list for a specific field
|
| 115 |
**/
|
| 116 |
function addnode_fix_empty(field_name) {
|
| 117 |
var from_obj = $('#from_' + field_name).get(0);
|
| 118 |
var to_obj = $('#to_' + field_name).get(0);
|
| 119 |
|
| 120 |
addnode_fix_empty_list(from_obj);
|
| 121 |
addnode_fix_empty_list(to_obj);
|
| 122 |
}
|
| 123 |
|
| 124 |
/**
|
| 125 |
* function to move the selected items up by one in the select box.
|
| 126 |
**/
|
| 127 |
function addnode_move_item_up(field_name) {
|
| 128 |
var to_obj = $('#to_' + field_name).get(0);
|
| 129 |
for(i=1; i< to_obj.options.length; i++) {
|
| 130 |
if(to_obj.options[i].selected) {
|
| 131 |
addnode_swap_item(to_obj, i, i-1);
|
| 132 |
}
|
| 133 |
}
|
| 134 |
}
|
| 135 |
|
| 136 |
/**
|
| 137 |
* helper function to swap two option elements in a select box
|
| 138 |
**/
|
| 139 |
function addnode_swap_item(obj, i, j) {
|
| 140 |
var o = obj.options;
|
| 141 |
var ti = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
|
| 142 |
var tj = new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
|
| 143 |
|
| 144 |
o[i] = tj;
|
| 145 |
o[j] = ti;
|
| 146 |
}
|
| 147 |
|
| 148 |
/**
|
| 149 |
* function to move the selected items down by one in the select box.
|
| 150 |
**/
|
| 151 |
function addnode_move_item_down(field_name) {
|
| 152 |
var to_obj = $('#to_' + field_name).get(0);
|
| 153 |
for(i = to_obj.options.length-2; i>=0; i--) {
|
| 154 |
if(to_obj.options[i].selected) {
|
| 155 |
addnode_swap_item(to_obj, i, i+1);
|
| 156 |
}
|
| 157 |
}
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
* function recalculates the new order or sequence of nodes in the select
|
| 162 |
* box and sets the value of a hidden field to the new order.
|
| 163 |
**/
|
| 164 |
function addnode_update_order(field_name) {
|
| 165 |
$('#to_' + field_name).each(function() {
|
| 166 |
var obj = this.options;
|
| 167 |
var ids = new Array();
|
| 168 |
for (var i = 0; i < obj.length; i++) {
|
| 169 |
if (obj[i].value != 0) {
|
| 170 |
ids[i] = obj[i].value;
|
| 171 |
}
|
| 172 |
}
|
| 173 |
var new_order = new String(ids.join(","));
|
| 174 |
field_name = field_name.replace(/_/g, '-');
|
| 175 |
$('#edit-' + field_name + '-nids').val(new_order);
|
| 176 |
});
|
| 177 |
}
|
| 178 |
|
| 179 |
function addnode_callback(fn,arg,id,title) {
|
| 180 |
if( id == '' ) {
|
| 181 |
return;
|
| 182 |
}
|
| 183 |
|
| 184 |
if(window[fn]) {
|
| 185 |
window[fn](this.parent.document,arg,id,title);
|
| 186 |
}
|
| 187 |
}
|