/** * @requires $ jQuery * @requires _ Underscore */ (function (/* Object */ ns) { 'use strict'; ns.QuickEdit = function () {return this.construct.apply(this, arguments)}; var fn = ns.QuickEdit.prototype; fn.construct = function (options) { this.ns = 'cqe'; this.options = { submitDelay: 1000 }; this.timer = null; $.extend(true, this.options, options); this.links = $(options.linkSelector).on('mouseenter.' + this.ns, _.bind(this.linkOnMouseEnter, this)); }; fn.linkOnMouseEnter = function (event) { if (!event.ctrlKey) { return; } var link = event.currentTarget; qs.ajax($(link).attr('href'), {}, {type: 'get'}).done(_.bind(this.onLinkContentLoad, this, link)); }; fn.onLinkContentLoad = function (link, response) { var self = this; $(link).qtip({ content: {text: response.content}, position: { my: 'center left', at: 'center right' }, show: { ready: true }, style: { classes: 'qtip-cms-quick-edit' }, hide: { fixed: true, delay: 300 }, events: { show: function (event, api) { var form = $('form', event.currentTarget); self.initForm(form, api); }, hide: function (event, api) { var form = $(event.target).find('form'); if (form.data('changed')) { self.submitForm(form.get(0), api, function () { api.destroy(true); }); } else { api.destroy(true); } }, render: function(event, api) { $('.qtip').qtip('hide'); } } }); }; fn.initForm = function (form, api) { form.find('input:checkbox').on('change.' + this.ns, function (event) { api.tooltip.find('.message-box').text('please wait ...').removeClass('mb-updated').addClass('mb-changed').show(); $(event.currentTarget.form).trigger('submit'); }); form.on('submit.' + this.ns, _.bind(this.onFormSubmit, this, api)); }; fn.onFormSubmit = function (api, event) { var form = event.currentTarget; $('form').data('changed', 1); if (this.timer) { clearTimeout(this.timer); } this.timer = setTimeout(_.bind(this.submitForm, this, form, api), this.options.submitDelay); return false; }; fn.submitForm = function (form, api, callback) { var data = Qs_Form.toObject(form); var self = this; var messageBox = api.tooltip.find('.message-box'); qs.ajax(form.action, data).done(function (response) { messageBox.text('updated').removeClass('mb-changed').addClass('mb-updated'); messageBox.show(300); setTimeout(_.bind(messageBox.fadeOut, messageBox, 300), 1000); if (callback) { qs.call(callback); } }); } })(qs.defineNS('app.cms'));