symfony-collection-js
Advanced tools
Comparing version 2.1.0 to 3.0.0
{ | ||
"name": "symfony-collection-js", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "A jquery plugin to dynamically create elements of a symfony form collection.", | ||
@@ -5,0 +5,0 @@ "main": "symfonyCollectionJs.js", |
@@ -41,4 +41,4 @@ # symfonyCollectionJs | ||
call_post_add_on_init: false, | ||
post_add: function($new_elem, is_init) { return true; }, | ||
post_delete: function($delete_elem) { return true; }, | ||
post_add: function($new_elem, context) { return true; }, | ||
post_delete: function($delete_elem, context) { return true; }, | ||
post_up: function($elem, $switched_elem) { return true; }, | ||
@@ -66,8 +66,8 @@ post_down: function($elem, $switched_elem) { return true; }, | ||
Remove an element with its index (starting from 0): | ||
Delete an element with its index (starting from 0): | ||
~~~~ | ||
$('.collection').formCollection('remove', 2); | ||
$('.collection').formCollection('delete', 2); | ||
~~~~ | ||
Clear every element: | ||
Clear every element (doesn't call post_delete): | ||
~~~~ | ||
@@ -77,2 +77,24 @@ $('.collection').formCollection('clear'); | ||
The context argument in post_add and post_delete will have of this values, depending on how it's called: | ||
~~~~ | ||
$.fn.formCollection.POST_ADD_CONTEXT = { | ||
BTN_ADD: 4, | ||
OTHER_BTN_ADD: 8, | ||
INIT: 15, | ||
ADD_METHOD: 16 | ||
}; | ||
$.fn.formCollection.POST_DELETE_CONTEXT = { | ||
BTN_DELETE: 23, | ||
DELETE_METHOD: 42 | ||
}; | ||
~~~~ | ||
# how to start the tests | ||
Just open the page index.html in the test folder. It starts the tests on opening. | ||
# any advanced example ? | ||
Yes ! in example/triply_nested_collection.html | ||
# Requirement | ||
@@ -79,0 +101,0 @@ |
@@ -31,3 +31,6 @@ // Uses CommonJS, AMD or browser globals to create a jQuery plugin. | ||
var selector = this.selector; | ||
var eventPrototypeModified = 'prototypeModified'; | ||
var eventPrototypeModified = 'prototypeModified'; | ||
var eventAddMethodCalled = 'addMethodCalled'; | ||
var eventDeleteMethodCalled = 'deleteMethodCalled'; | ||
var eventClearMethodCalled = 'clearMethodCalled'; | ||
@@ -38,6 +41,6 @@ if (options === undefined || typeof options === 'object') { | ||
call_post_add_on_init: false, | ||
post_add: function($new_elem, is_init) { | ||
post_add: function($new_elem, context) { | ||
return true; | ||
}, | ||
post_delete: function($delete_elem) { | ||
post_delete: function($delete_elem, context) { | ||
return true; | ||
@@ -59,22 +62,30 @@ }, | ||
settings = $.extend(true, {}, defaults, options); | ||
if (typeof globalSettings == 'undefined') | ||
globalSettings = {}; | ||
globalSettings[selector] = settings; // to make them accessible on another call | ||
} else if (typeof options === 'string') { | ||
if ($.inArray(options, ['add', 'remove', 'clear']) === -1) { | ||
if ($.inArray(options, ['add', 'delete', 'clear']) === -1) { | ||
console.log('Invalid options'); | ||
return false; | ||
} else if (options === 'remove' && param === undefined) { | ||
} else if (options === 'delete' && param === undefined) { | ||
console.log('Missing index'); | ||
return false; | ||
} else if (typeof globalSettings == 'undefined' || globalSettings[selector] === undefined) { | ||
console.log('Element not initialized'); | ||
return false; | ||
} | ||
var settings = globalSettings[selector]; | ||
} | ||
return $(this).each(function() { | ||
var $collection_root = $(this); | ||
switch (options) { // particular case, so it's better to have it at the top and forget it | ||
case 'add': | ||
$collection_root.trigger(eventAddMethodCalled); | ||
return; | ||
break; | ||
case 'delete': | ||
$collection_root.trigger(eventDeleteMethodCalled, [param]); | ||
return; | ||
break; | ||
case 'clear': | ||
$collection_root.trigger(eventClearMethodCalled); | ||
return; | ||
break; | ||
} | ||
var prototype = $(this).data('prototype'); | ||
var n = $(this).children().length; | ||
var $collection_root = $(this); | ||
var needed_data_for_update = []; | ||
@@ -92,2 +103,21 @@ | ||
/* | ||
* the listener way below is a trick to allow a second call to formCollection to access the same | ||
* data (elements, options) | ||
*/ | ||
$collection_root.on(eventAddMethodCalled, function(e){ | ||
add_elem_bottom($.fn.formCollection.POST_ADD_CONTEXT.ADD_METHOD); | ||
}); | ||
$collection_root.on(eventDeleteMethodCalled, function(em, param){ | ||
$elem = $collection_root.children().eq(param); | ||
delete_elem($elem); | ||
settings.post_delete($elem, $.fn.formCollection.POST_DELETE_CONTEXT.DELETE_METHOD); | ||
}); | ||
$collection_root.on(eventClearMethodCalled, function(e){ | ||
$collection_root.empty(); | ||
n = 0; | ||
}); | ||
var build_node_needed_data_for_update = function(path, attributes) { | ||
@@ -146,7 +176,7 @@ var obj = { | ||
var $new_elem = add_elem_down($elem); | ||
settings.post_add($new_elem, false); | ||
settings.post_add($new_elem, $.fn.formCollection.POST_ADD_CONTEXT.BTN_ADD); | ||
}); | ||
$elem.find(settings.btn_delete_selector).click(function() { | ||
delete_elem($elem); | ||
settings.post_delete($elem); | ||
settings.post_delete($elem, $.fn.formCollection.POST_DELETE_CONTEXT.BTN_DELETE); | ||
}); | ||
@@ -224,3 +254,3 @@ $elem.find(settings.btn_up_selector).click(function() { | ||
var add_elem_bottom = function() { | ||
var add_elem_bottom = function(context) { | ||
if (n >= settings.max_elems) | ||
@@ -231,3 +261,3 @@ return false; | ||
n++; | ||
settings.post_add($new_elem, false); | ||
settings.post_add($new_elem, context); | ||
}; | ||
@@ -266,48 +296,44 @@ | ||
switch (options) { | ||
case 'add': | ||
add_elem_bottom(); | ||
break; | ||
case 'clear': | ||
$collection_root.empty(); | ||
n = 0; | ||
break; | ||
case 'remove': | ||
$collection_root.children().eq(param).remove(); | ||
n--; | ||
update_indexes_from(param); | ||
break; | ||
default: | ||
var init_existing = function() { | ||
/* | ||
* the existing nodes have no placeholders. but we need it to update it. | ||
* we need a model to save the attributes formats with placeholdes in the target element | ||
*/ | ||
var $modelElement = $(prototype); | ||
$collection_root.children().each(function() { | ||
init_elem_listeners($(this)); | ||
if (settings.call_post_add_on_init) | ||
settings.post_add($(this), $.fn.formCollection.POST_ADD_CONTEXT.INIT); | ||
}); | ||
}; | ||
var init_existing = function() { | ||
/* | ||
* the existing nodes have no placeholders. but we need it to update it. | ||
* we need a model to save the attributes formats with placeholdes in the target element | ||
*/ | ||
var $modelElement = $(prototype); | ||
$collection_root.children().each(function() { | ||
init_elem_listeners($(this)); | ||
if (settings.call_post_add_on_init) | ||
settings.post_add($(this), true); | ||
}); | ||
}; | ||
init_existing(); | ||
init_needed_data_for_update(); | ||
if (settings.other_btn_add) { | ||
if (typeof settings.other_btn_add === 'string') | ||
var $otherBtnAdd = $(settings.other_btn_add) | ||
else if (settings.other_btn_add instanceof jQuery) | ||
var $otherBtnAdd = settings.other_btn_add; | ||
else { | ||
console.log('other_btn_add: bad value, can be a selector or a jQuery object.') | ||
break; | ||
} | ||
$otherBtnAdd.click(function() { | ||
add_elem_bottom(); | ||
}); | ||
} | ||
init_existing(); | ||
init_needed_data_for_update(); | ||
if (settings.other_btn_add) { | ||
var $otherBtnAdd = null; | ||
if (typeof settings.other_btn_add === 'string') | ||
$otherBtnAdd = $(settings.other_btn_add) | ||
else if (settings.other_btn_add instanceof jQuery) | ||
$otherBtnAdd = settings.other_btn_add; | ||
else { | ||
console.log('other_btn_add: bad value, can be a selector or a jQuery object.') | ||
} | ||
if ($otherBtnAdd) { | ||
$otherBtnAdd.click(function() { | ||
add_elem_bottom($.fn.formCollection.POST_ADD_CONTEXT.OTHER_BTN_ADD); | ||
}); | ||
} | ||
} | ||
}); | ||
}; | ||
$.fn.formCollection.POST_ADD_CONTEXT = { | ||
BTN_ADD: 4, | ||
OTHER_BTN_ADD: 8, | ||
INIT: 15, | ||
ADD_METHOD: 16 | ||
}; | ||
$.fn.formCollection.POST_DELETE_CONTEXT = { | ||
BTN_DELETE: 23, | ||
DELETE_METHOD: 42 | ||
}; | ||
})); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
95774
9
1083
105
1