/** * @requires $ jQuery * @requires _ UnderscoreJS */ (function (/* Object */ ns) { "use strict"; ns.Table = function (options) { qs.CustomForm.apply(this, arguments); this.itemName = options.itemName; this.sortable = options.sortable; this.removable = options.removable; this.numerable = options.numerable; this.setName(options.name); this.get('add').on('click', _.bind(this.addNewButtonOnClick, this)); if (this.sortable) { this.get('moveUp', this.get('items')).on('click', _.bind(this.moveUpOnClick, this)); this.get('moveDown', this.get('items')).on('click', _.bind(this.moveDownOnClick, this)); } if (this.removable) { this.get('delete', this.get('items')).on('click', _.bind(this.deleteOnClick, this)); } this.notifyDatasetChanged(); }; ns.Table.prototype = Object.create(qs.CustomForm.prototype); var fn = ns.Table.prototype; fn.setName = function (name) { this._name = name; }; fn.getName = function () { return this._name; }; fn.addNewButtonOnClick = function (event) { var options = { formPartAction: 'new' }; Qs_Form.updatePart(event.currentTarget.form, this.getName(), options); }; fn.moveUpOnClick = function (event) { var element = $(event.currentTarget); var item = element.parents('[data-collection-item]:first'); if (element.is(':enabled')) { item.insertBefore(item.prev('[data-collection-item]')); this.notifyDatasetChanged(); } }; fn.moveDownOnClick = function (event) { var element = $(event.currentTarget); var item = element.parents('[data-collection-item]:first'); if (element.is(':enabled')) { item.insertAfter(item.next('[data-collection-item]')); this.notifyDatasetChanged(); } }; fn.deleteOnClick = function (event) { var row = $(event.currentTarget).parents('[data-collection-item]:first'); row.addClass('remove'); if (!confirm('Do you really want to delete this ' + this.itemName + '?')) { row.removeClass('remove'); return false; } var itemsCount = this.node.find('>tbody>[data-collection-item]').length; if (1 == itemsCount) { var btnAdd = this.get('add'); var elementContainer = $('