/** * @required $ jQuery */ (function (ns) { ns.List = function (options) { this.construct.apply(this, arguments); }; var fn = ns.List.prototype; fn.construct = function (options) { this.options = $.extend({ containerId: undefined, requestUrl: undefined, update: 'row', summaryTable: undefined }, options); this.container = $('#' + this.options.containerId); // '[data-force-status] > select' this.container.bind('change.reportList', qs.proxy(this.onForcedStatusChange, this)); this.initRejectLinks(); if (this.options.summaryTable) { this.initSummaryTable(); } }; fn.initRejectLinks = function () { // 'a[data-report-reject]' this.container.bind('click.reportList', qs.proxy(this.onRejectFileClick, this)); }; fn.onRejectFileClick = function (e) { var target = $(e.target); if (!target.is('a[data-report-reject=""]')) { return; } e.preventDefault(); var requestUrl = target.attr('data-url'); target.attr('disabled', 'disabled'); $.ajax({ url: requestUrl, data: { action: 'rejectFileForm' }, type: 'post', dataType: 'json', success: qs.proxy(this.onRejectFileFormDone, this, target), error: qs.proxy(this.onRejectFileFormFail, this, target) }); }; fn.onRejectFileFormDone = function (btn, response) { var that = this; $.fancybox({content: response.html, onComplete: function () { var popup = $('#fancybox-content'); $('[data-action="reject"]', popup).bind('click', qs.proxy(that.onRejectFormApply, that, btn)); $('[data-action="cancel"]', popup).bind('click', $.fancybox.close); }}); btn.removeAttr('disabled'); }; fn.onRejectFileFormFail = function (btn, response) { btn.removeAttr('disabled'); alert("Can't reject Report File"); }; fn.onRejectFormApply = function (btn, e) { e.preventDefault(); var target = $(e.target); var form = target.closest('form'); var requestUrl = btn.attr('data-url'); var request = {}; var values = form.serializeArray(); var i, c; for (i = 0, c = values.length; i < c; ++i) { request[values[i].name] = values[i].value; } target.attr('disabled', 'disabled'); request.action = 'rejectFile'; $.ajax({ url: requestUrl, data: request, type: 'post', dataType: 'json', success: qs.proxy(this.onRejectFileDone, this, btn), error: qs.proxy(this.onRejectFileFail, this, btn) }); }; fn.onRejectFileDone = function (btn, response) { $.fancybox.close(); var fileId = btn.attr('data-file-id'); var selector = '[data-report=""][data-file-id="' + fileId + '"]'; var parentType = 'tr'; if ('cell' === this.options.update) { parentType = 'td'; } if (response && response.html) { var newBlock = $(response.html).find(selector).closest(parentType); if (newBlock.length) { btn.closest(parentType).replaceWith(newBlock); } else { location.reload(); } } }; fn.onRejectFileFail = function (btn, response) { $.fancybox.close(); alert("Can't reject Report File"); }; fn.onForcedStatusChange = function (e) { var target = $(e.target); if (!target.is('[data-force-status=""] > select')) { return; } var block = target.closest('div'); var reportId = block.attr('data-report-id'); var schoolId = block.attr('data-school-id'); var statusId = target.val(); target.attr('disabled', 'disabled'); if (1 != block.length) { return; } $.ajax({ url: this.options.requestUrl, data: { action: 'forceStatus', reportId: reportId, schoolId: schoolId, statusId: statusId }, type: 'post', dataType: 'json', success: qs.proxy(this.onForceStatusDone, this, block), error: qs.proxy(this.onForceStatusFail, this, block) }); }; fn.onForceStatusDone = function (block, response) { var reportId = block.attr('data-report-id'); var schoolId = block.attr('data-school-id'); var selector = 'div.reportId' + reportId + '_schoolId' + schoolId + ':first'; var parentType = 'tr'; if ('cell' === this.options.update) { parentType = 'td'; } if (response && response.html) { block.closest(parentType).replaceWith($(response.html).find(selector).closest(parentType)); } }; fn.onForceStatusFail = function (block, response) { block.find('select').removeAttr('disabled'); alert("Can't update Report status"); }; fn.initSummaryTable = function () { var table = $(this.options.summaryTable); table.stickyTableHeaders({cacheHeaderHeight: true}); }; })(qs.defineNS('app.report.admin'));