| 1 |
/**
|
| 2 |
* assetWizardCCK object for namespacing the following functions.
|
| 3 |
*/
|
| 4 |
Drupal.assetWizardCCK = {};
|
| 5 |
|
| 6 |
Drupal.assetWizardCCK.initialize = function(){
|
| 7 |
// Add asset button
|
| 8 |
Drupal.assetWizardCCK.initOperations();
|
| 9 |
|
| 10 |
// initialize multiple fields
|
| 11 |
// Drupal.assetWizardCCK.hideEmptyFields();
|
| 12 |
$('.asset-field-multiple').each(
|
| 13 |
function(){
|
| 14 |
Drupal.assetWizardCCK.compactMultiples(this);
|
| 15 |
}
|
| 16 |
);
|
| 17 |
|
| 18 |
// attach assetWizardInsert event handler
|
| 19 |
$(window).bind('assetWizardInsert', Drupal.assetWizardCCK.onAssetWizardInsert);
|
| 20 |
|
| 21 |
}
|
| 22 |
|
| 23 |
Drupal.assetWizardCCK.initializeMultiples = function(fieldset){
|
| 24 |
$('.form-item', fieldset).each(
|
| 25 |
function(){
|
| 26 |
var item = this;
|
| 27 |
if(parseInt($('.form-asset', item).val()) > 0){
|
| 28 |
$(item).hide();
|
| 29 |
}
|
| 30 |
else{
|
| 31 |
$icon = $('.assetfield-icon', item);
|
| 32 |
if($icon.size()){
|
| 33 |
var aid = $('.form-asset', item).val();
|
| 34 |
$icon.load(Drupal.settings.assetWizard.assetUrl + '/' + aid + '/icon/ajax');
|
| 35 |
}
|
| 36 |
x = i + 1;
|
| 37 |
}
|
| 38 |
}
|
| 39 |
);
|
| 40 |
|
| 41 |
};
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Shift all values to the front of the multiple field array, ensure that all
|
| 45 |
* populated items are visible, and vice-versa.
|
| 46 |
* @param fieldset
|
| 47 |
* the fieldset containing multiple asset form-items
|
| 48 |
*/
|
| 49 |
Drupal.assetWizardCCK.compactMultiples = function(fieldset){
|
| 50 |
var values = [];
|
| 51 |
$('.form-asset', fieldset).each(
|
| 52 |
function(){
|
| 53 |
var value = $(this).val();
|
| 54 |
if (value && value > 0) {
|
| 55 |
values.push(value);
|
| 56 |
}
|
| 57 |
$(this).val(0);
|
| 58 |
}
|
| 59 |
);
|
| 60 |
|
| 61 |
$('.form-item', fieldset).each(
|
| 62 |
function(i){
|
| 63 |
if (values[i]) {
|
| 64 |
$(this).show().find('.form-asset').val(values[i]);
|
| 65 |
}
|
| 66 |
else{
|
| 67 |
$(this).hide().find('.form-asset').val(values[i]);
|
| 68 |
}
|
| 69 |
}
|
| 70 |
);
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Hide empty form-items on multiple form-fields, leaving 1 empty item visibe to
|
| 75 |
* add another item. This assumes that all multiple fields are front-loaded,
|
| 76 |
* i.e. if the form-item at index 3 is not empty, the items from 0-3 will be
|
| 77 |
* shown, and if there is an item 4, it will be shown as the new item.
|
| 78 |
*/
|
| 79 |
Drupal.assetWizardCCK.hideEmptyFields = function(){
|
| 80 |
$('.asset-field-multiple').each(
|
| 81 |
function(){
|
| 82 |
var fieldset = this;
|
| 83 |
var empty = [];
|
| 84 |
|
| 85 |
Drupal.assetWizardCCK.compactMultiples(fieldset);
|
| 86 |
|
| 87 |
$('.form-item', fieldset).each(
|
| 88 |
function(i){
|
| 89 |
var item = this;
|
| 90 |
var val = $('.form-asset', item).val();
|
| 91 |
if(!val || val <= 0){
|
| 92 |
$(item).hide();
|
| 93 |
empty.push(i);
|
| 94 |
}
|
| 95 |
else if($('.asset-remove', item).size() == 0){
|
| 96 |
$('<a href="#" class="asset-remove">Remove</a>')
|
| 97 |
.click(Drupal.assetWizardCCK.onRemoveClick)
|
| 98 |
.wrap('<li></li>').parent().appendTo($('.assetfield-operations', item));
|
| 99 |
}
|
| 100 |
|
| 101 |
Drupal.assetWizardCCK.updatePreview($('.form-asset', item)[0]);
|
| 102 |
}
|
| 103 |
);
|
| 104 |
|
| 105 |
if (empty.length > 0) {
|
| 106 |
// $('.form-item', fieldset).eq(empty[0]).show();
|
| 107 |
}
|
| 108 |
}
|
| 109 |
);
|
| 110 |
};
|
| 111 |
|
| 112 |
Drupal.assetWizardCCK.initOperations = function(){
|
| 113 |
$('.asset-field-multiple').each(
|
| 114 |
function(){
|
| 115 |
var fieldset = this;
|
| 116 |
$('<span class="add">New asset</span>')
|
| 117 |
.click(Drupal.assetWizardCCK.addMultiple)
|
| 118 |
.insertAfter($('legend', fieldset));
|
| 119 |
|
| 120 |
$('.assetfield-container', fieldset).each(
|
| 121 |
function(){
|
| 122 |
$ops = $('<div class="ops"></div>');
|
| 123 |
$('<span class="edit">Edit</span>')
|
| 124 |
.click(Drupal.assetWizardCCK.editMultiple)
|
| 125 |
.appendTo($ops);
|
| 126 |
$('<span class="remove">Remove</span>')
|
| 127 |
.click(Drupal.assetWizardCCK.removeMultiple)
|
| 128 |
.appendTo($ops);
|
| 129 |
$(this).append($ops)
|
| 130 |
}
|
| 131 |
);
|
| 132 |
}
|
| 133 |
);
|
| 134 |
};
|
| 135 |
|
| 136 |
Drupal.assetWizardCCK.addMultiple = function(){
|
| 137 |
var $fieldset = $(this).parents().filter('fieldset');
|
| 138 |
Drupal.assetWizardCCK.compactMultiples($fieldset);
|
| 139 |
$('.form-item:hidden a.asset-wizard-start', $fieldset).eq(0).trigger('click');
|
| 140 |
};
|
| 141 |
|
| 142 |
Drupal.assetWizardCCK.removeMultiple = function(){
|
| 143 |
var $fieldset = $(this).parents().filter('fieldset');
|
| 144 |
$(this).parents().filter('.form-item').find('.form-asset').val(0);
|
| 145 |
Drupal.assetWizardCCK.compactMultiples($fieldset);
|
| 146 |
};
|
| 147 |
|
| 148 |
Drupal.assetWizardCCK.editMultiple = function(){
|
| 149 |
$(this).parents().filter('.form-item').find('a.asset-wizard-start').trigger('click');
|
| 150 |
|
| 151 |
};
|
| 152 |
|
| 153 |
Drupal.assetWizardCCK.updatePreview = function(input){
|
| 154 |
var input = input || Drupal.assetWizard.input;
|
| 155 |
var value = $(input).val();
|
| 156 |
var $item = $(input).parents().filter('.form-item');
|
| 157 |
|
| 158 |
if (value && value > 0) {
|
| 159 |
$.getJSON(Drupal.settings.assetWizard.assetUrl + 'field/' + value,
|
| 160 |
function(data){
|
| 161 |
$('.assetfield-icon img', $item).attr('src', data.icon);
|
| 162 |
$('.assetfield-title', $item).html(data.title);
|
| 163 |
}
|
| 164 |
);
|
| 165 |
}
|
| 166 |
else{
|
| 167 |
$('.assetfield-icon img', $item).attr('src', Drupal.settings.assetWizard.noIcon);
|
| 168 |
$('.assetfield-title', $item).html('');
|
| 169 |
}
|
| 170 |
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* assetWizardInsert event handler
|
| 175 |
*
|
| 176 |
* @param {Object} e
|
| 177 |
* the jQuery normalized event object
|
| 178 |
* @param {Object} wizard
|
| 179 |
* the Drupal.assetWizard object
|
| 180 |
* @param {int} value
|
| 181 |
* the value being inserted into the field
|
| 182 |
*/
|
| 183 |
Drupal.assetWizardCCK.onAssetWizardInsert = function(e, wizard, aid){
|
| 184 |
Drupal.assetWizardCCK.hideEmptyFields();
|
| 185 |
Drupal.assetWizardCCK.updatePreview();
|
| 186 |
}
|
| 187 |
|
| 188 |
Drupal.assetWizardCCK.onRemoveClick = function(e){
|
| 189 |
var item = $(this).parents().filter('.form-item');
|
| 190 |
$('.form-asset', item).val(0);
|
| 191 |
$(item).slideUp('fast', Drupal.assetWizardCCK.hideEmptyFields);
|
| 192 |
this.blur();
|
| 193 |
$(this).remove();
|
| 194 |
return false;
|
| 195 |
}
|
| 196 |
|
| 197 |
$(document).ready(Drupal.assetWizardCCK.initialize);
|
| 198 |
|