| 1 |
//if (Drupal.jsEnabled) {
|
| 2 |
var table= new Array();
|
| 3 |
var i=0;
|
| 4 |
var parent_num=1;
|
| 5 |
var table_count=0;
|
| 6 |
var parent_forms=new Array(1);
|
| 7 |
$(document).ready(function(){
|
| 8 |
//debugger;
|
| 9 |
$("div.this_is_a_parent", "#node-form").each(function(){
|
| 10 |
var field_hidden_text=$(this).html().split(' ')[0]; //the hidden text contains details needed to determine the children such as the field type name of the parent
|
| 11 |
var the_parent_form=$(this).prev();
|
| 12 |
$(the_parent_form).each(function(){
|
| 13 |
var parent_form_item=get_prev_form_item($(this)); //get the real parent form-item the '.this_is_a_parent' element is after the form-item element
|
| 14 |
var has_attribute_parent_num=$(parent_form_item).attr('parent_num');
|
| 15 |
if ((($(parent_form_item).attr('parent_num'))===null)||(typeof ($(parent_form_item).attr('parent_num'))=="undefined")){ //check if this dom element has been inserted to the parent_forms array
|
| 16 |
$(parent_form_item).attr('parent_num',parent_num); //set the parent number attribute
|
| 17 |
parent_forms.push(new Array());
|
| 18 |
parent_forms[parent_num]["children_arr"]=new Array();
|
| 19 |
parent_forms[parent_num]["parents_arr"]=new Array();
|
| 20 |
parent_forms[parent_num++]["parent_form"]=$(parent_form_item);
|
| 21 |
}
|
| 22 |
//the next code is for setting the checkboxes/radios hide/show options
|
| 23 |
if ($(parent_form_item).find(':radio,.form-checkboxes').size()){ //the next code is for setting the radios/checkboxes groups hide/show options
|
| 24 |
$(parent_form_item).find(':radio,:checkbox').each(function (){//go through every radio/checkbox button in the parent form-item
|
| 25 |
var parent_checked=$(this).attr('checked')?1:0; //get the state of the radio/checkbox
|
| 26 |
var _button=$(this);
|
| 27 |
var txt='option_text='+$(_button).val();
|
| 28 |
var parent_id=($(parent_form_item).attr('parent_num'));
|
| 29 |
$(_button).attr('parent_num',parent_id); //set the parent number of this checkbox/radio button
|
| 30 |
var $childs = $('div.child_of_'+field_hidden_text); //find all child elements of this form-item
|
| 31 |
$childs.each(function(){ //go through all the dependent elements and hide/show them according to the state of the radio/checkbox button
|
| 32 |
if ($(this).html().split('option_text=')[1]==$(_button).val()){
|
| 33 |
//this specific checkbox/radio button is the parent of this field
|
| 34 |
var new_row={"form_item":$(parent_form_item),"parent":$(_button), "child":get_prev_form_item($(this))};
|
| 35 |
var in_array=$(this).attr('in_array');
|
| 36 |
if ((in_array===null)||(typeof in_array=="undefined")){//these 'if' makes sure that there are no duplicates in the table
|
| 37 |
var parent_num_attr=$(parent_form_item).attr('parent_num');
|
| 38 |
parent_forms[parent_id]["children_arr"].push(get_prev_form_item($(this)));
|
| 39 |
parent_forms[parent_id]["parents_arr"].push($(_button));
|
| 40 |
parent_forms[parent_id]["parent_form"].attr('is_selection',0);
|
| 41 |
table[table_count++]=new_row;
|
| 42 |
$(this).attr('in_array',1);
|
| 43 |
if ($(this).html().indexOf('put_after_parent')>0){
|
| 44 |
//children that need to be put after their parents are only moved after the table is complete, so they are being signed for future recognition
|
| 45 |
get_prev_form_item($(this)).attr('put_after_parent',1);
|
| 46 |
}
|
| 47 |
}
|
| 48 |
}
|
| 49 |
});
|
| 50 |
$(this).click(function(){//whenever a click event occurs - go through all radio buttons, check their state and update the document
|
| 51 |
//this is important because in radio buttons choosing one option sets the previously selected radio button as unselected
|
| 52 |
var click_parent_num=$(this).attr('parent_num');
|
| 53 |
if ((click_parent_num!==null)&&(typeof click_parent_num!="undefined")) {
|
| 54 |
var parentNum=parseInt($(this).attr('parent_num'));
|
| 55 |
show_hide_element(parent_forms[parseInt($(this).attr('parent_num'))]["parent_form"]);
|
| 56 |
}
|
| 57 |
});
|
| 58 |
});
|
| 59 |
}
|
| 60 |
else if ($(parent_form_item).find(':checkbox').size()){//the form item input is from type single on/off checkbox
|
| 61 |
var parent_checked=$(parent_form_item).find(':checkbox').attr('checked')?1:0;//get the check state of the checkbox
|
| 62 |
$('div.child_of_'+field_hidden_text).each(function(){
|
| 63 |
var _button=$(parent_form_item).find(':checkbox')
|
| 64 |
var new_row={"form_item":$(parent_form_item),"parent":$(_button), "child":get_prev_form_item($(this))};
|
| 65 |
var in_array=$(this).attr('in_array');
|
| 66 |
if ((in_array===null)||(typeof in_array=="undefined")){
|
| 67 |
var parent_id=($(parent_form_item).attr('parent_num'));
|
| 68 |
$(_button).attr('parent_num',parent_id);
|
| 69 |
parent_forms[parent_id]["children_arr"].push(get_prev_form_item($(this)));
|
| 70 |
parent_forms[parent_id]["parents_arr"].push($(_button));
|
| 71 |
parent_forms[parent_id]["parent_form"].attr('is_selection',0);
|
| 72 |
table[table_count++]=new_row;
|
| 73 |
$(this).attr('in_array',1);
|
| 74 |
if ($(this).html().indexOf('put_after_parent')>0){
|
| 75 |
get_prev_form_item($(this)).attr('put_after_parent',1);
|
| 76 |
}
|
| 77 |
}
|
| 78 |
});
|
| 79 |
$(parent_form_item).find(':checkbox').click(function(){//set the click event of the checkbox
|
| 80 |
show_hide_element(parent_forms[parseInt($(this).attr('parent_num'))]["parent_form"]);
|
| 81 |
});
|
| 82 |
}
|
| 83 |
else if ($(parent_form_item).find('select').size()){
|
| 84 |
var selection_form_item=$(this);
|
| 85 |
var selection_field=$(parent_form_item).find('select');
|
| 86 |
var selectedIndex=selection_field[0].selectedIndex;
|
| 87 |
var options=selection_field[0].options;
|
| 88 |
var $childs = $('div.child_of_'+field_hidden_text); //find all child elements of this form-item
|
| 89 |
$childs.each(function(){ //go through all the dependent elements and hide/show them according to the state of the radio/checkbox button
|
| 90 |
var new_row={"form_item":$(parent_form_item),"parent":$(selection_field), "child":get_prev_form_item($(this))};
|
| 91 |
var in_array=$(this).attr('in_array');
|
| 92 |
var parent_id=($(parent_form_item).attr('parent_num'));
|
| 93 |
if ((in_array===null)||(typeof in_array=="undefined")){//these 'if' makes sure that there are no duplicates in the table
|
| 94 |
var parent_num_attr=$(parent_form_item).attr('parent_num');
|
| 95 |
var child=get_prev_form_item($(this));
|
| 96 |
parent_forms[parent_id]["children_arr"].push($(child));
|
| 97 |
parent_forms[parent_id]["parents_arr"].push($(selection_field));
|
| 98 |
parent_forms[parent_id]["parent_form"].attr('is_selection',1);
|
| 99 |
table[table_count++]=new_row;
|
| 100 |
$(this).attr('in_array',1);
|
| 101 |
var option_text=$(this).html().split('option_text=')[1];
|
| 102 |
var option_index;
|
| 103 |
for (var i=0; i<options.length; ++i){
|
| 104 |
if (options[i].text==option_text)
|
| 105 |
option_index=i;
|
| 106 |
}
|
| 107 |
$(child).attr('option_index',option_index);
|
| 108 |
$(selection_field).attr('parent_num',parent_id);
|
| 109 |
if ($(this).html().indexOf('put_after_parent')>0){
|
| 110 |
//children that need to be put after their parents are only moved after the table is complete, so they are being signed for future recognition
|
| 111 |
get_prev_form_item($(this)).attr('put_after_parent',1);
|
| 112 |
}
|
| 113 |
}
|
| 114 |
});
|
| 115 |
$(selection_field).change(function(){
|
| 116 |
show_hide_element(parent_forms[parseInt($(this).attr('parent_num'))]["parent_form"]);
|
| 117 |
});
|
| 118 |
}
|
| 119 |
//debugger;
|
| 120 |
// }
|
| 121 |
});
|
| 122 |
});
|
| 123 |
$('#autosave-status #view a').click(function() {
|
| 124 |
show_hide_elements();
|
| 125 |
});
|
| 126 |
arrange_children();
|
| 127 |
show_hide_elements();
|
| 128 |
});
|
| 129 |
// });
|
| 130 |
|
| 131 |
function hide_show_childs_by_element(childElement,checked, parent){
|
| 132 |
//this function gets a sign element, a button state and the parent of the dependent element
|
| 133 |
//in order to get the dependent element get_prev_form_item is used
|
| 134 |
//the parent element is used in order to hide/show the dependent element accroding to the visibility of the parent
|
| 135 |
//if the parent is hidden the dependent elements should also be hidden.
|
| 136 |
|
| 137 |
if (!parent)
|
| 138 |
return;
|
| 139 |
|
| 140 |
//get the visivility of the parent, if not set then set it as "true"
|
| 141 |
var parent_visible=$(parent).attr('_visible');
|
| 142 |
if ((parent_visible===null)||(typeof parent_visible=="undefined")){
|
| 143 |
$(parent).attr('_visible',"true");
|
| 144 |
parent_visible="true";
|
| 145 |
}
|
| 146 |
|
| 147 |
//get the child element and check its visibility. if not set then set it and all
|
| 148 |
//its children(important because radio-button are form-item children of a parent form-item, the latter is the element that actually being shown/hidden)
|
| 149 |
//form-item elements as true
|
| 150 |
//var childElement=get_prev_form_item($(childSignElement));//maybe should be in each clause
|
| 151 |
var child_visible= $(childElement).attr('_visible');
|
| 152 |
var childElementFormItems=$(childElement).find('div.form-item');
|
| 153 |
if ((child_visible===null)||(typeof child_visible=="undefined")){
|
| 154 |
$(childElementFormItems).attr('_visible',"true");
|
| 155 |
$(childElement).attr('_visible',"true");
|
| 156 |
child_visible="true";
|
| 157 |
}
|
| 158 |
var change=0;
|
| 159 |
//show/hide the child element and update its visible state
|
| 160 |
//aftert the update call hide_show_elements in order to update its own dependent elements
|
| 161 |
var firstChild=$(childElement).find(":first");
|
| 162 |
var firstChildIsFieldset= firstChild.is("fieldset"); //this means these form item is a related subform element
|
| 163 |
|
| 164 |
if ((checked)&&(child_visible=="false")&&(parent_visible=="true")){
|
| 165 |
$(childElement).show('slow');
|
| 166 |
if (!firstChildIsFieldset)
|
| 167 |
$(childElementFormItems).attr('_visible',"true");
|
| 168 |
$(childElement).attr('_visible',"true");
|
| 169 |
change=1;
|
| 170 |
}
|
| 171 |
else if (((!checked)||(parent_visible=="false"))&&(child_visible=="true")){
|
| 172 |
$(childElement).hide('slow');
|
| 173 |
if (!firstChildIsFieldset)
|
| 174 |
$(childElementFormItems).attr('_visible',"false");
|
| 175 |
$(childElement).attr('_visible',"false");
|
| 176 |
change=1;
|
| 177 |
}
|
| 178 |
if ((change==1)&&(!firstChildIsFieldset)){//
|
| 179 |
show_hide_element($(childElement));
|
| 180 |
}
|
| 181 |
}
|
| 182 |
|
| 183 |
function get_prev_form_item(element){
|
| 184 |
while(!(($(element).is("div.form-item"))||($(element).is("fieldset")))){
|
| 185 |
element=element.prev();
|
| 186 |
}
|
| 187 |
return element;
|
| 188 |
}
|
| 189 |
|
| 190 |
|
| 191 |
function get_parent_form_item(element){
|
| 192 |
while(!(($(element).is("div.form-item"))||($(element).is("fieldset")))){
|
| 193 |
element=element.parent();
|
| 194 |
}
|
| 195 |
return element;
|
| 196 |
}
|
| 197 |
|
| 198 |
function show_hide_elements(){
|
| 199 |
//this function is being called whenever a radio button is clicked or shown/hidden
|
| 200 |
//it iterates through all elements that depend on a radio button
|
| 201 |
for(var parent_form=1; parent_form<parent_forms.length; ++parent_form){
|
| 202 |
show_hide_element(parent_forms[parent_form]["parent_form"]);
|
| 203 |
}
|
| 204 |
}
|
| 205 |
|
| 206 |
function show_hide_element(parent){
|
| 207 |
//this function is being called whenever a radio button is clicked or shown/hidden
|
| 208 |
//it iterates through all elements that depend on a radio button
|
| 209 |
var parent_id=$(parent).attr('parent_num');
|
| 210 |
if ((parent_id===null)||(typeof parent_id=="undefined"))
|
| 211 |
return;
|
| 212 |
var parent_form=parent_forms[parseInt(parent_id)];
|
| 213 |
var number_of_children=parent_form["children_arr"].length;
|
| 214 |
for (var child=0; child<number_of_children; child++){
|
| 215 |
var checked=0;
|
| 216 |
//var selection_field=$(parent)
|
| 217 |
var sdkjh=typeof ($(parent).attr('is_selection'));
|
| 218 |
if ($(parent).attr('is_selection') == 1){
|
| 219 |
var selection_field=parent_form["parents_arr"][child];
|
| 220 |
if (parent_form["children_arr"][child].attr('option_index')== selection_field[0].selectedIndex){
|
| 221 |
checked=1;
|
| 222 |
}
|
| 223 |
}
|
| 224 |
hide_show_childs_by_element(parent_form["children_arr"][child], parent_form["parents_arr"][child].attr('checked')?1:checked, parent_form["parent_form"]);
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
function arrange_children(){
|
| 229 |
for (var index=0; index<table.length; index++){
|
| 230 |
if (table[index]["child"].attr('put_after_parent')){
|
| 231 |
table[index]["child"].appendTo(get_parent_form_item($(table[index]["parent"]).parent()));
|
| 232 |
table[index]["child"].addClass("child");
|
| 233 |
}
|
| 234 |
}
|
| 235 |
}
|