/** * @requires $ jQuery * @requires _ UnderscoreJS */ (function (/* Object */ ns) { "use strict"; ns.Table = function (options) { qs.FormNode.apply(this, arguments); this.itemName = options.itemName; this.sortable = options.sortable; this.removable = options.removable; this.numerable = options.numerable; this.startIndex = options.startIndex; this.maxItems = options.maxItems; 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.FormNode.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('removing'); if (!confirm('Do you really want to delete this ' + this.itemName + '?')) { row.removeClass('removing'); return false; } var itemsCount = this.node.find('>tbody>tr').length; if (1 == itemsCount) { var btnAdd = this.get('add'); var elementContainer = $('
').attr('id', btnAdd.parents('td').attr('id')); elementContainer.append(btnAdd); this.node.get(0).parentNode.insertBefore( elementContainer.get(0), this.node.get(0) ); this.node.remove(); } else { row.remove(); } this.notifyDatasetChanged(); }; fn.notifyDatasetChanged = function () { var items = this.get('items'); this.initOptionsColumn(items); if (this.numerable && this.sortable) { var index = this.startIndex + 1; this.get('rowNumber', items).each(function () { $(this).text(index++); }); } this.node.toggleClass('table-empty', items.length == 0); this.get('add').prop('disabled', this.maxItems && items.length >= this.maxItems); }; fn.initOptionsColumn = function (items) { if (!this.sortable) { return; } var length = items.length; if (length === 1) { this.get('moveUp, moveDown', items.first()).prop('disabled', true); } else if (length >= 2) { this.get('moveUp', items.first()).prop('disabled', true); this.get('moveDown', items.first()).prop('disabled', false); this.get('moveUp', items.last()).prop('disabled', false); this.get('moveDown', items.last()).prop('disabled', true); if (length > 2) { this.get('moveUp, moveDown', items.slice(1, -1)).filter(':disabled').prop('disabled', false); } } }; })(qs.defineNS('qs.form.subform'));