| 1 |
// $Id$
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Attaches add more fields behaviour to any form.
|
| 5 |
* Dynamic adding of fields requires:
|
| 6 |
* - a button to click to produce more fields (id="field-name-more")
|
| 7 |
* - a wrapper div around the set of fields to be duplicated (id="field-name-wrapper")
|
| 8 |
* additional fields will be prepended to the end of this fieldset
|
| 9 |
* - a hidden element counter with the current number of fields (id="field-name-count")
|
| 10 |
* - field-name should be replaced by a unique identifier for your field
|
| 11 |
* - a callback function which produces an additional field
|
| 12 |
*/
|
| 13 |
Drupal.linkAutoAttach = function() {
|
| 14 |
$('input.more-links').each(function() {
|
| 15 |
var uri = this.value;
|
| 16 |
// Extract the base name from the id (my-text-field-url -> my-text-field).
|
| 17 |
var base = this.id.substring(0, this.id.length - 9);
|
| 18 |
var button = base + '-more';
|
| 19 |
var wrapper = base + '-wrapper';
|
| 20 |
var counter = base + '-count';
|
| 21 |
var link = new Drupal.jslink(uri, button, wrapper, counter);
|
| 22 |
});
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* JS jslink object.
|
| 27 |
*/
|
| 28 |
Drupal.jslink = function(uri, button, wrapper, counter) {
|
| 29 |
this.button = '#'+ button;
|
| 30 |
this.wrapper = '#'+ wrapper;
|
| 31 |
this.counter = '#'+ counter;
|
| 32 |
Drupal.redirectFormButton(uri, $(this.button).get(0), this);
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Handler for the form redirection submission.
|
| 37 |
*/
|
| 38 |
Drupal.jslink.prototype.onsubmit = function() {
|
| 39 |
// Increment count
|
| 40 |
var count = parseInt($(this.counter).val());
|
| 41 |
$(this.counter).val(count + 1);
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Handler for the form redirection completion.
|
| 46 |
*/
|
| 47 |
Drupal.jslink.prototype.oncomplete = function(data) {
|
| 48 |
// Avoid unnecessary scrolling
|
| 49 |
Drupal.freezeHeight();
|
| 50 |
|
| 51 |
// Place HTML into temporary div
|
| 52 |
var div = document.createElement('div');
|
| 53 |
$(div).html(data);
|
| 54 |
|
| 55 |
// Append to form and update behaviour
|
| 56 |
$(div).hide();
|
| 57 |
$(this.wrapper).append(div);
|
| 58 |
$(div).slideDown('fast');
|
| 59 |
Drupal.linkAutoAttach();
|
| 60 |
|
| 61 |
Drupal.unfreezeHeight();
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Handler for the form redirection error.
|
| 66 |
*/
|
| 67 |
Drupal.jslink.prototype.onerror = function(error) {
|
| 68 |
alert('An error occurred:\n\n'+ error);
|
| 69 |
}
|
| 70 |
|
| 71 |
// Global killswitch
|
| 72 |
if (Drupal.jsEnabled) {
|
| 73 |
$(document).ready(Drupal.linkAutoAttach);
|
| 74 |
}
|