Skip to content

Instantly share code, notes, and snippets.

@ZhukV
Last active December 18, 2015 00:39
Show Gist options
  • Save ZhukV/5698410 to your computer and use it in GitHub Desktop.
Save ZhukV/5698410 to your computer and use it in GitHub Desktop.
;(function ($) {
/**
* Event for collection
*/
function CollectionEvent(element)
{
var status = true;
/**
* Get element
*/
this.getElement = function() {
return element;
};
this.disable = function() {
status = false;
};
this.enable = function() {
status = true;
};
this.isEnabled = function() {
return status;
};
}
/**
* Collection system
*
* @param container
*/
function Collection(container)
{
var _init = false,
eventDispatcher = new EventDispatcher;
/**
* Get event dispatcher
*/
this.getEventDispatcher = function()
{
return eventDispatcher;
};
/**
* Add event to dispatcher
*/
this.addEvent = eventDispatcher.addListener;
this.callEvent = function(name, event)
{
eventDispatcher.dispatch(name, this, [event]);
};
/**
* Is initialize collection
*/
this.isInit = function()
{
return _init;
};
/**
* Get container
*/
this.getContainer = function()
{
return container;
};
/**
* Initialize collection
*/
this._initialize();
_init = true;
}
/**
* Initialize
*/
Collection.prototype._initialize = function()
{
if (!this.isInit()) {
this.setEventAddItem(this.getItemsForSetEventAddItem(this.getContainer()));
this.setEventRemoveItem(this.getItemsForSetEventRemoveItem(this.getContainer()));
// Remove element
this.addEvent('postAddElement', function(event){
this.setEventRemoveItem(this.getItemsForSetEventRemoveItem(event.getElement()));
});
// Holder image
this.addEvent('postAddElement', function(event){
event.getElement().find('img[data-holder-src]').each(function(){
$(this).attr('data-src', $(this).attr('data-holder-src'));
Holder.run({
image: this
});
});
});
}
};
/**
* Get prototype
*
* @return string|null
*/
Collection.prototype.getDataPrototype = function()
{
return this.getContainer().find('[data-prototype]').attr('data-prototype');
};
/**
* Get collection html element
*/
Collection.prototype.getCollectionStorage = function()
{
return this.getContainer().find('.collections:first');
};
/**
* Get collection element
*/
Collection.prototype.getCollectionElement = function(index)
{
if (index == 'last') {
return this.getCollectionStorage().find('>:last');
} else if(index == 'first') {
return this.getCollectionStorage().find('>:first');
} else if (typeof index == 'integer') {
return this.getCollectionStorage().find('>:eq(' + index + ')');
}
throw new Error('Undefined index type.');
};
/**
* Get collection elements
*/
Collection.prototype.getCollectionElements = function()
{
return this.getCollectionStorage().find('> *');
};
/**
* Get count items in collection
*/
Collection.prototype.count = function()
{
return this.getCollectionStorage().find('>*').size();
};
/**
* Alias: $.find(selector)
*
* @param selector
* @returns {*}
*/
Collection.prototype.find = function(selector)
{
return this.getCollectionStorage().find(selector);
};
/**
* Alias: $.each(callback)
*
* @param callback
* @returns {*}
*/
Collection.prototype.each = function(callback)
{
return this.getCollectionElements().each(callback);
};
/**
* Add item to collection
*
* @return boolean
*/
Collection.prototype.addItemToCollection = function()
{
var event = new CollectionEvent(null);
this.callEvent('preAddElement', event);
if (event.isEnabled()) {
this.getCollectionStorage().append(this.getDataPrototype().replace(/__name__/g, this.count()));
this.callEvent('postAddElement', new CollectionEvent(this.getCollectionElement('last')));
}
return false;
};
/**
* Remove item from collection
*/
Collection.prototype.removeItemFromCollection = function(target)
{
var parents = target.parents('.collection-item');
if (parents.length) {
var event = new CollectionEvent(parents.get(0));
this.callEvent('preRemoveElement', event);
if (event.isEnabled()) {
$(parents.get(0)).remove();
} else {
return false;
}
}
this.callEvent('postRemoveElement', new CollectionEvent(null));
return false;
};
/**
* Add event click add item to collection
*/
Collection.prototype.setEventAddItem = function(a)
{
a.click($.proxy(this, 'addItemToCollection'));
};
/**
* Add event click remove item from collection
*/
Collection.prototype.setEventRemoveItem = function(a)
{
var $this = this;
a.click(function(){
return $this.removeItemFromCollection.call($this, $(this));
});
};
/**
* Get items for set event at add item to collection
*
* @return array
*/
Collection.prototype.getItemsForSetEventAddItem = function(e)
{
return e.find('a[href="#add-item"], a.add-item');
};
/**
* Get items for set event at remove element from collection
*
* @return array
*/
Collection.prototype.getItemsForSetEventRemoveItem = function(e)
{
return e.find('a[href="#remove-item"], a.remove-item');
};
window.Collection = Collection;
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment