|
1 | | -/*! |
2 | | - * jQuery Listable Plugin |
3 | | - * Author: @lejeunerenard |
4 | | - * Licensed under the LGPL license |
| 1 | +/*! Listable v0.1.0 - 2013-11-19 |
| 2 | + * Author: Sean Zellmer |
| 3 | + * License: MIT |
5 | 4 | */ |
6 | 5 |
|
7 | | - |
8 | 6 | ;(function ( $, window, document, undefined ) { |
9 | 7 |
|
10 | 8 | var update = false; // Whether the current form is an update or not |
|
33 | 31 | 'form_vault' : '#listable-form-vault', |
34 | 32 | 'gear_image' : '', |
35 | 33 | 'gear_transition' : 'fade', |
36 | | - 'image_dragging' : false, |
| 34 | + 'image_dragging' : true, |
37 | 35 | 'initial_add' : true, |
38 | | - 'max_depth' : false, |
| 36 | + 'max_depth' : null, |
39 | 37 | 'shallower_image' : '/javascripts/listable/images/left_arrow.png', |
40 | 38 | 'types' : [], |
41 | 39 | // Callbacks |
|
135 | 133 | $(settings.form_vault).prepend('<div id="msg-listable"></div>'); |
136 | 134 | } |
137 | 135 |
|
138 | | - if ( settings.image_dragging ) { |
| 136 | + if ( ! settings.image_dragging ) { |
139 | 137 | this.element.find('img').live('dragstart', function(event) { event.preventDefault(); }); |
140 | 138 | } |
141 | 139 |
|
|
224 | 222 | if (settings.gear_image) { // If the delete setting is set to true then enable the delete button |
225 | 223 | this.element.find('.listable_item_buttons').hide(0); |
226 | 224 | this.element.find('.listable_gear').live('click', function(event){ |
227 | | - event.preventDefault(); |
228 | | - if (settings.gear_transition == 'slide') { |
229 | | - $(this).siblings('.listable_item_buttons').fadeToggle('fast'); |
230 | | - } else { |
231 | | - $(this).siblings('.listable_item_buttons').fadeToggle('fast'); |
232 | | - } |
| 225 | + event.preventDefault(); |
| 226 | + if (settings.gear_transition == 'slide') { |
| 227 | + $(this).siblings('.listable_item_buttons').slideToggle('fast'); |
| 228 | + } else { |
| 229 | + $(this).siblings('.listable_item_buttons').fadeToggle('fast'); |
| 230 | + } |
233 | 231 | }); |
234 | 232 | } |
235 | 233 | if (settings.delete) { // If the delete setting is set to true then enable the delete button |
|
427 | 425 |
|
428 | 426 | _setOption: function( key, value ) { |
429 | 427 | switch( key ) { |
430 | | - case 'current_divider': |
431 | | - this.current_divider = value; |
432 | | - default: |
433 | | - this.options[ key ] = value; |
434 | | - break; |
| 428 | + case 'current_divider': |
| 429 | + this.current_divider = value; |
| 430 | + default: |
| 431 | + this.options[ key ] = value; |
| 432 | + break; |
435 | 433 | } |
436 | 434 |
|
437 | 435 | $.Widget.prototype._setOption.apply( this, arguments ); |
438 | 436 | }, |
439 | 437 |
|
| 438 | + /* |
| 439 | + * Save |
| 440 | + * Takes the listable item type and creates / updates the listable. |
| 441 | + * |
| 442 | + */ |
440 | 443 |
|
441 | 444 | save: function( itemType, options1, options2 ) { |
| 445 | + // Universal variables |
442 | 446 | var settings = this.options; |
443 | | - if ( $.isFunction(options1) ) { |
444 | | - settings.beforeSave = options1; |
445 | | - } else if ( typeof options1 != "undefined") { |
446 | | - if ( $.isFunction(options1.beforeSave) ) { |
447 | | - settings.beforeSave = options1.beforeSave; |
448 | | - } |
449 | | - if ( $.isFunction(options1.after_save) ) { |
450 | | - settings.after_save = options1.after_save; |
| 447 | + var that = this; |
| 448 | + var vars = {}; |
| 449 | + |
| 450 | + // Check if second param is a function |
| 451 | + if ( $.isFunction(options1) ) { |
| 452 | + // If so set it to the beforeSave callback |
| 453 | + settings.beforeSave = options1; |
| 454 | + // If not then if the second param is not undefined, treat it as an options object |
| 455 | + } else if ( typeof options1 != "undefined") { |
| 456 | + if ( $.isFunction(options1.beforeSave) ) { |
| 457 | + settings.beforeSave = options1.beforeSave; |
| 458 | + } |
| 459 | + if ( $.isFunction(options1.after_save) ) { |
| 460 | + settings.after_save = options1.after_save; |
| 461 | + } |
| 462 | + if ( $.isFunction(options1.beforeDisplay) ) { |
| 463 | + settings.beforeDisplay = options1.beforeDisplay; |
| 464 | + } |
| 465 | + if ( typeof options1.vars !== 'undefined' ) vars = options1.vars; |
451 | 466 | } |
452 | | - if ( $.isFunction(options1.beforeDisplay) ) { |
453 | | - settings.beforeDisplay = options1.beforeDisplay; |
| 467 | + // Set after save if given as the second option |
| 468 | + if ( $.isFunction(options2) ) { |
| 469 | + settings.after_save = options2; |
454 | 470 | } |
455 | | - } |
456 | | - if ( $.isFunction(options2) ) { |
457 | | - settings.after_save = options2; |
458 | | - } |
459 | | - var that = this; |
460 | | - if (update) { // Check to see if the user is updating an item or creating a new one |
461 | | - var vars = {}; |
| 471 | + |
| 472 | + // Call beforeSave if it exists |
462 | 473 | if ($.isFunction(settings.beforeSave)) { |
463 | 474 | settings.beforeSave(itemType, vars); |
464 | 475 | } |
465 | | - $.each(itemType.variables, function(index, value) { // Iterate through field variables and colect values. These values are stored in vars under the name of the variable |
466 | | - var input_element; // The input element for this variable |
467 | | - var prefix; // Prefixes can be used to distinguish inputs of the same "name" |
468 | 476 |
|
469 | | - // Determine Prefix |
470 | | - if (itemType.prefix) { |
471 | | - prefix = itemType.prefix+'_'; |
472 | | - } else { |
473 | | - prefix = ''; |
474 | | - } |
| 477 | + // If vars wasnt defined in an option, load it up with form values. |
| 478 | + if ($.isEmptyObject(vars)) { |
| 479 | + // Iterate through field variables and colect values. These values are stored in vars under the name of the variable. |
| 480 | + $.each(itemType.variables, function(index, value) { |
| 481 | + var input_element; // The input element for this variable |
| 482 | + var prefix; // Prefixes can be used to distinguish inputs of the same "name" |
475 | 483 |
|
476 | | - // Find element |
477 | | - input_element = that._findInput({ |
478 | | - formId: itemType.formid, |
479 | | - prefix: prefix, |
480 | | - value: value |
481 | | - }); |
| 484 | + // Determine Prefix |
| 485 | + if (itemType.prefix) { |
| 486 | + prefix = itemType.prefix+'_'; |
| 487 | + } else { |
| 488 | + prefix = ''; |
| 489 | + } |
482 | 490 |
|
483 | | - if (input_element.attr('type') == 'checkbox') { |
484 | | - if (input_element.attr('checked')) { |
485 | | - vars[value] = 1; |
486 | | - } else { |
487 | | - vars[value] = 0; |
488 | | - } |
| 491 | + // Find element |
| 492 | + input_element = that._findInput({ |
| 493 | + formId: itemType.formid, |
| 494 | + prefix: prefix, |
| 495 | + value: value |
| 496 | + }); |
489 | 497 |
|
490 | | - } else { |
491 | | - vars[value] = input_element.val(); |
492 | | - } |
493 | | - settings.variable_vault.find('input.'+update+'[name="'+value+'\[\]"]').val(vars[value]); // Update all the hidden input fields with values collected |
494 | | - }); |
495 | | - this.refresh(); |
496 | | - } else { |
497 | | - var vars = {}; |
498 | | - if ($.isFunction(settings.beforeSave)) { |
499 | | - settings.beforeSave(itemType, vars); |
| 498 | + if (input_element.attr('type') == 'checkbox') { |
| 499 | + if (input_element.attr('checked')) { |
| 500 | + vars[value] = 1; |
| 501 | + } else { |
| 502 | + vars[value] = 0; |
| 503 | + } |
| 504 | + } else { |
| 505 | + vars[value] = input_element.val(); |
| 506 | + } |
| 507 | + }); |
500 | 508 | } |
501 | | - $.each(itemType.variables, function(index, value) { // Iterate through field variables and colect values. These values are stored in vars under the name of the variable |
502 | | - var input_element; // The input element for this variable |
503 | | - var prefix; // Prefixes can be used to distinguish inputs of the same "name" |
504 | 509 |
|
505 | | - // Determine Prefix |
506 | | - if (itemType.prefix) { |
507 | | - prefix = itemType.prefix+'_'; |
| 510 | + // Update or create hidden inputs |
| 511 | + $.each(vars, function(name, value) { |
| 512 | + if (update) { // Check to see if the user is updating an item or creating a new one |
| 513 | + settings.variable_vault.find('input.'+update+'[name="'+name+'\[\]"]').val(value); // Update all the hidden input fields with values collected |
508 | 514 | } else { |
509 | | - prefix = ''; |
| 515 | + $(settings.variable_vault).append('<input type="hidden" name="'+name+'[]" value="'+value+'" class="field_'+$.fn.listable.counter+'" >'); // Add the hidden input element to the variable vault |
510 | 516 | } |
| 517 | + }); |
511 | 518 |
|
512 | | - // Find element |
513 | | - input_element = that._findInput({ |
514 | | - formId: itemType.formid, |
515 | | - prefix: prefix, |
516 | | - value: value |
517 | | - }); |
518 | | - |
519 | | - if (input_element.attr('type') == 'checkbox') { |
520 | | - if (input_element.attr('checked')) { |
521 | | - vars[value] = 1; |
522 | | - } else { |
523 | | - vars[value] = 0; |
524 | | - } |
525 | | - |
526 | | - } else { |
527 | | - vars[value] = input_element.val(); |
528 | | - } |
529 | | - $(settings.variable_vault).append('<input type="hidden" name="'+value+'[]" value="'+vars[value]+'" class="field_'+$.fn.listable.counter+'" >'); // Add the hidden input element to the variable vault |
530 | | - }); |
531 | | - // Now add standard variables like order and type |
| 519 | + // Now add standard variables like order and type |
| 520 | + if (!update) { |
532 | 521 | if (settings.add_after) { |
533 | 522 | // Add order with 1 plus the current dividers order |
534 | 523 | $(settings.variable_vault).append('<input type="hidden" name="order[]" value="'+ ( parseInt( $('.' + that.current_divider.attr('class').replace(/field_divider /,'') + '[name="order\[\]"]').val() ) + 1 ) +'" class="field_'+$.fn.listable.counter+'" >\ |
|
547 | 536 | if (settings.depth) { |
548 | 537 | $(settings.variable_vault).append('<input type="hidden" name="depth[]" value="0" class="field_'+$.fn.listable.counter+'" >'); |
549 | 538 | } |
550 | | - $.fn.listable.counter ++; |
551 | | - this.refresh(); |
| 539 | + |
| 540 | + $.fn.listable.counter ++; |
552 | 541 | } |
553 | | - this.element.find('.form_field').each(function(index) { |
554 | | - settings.variable_vault.find('input.'+$(this).attr('class').replace(/form_field /,'').replace(/ depth_\d/, '')+'[name=order\\[\\]]').val(index); |
555 | | - }); |
| 542 | + |
| 543 | + // Update visual |
| 544 | + this.refresh(); |
| 545 | + |
| 546 | + this.element.find('.form_field').each(function(index) { |
| 547 | + settings.variable_vault.find('input.'+$(this).attr('class').replace(/form_field /,'').replace(/ depth_\d/, '')+'[name=order\\[\\]]').val(index); |
| 548 | + }); |
| 549 | + |
| 550 | + // Call after_save callback if able |
556 | 551 | if ($.isFunction(settings.after_save)) { |
557 | 552 | settings.after_save(itemType); |
558 | 553 | } |
559 | 554 |
|
560 | | - // Make sure all elements are in order |
561 | | - this.update_order(); |
562 | | - |
| 555 | + // Clear form |
| 556 | + $('#'+itemType.formid)[0].reset(); // Clear the form when it is closed so data from editing doesnt show when adding a new field |
| 557 | + |
| 558 | + // Make sure all elements are in order |
| 559 | + this.update_order(); |
| 560 | + |
563 | 561 | return this; |
564 | 562 | }, |
565 | 563 |
|
566 | 564 | refresh: function() { |
567 | | - var settings = this.options; |
| 565 | + var settings = this.options; |
568 | 566 | var that = this; |
569 | 567 |
|
570 | 568 | // Load up types and their corresponding "type" |
|
682 | 680 | that.current_divider = temp_current_divder |
683 | 681 | } |
684 | 682 | $.fn.listable.counter ++; // So that the counter is one more than the total number of elements |
| 683 | + return this; |
685 | 684 | }, |
686 | 685 | transfer: function(event, ui) { |
687 | 686 | var settings = this.options; |
|
0 commit comments