Created
September 6, 2013 00:47
-
-
Save merk/6458159 to your computer and use it in GitHub Desktop.
Symfony2 form collection javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file is part of the InfiniteFormBundle package. | |
* | |
* (c) Infinite Networks Pty Ltd <http://infinite.net.au> | |
*/ | |
/** | |
* Provides helper javascript for handling adding and removing | |
* items from a form collection. | |
* | |
* @author Tim Nagel <[email protected]> | |
*/ | |
$(function ($) { | |
"use strict"; | |
window.infinite = window.infinite || {}; | |
window.infinite.Collection = function (collection, prototypes) { | |
this.$collection = $(collection); | |
this.internalCount = $(collection).count(); | |
var $prototypes = []; | |
$.each(prototypes, function() { $prototypes[prototypes.length] = $(prototype); }); | |
this.$prototypes = $prototypes; | |
}; | |
window.infinite.Collection.prototype = { | |
/** | |
* Stores the count of items in the collection | |
*/ | |
internalCount: 0, | |
/** | |
* Sets up the collection and its prototypes for action. | |
*/ | |
initialise: function () { | |
var that = this; | |
this.$prototypes.on('click', function (e) { | |
e.preventDefault(); | |
var $prototype = $(this); | |
if ($prototype.is('.disabled')) { | |
return; | |
} | |
that.addToCollection($prototype); | |
}); | |
this.$collection.on('click', '.remove_item', function (e) { | |
e.preventDefault(); | |
var $button = $(this); | |
if ($button.is('.disabled')) { // for browsers that don't respect pointer-events | |
return; | |
} | |
that.removeFromCollection($button.find('.item')); | |
}); | |
}, | |
/** | |
* Adds another row to the collection | |
*/ | |
addToCollection: function (prototype, values) { | |
values = values || {}; | |
var row = $(this._getPrototypeHtml(prototype)); | |
this._fillRowWithValues(row, values); | |
var event = $.Event('infinite_collection_add'); | |
event.collection = this.$collection; | |
event.row = row; | |
event.insertBefore = null; | |
this.$collection.trigger(event); | |
if (!event.isDefaultPrevented()) { | |
if (event.insertBefore) { | |
row.insertBefore(event.insertBefore); | |
} else { | |
this.$collection.append(row); | |
} | |
return row; | |
} | |
return false; | |
}, | |
/** | |
* Removes a supplied row from the collection. | |
*/ | |
removeFromCollection: function (row) { | |
var event = jQuery.Event('infinite_collection_remove'); | |
event.collection = this.$collection; | |
event.row = row; | |
this.$collection.trigger(event); | |
if (!event.isDefaultPrevented()) { | |
row.remove(); | |
} | |
}, | |
/** | |
* Retrieves the HTML from the prototype button, replacing __name__ with | |
* an incremented counter value. | |
* | |
* @private | |
*/ | |
_getPrototypeHtml: function (prototype) { | |
var html = prototype.data('prototype'); | |
return html.replace(/__name__/gi, this.internalCount++); | |
}, | |
/** | |
* Fills a given row with default values. | |
* | |
* @private | |
*/ | |
_fillRowWithValues: function (row, values) { | |
$.each(values, function (field, value) { | |
var el = row.find(field); | |
if (el.is('input, textarea, select')) { | |
el.val(value); | |
} else { | |
el.text(value); | |
} | |
if (value && !el.is(':visible') && !el.is('.stay-hidden')) { | |
el.show(); | |
} | |
}); | |
} | |
}; | |
}(window.jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment