| 1 |
// $Id: jquery.netforum.js,v 1.3.2.1 2009/10/18 16:10:25 jamesmichaelhill Exp $
|
| 2 |
|
| 3 |
|
| 4 |
if (Drupal.jsEnabled){
|
| 5 |
$(document).ready(function(){
|
| 6 |
//first step, bind to the change event for our function drop down list
|
| 7 |
$('#edit-netforum-request').change(function(){
|
| 8 |
return updateParameterForm();
|
| 9 |
});
|
| 10 |
//second step, swap out the four default parameters that drupal renders
|
| 11 |
//and swap in the parameters for whatever is selected
|
| 12 |
|
| 13 |
updateParameterForm();
|
| 14 |
});
|
| 15 |
}
|
| 16 |
|
| 17 |
function getParamsForSelectedFunction() {
|
| 18 |
return xweb_functions[getSelectedFunction()];
|
| 19 |
}
|
| 20 |
|
| 21 |
function getSelectedFunction() {
|
| 22 |
return $('#edit-netforum-request').val();
|
| 23 |
}
|
| 24 |
|
| 25 |
//this is just a wrapper to animate the form changes
|
| 26 |
function updateParameterForm() {
|
| 27 |
|
| 28 |
$('#parameters').hide("fast");
|
| 29 |
if (getParamsForSelectedFunction() == "parameters") {
|
| 30 |
$.getJSON(document.URL + "/params_for/" + getSelectedFunction(), {}, function(json){
|
| 31 |
xweb_functions[getSelectedFunction()] = json;
|
| 32 |
updateParameters(getParamsForSelectedFunction());
|
| 33 |
$('#parameters').show("normal");
|
| 34 |
});
|
| 35 |
}
|
| 36 |
else {
|
| 37 |
updateParameters(getParamsForSelectedFunction());
|
| 38 |
$('#parameters').show("normal");
|
| 39 |
}
|
| 40 |
|
| 41 |
}
|
| 42 |
|
| 43 |
|
| 44 |
//This function looks at the selected function, references it with an array placed in the page
|
| 45 |
//header by the drupal render, and adjusts the fields displayed. They are all named
|
| 46 |
//netforum_param_1, netforum_param_2, etc
|
| 47 |
function updateParameters(parameter_list) {
|
| 48 |
|
| 49 |
//the xweb_functions variable is included in the head of the page by the module.
|
| 50 |
//it is an array with the function names and the parameters listed
|
| 51 |
|
| 52 |
var paramsDiv = $('#parameters');
|
| 53 |
paramsDiv.empty();
|
| 54 |
if (parameter_list.length == 0) {
|
| 55 |
paramsDiv.html("<i>no parameters</i>");
|
| 56 |
}
|
| 57 |
else {
|
| 58 |
addParamsInto(paramsDiv[0], parameter_list);
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
function addParamsInto(paramsDiv, parameter_list) {
|
| 63 |
if (typeof(addParamsInto.paramId) == "undefined") {
|
| 64 |
addParamsInto.paramId = 0;
|
| 65 |
}
|
| 66 |
|
| 67 |
if (typeof(addParamsInto.objStack) == "undefined") {
|
| 68 |
addParamsInto.objStack = [];
|
| 69 |
}
|
| 70 |
|
| 71 |
jQuery.each(parameter_list, function(key, val) {
|
| 72 |
if (typeof(val) == "string") {
|
| 73 |
addParamsInto.paramId += 1;
|
| 74 |
paramsDiv.appendChild(buildInputDiv(addParamsInto.paramId, val, addParamsInto.objStack));
|
| 75 |
}
|
| 76 |
else if (typeof(val) == "object") {
|
| 77 |
addParamsInto.objStack.push(key);
|
| 78 |
var legend = document.createElement('legend');
|
| 79 |
legend.innerHTML = key;
|
| 80 |
|
| 81 |
var newFieldset = document.createElement('fieldset');
|
| 82 |
newFieldset.setAttribute('class', 'collapsible collapsed');
|
| 83 |
newFieldset.appendChild(legend);
|
| 84 |
|
| 85 |
addParamsInto(newFieldset, val);
|
| 86 |
|
| 87 |
paramsDiv.appendChild(newFieldset);
|
| 88 |
reBindFieldsets();
|
| 89 |
addParamsInto.objStack.pop();
|
| 90 |
}
|
| 91 |
});
|
| 92 |
}
|
| 93 |
|
| 94 |
function buildInputDiv(pnum, pname, stack) {
|
| 95 |
var prefix = "";
|
| 96 |
if (stack.length > 0) {
|
| 97 |
prefix = "[" + stack.join("][") + "]";
|
| 98 |
}
|
| 99 |
|
| 100 |
var newDiv = document.createElement('div');
|
| 101 |
newDiv.setAttribute('id', 'netforum_custom_'+pnum);
|
| 102 |
newDiv.setAttribute('class', 'form-item');
|
| 103 |
|
| 104 |
var newLabel = document.createElement('label');
|
| 105 |
newLabel.setAttribute('for', 'edit-netforum-param-'+pnum);
|
| 106 |
newLabel.innerHTML = pname + ":";
|
| 107 |
newDiv.appendChild(newLabel);
|
| 108 |
|
| 109 |
var newInput = document.createElement('input');
|
| 110 |
newInput.setAttribute('type', 'text');
|
| 111 |
newInput.setAttribute('maxlength', '2000');
|
| 112 |
newInput.setAttribute('name', 'netforum_params'+prefix+"["+pname+"]");
|
| 113 |
newInput.setAttribute('id', 'edit-netforum-param-'+pnum);
|
| 114 |
newInput.setAttribute('size', 60);
|
| 115 |
newInput.setAttribute('class', 'form-text');
|
| 116 |
|
| 117 |
// When we are redisplaying a form, this var will be populated. We work through
|
| 118 |
// the stack and walk through the vars to set the defaults
|
| 119 |
if (typeof(form_defaults) != "undefined" && form_defaults[getSelectedFunction()]) {
|
| 120 |
var tmpStack = stack,
|
| 121 |
tmpStackLen = stack.length,
|
| 122 |
defaults = form_defaults[getSelectedFunction()];
|
| 123 |
|
| 124 |
for (i=0;i<tmpStackLen;i++) {
|
| 125 |
if (typeof(defaults[tmpStack[i]]) != "undefined") {
|
| 126 |
defaults = defaults[tmpStack[i]];
|
| 127 |
}
|
| 128 |
}
|
| 129 |
if (typeof(defaults[pname]) != "undefined") {
|
| 130 |
newInput.value = defaults[pname];
|
| 131 |
}
|
| 132 |
}
|
| 133 |
newDiv.appendChild(newInput);
|
| 134 |
return newDiv;
|
| 135 |
}
|
| 136 |
|
| 137 |
//Same thing as the document redy function in drupal's collapse.js
|
| 138 |
function reBindFieldsets() {
|
| 139 |
$('fieldset.collapsible > legend').each(function() {
|
| 140 |
var fieldset = $(this.parentNode);
|
| 141 |
// Expand if there are errors inside
|
| 142 |
if ($('input.error, textarea.error, select.error', fieldset).size() > 0) {
|
| 143 |
fieldset.removeClass('collapsed');
|
| 144 |
}
|
| 145 |
|
| 146 |
// Turn the legend into a clickable link and wrap the contents of the fieldset
|
| 147 |
// in a div for easier animation
|
| 148 |
var text = this.innerHTML;
|
| 149 |
$(this).empty().append($('<a href="#">'+ text +'</a>').click(function() {
|
| 150 |
var fieldset = $(this).parents('fieldset:first')[0];
|
| 151 |
// Don't animate multiple times
|
| 152 |
if (!fieldset.animating) {
|
| 153 |
fieldset.animating = true;
|
| 154 |
Drupal.toggleFieldset(fieldset);
|
| 155 |
}
|
| 156 |
return false;
|
| 157 |
})).after($('<div class="fieldset-wrapper"></div>').append(fieldset.children(':not(legend)')));
|
| 158 |
});
|
| 159 |
}
|