Skip to content

Commit ebdbc12

Browse files
committed
Merge pull request #3 from HighgateCross/master
Grunt, Cleanup, Documentation and Vars object for save function
2 parents 432c268 + 7395d4b commit ebdbc12

File tree

11 files changed

+1131
-115
lines changed

11 files changed

+1131
-115
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
*.swp
1+
*.sw[poq]
22
.DS_Store
3+
node_modules

Gruntfile.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//Wrapper function with one parameter
2+
module.exports = function(grunt) {
3+
grunt.initConfig({
4+
// pkg is used from templates and therefore
5+
// MUST be defined inside the initConfig object
6+
pkg : grunt.file.readJSON('package.json'),
7+
8+
meta: {
9+
banner: '/*! <%= pkg.name %> v<%= pkg.version %> - ' +
10+
'<%= grunt.template.today("yyyy-mm-dd") %> \n' +
11+
' * Author: <%= pkg.author %> \n' +
12+
' * License: <%= pkg.license %>\n' +
13+
' */\n\n'
14+
},
15+
// jshint config
16+
jshint: {
17+
options: {
18+
eqeqeq: true,
19+
trailing: true
20+
},
21+
target: {
22+
src: ['src/**/*.js']
23+
}
24+
},
25+
// Concat config
26+
concat: {
27+
options: {
28+
separator: '\n\n'
29+
},
30+
dist: {
31+
options: {
32+
banner: '<%= meta.banner %>'
33+
},
34+
src: [
35+
'src/**/*.js'
36+
],
37+
dest: 'dist/jquery.listable.js'
38+
}
39+
},
40+
copy: {
41+
css: {
42+
expand: true,
43+
cwd: 'src/',
44+
src: '**/*.css',
45+
dest: 'dist/',
46+
flattern: true,
47+
filter: 'isFile'
48+
}
49+
},
50+
uglify: {
51+
options: {
52+
banner: '<%= meta.banner %>'
53+
},
54+
dist: {
55+
files: {
56+
'dist/jquery.listable.min.js': ['dist/jquery.listable.js']
57+
}
58+
}
59+
}
60+
});
61+
grunt.loadNpmTasks('grunt-contrib-jshint');
62+
grunt.loadNpmTasks('grunt-contrib-concat');
63+
grunt.loadNpmTasks('grunt-contrib-uglify');
64+
grunt.loadNpmTasks('grunt-contrib-copy');
65+
grunt.registerTask('default', ['concat', 'copy', 'uglify']);
66+
grunt.registerTask('test', ['jshint']);
67+
};
Lines changed: 102 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
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
54
*/
65

7-
86
;(function ( $, window, document, undefined ) {
97

108
var update = false; // Whether the current form is an update or not
@@ -33,9 +31,9 @@
3331
'form_vault' : '#listable-form-vault',
3432
'gear_image' : '',
3533
'gear_transition' : 'fade',
36-
'image_dragging' : false,
34+
'image_dragging' : true,
3735
'initial_add' : true,
38-
'max_depth' : false,
36+
'max_depth' : null,
3937
'shallower_image' : '/javascripts/listable/images/left_arrow.png',
4038
'types' : [],
4139
// Callbacks
@@ -135,7 +133,7 @@
135133
$(settings.form_vault).prepend('<div id="msg-listable"></div>');
136134
}
137135

138-
if ( settings.image_dragging ) {
136+
if ( ! settings.image_dragging ) {
139137
this.element.find('img').live('dragstart', function(event) { event.preventDefault(); });
140138
}
141139

@@ -224,12 +222,12 @@
224222
if (settings.gear_image) { // If the delete setting is set to true then enable the delete button
225223
this.element.find('.listable_item_buttons').hide(0);
226224
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+
}
233231
});
234232
}
235233
if (settings.delete) { // If the delete setting is set to true then enable the delete button
@@ -427,108 +425,99 @@
427425

428426
_setOption: function( key, value ) {
429427
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;
435433
}
436434

437435
$.Widget.prototype._setOption.apply( this, arguments );
438436
},
439437

438+
/*
439+
* Save
440+
* Takes the listable item type and creates / updates the listable.
441+
*
442+
*/
440443

441444
save: function( itemType, options1, options2 ) {
445+
// Universal variables
442446
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;
451466
}
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;
454470
}
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
462473
if ($.isFunction(settings.beforeSave)) {
463474
settings.beforeSave(itemType, vars);
464475
}
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"
468476

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"
475483

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+
}
482490

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+
});
489497

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+
});
500508
}
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"
504509

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
508514
} 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
510516
}
517+
});
511518

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) {
532521
if (settings.add_after) {
533522
// Add order with 1 plus the current dividers order
534523
$(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,24 +536,33 @@
547536
if (settings.depth) {
548537
$(settings.variable_vault).append('<input type="hidden" name="depth[]" value="0" class="field_'+$.fn.listable.counter+'" >');
549538
}
550-
$.fn.listable.counter ++;
551-
this.refresh();
539+
540+
$.fn.listable.counter ++;
552541
}
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
556551
if ($.isFunction(settings.after_save)) {
557552
settings.after_save(itemType);
558553
}
559554

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+
563561
return this;
564562
},
565563

566564
refresh: function() {
567-
var settings = this.options;
565+
var settings = this.options;
568566
var that = this;
569567

570568
// Load up types and their corresponding "type"
@@ -682,6 +680,7 @@
682680
that.current_divider = temp_current_divder
683681
}
684682
$.fn.listable.counter ++; // So that the counter is one more than the total number of elements
683+
return this;
685684
},
686685
transfer: function(event, ui) {
687686
var settings = this.options;

0 commit comments

Comments
 (0)