js/src/fields/controls/subarea/ModuleView.js
var tplModuleView = require('fields/controls/subarea/templates/module-view.hbs');
var Ajax = require('common/Ajax');
var Config = require('common/Config');
var TinyMCE = require('common/TinyMCE');
var Payload = require('common/Payload');
var FullscreenView = require('backend/Views/FullscreenView');
var UI = require('common/UI');
module.exports = Backbone.View.extend({
events: {
'click .kbms-action--open': 'openForm',
'click .kbms-action--delete': 'removeModule',
'click .kbms-action--update': 'saveModule'
},
tagName: 'div',
className: 'kb-submodule',
initialize: function (options) {
this.formLoaded = false;
this.slotView = options.slotView;
this.controller = options.slotView.controller;
this.ModuleModel = options.ModuleModel;
this.parentModel = options.parentModel;
},
draft: function () {
},
render: function () {
var that = this;
this.$el.append(tplModuleView({module: this.ModuleModel.toJSON()}));
this.slotView.$container.prepend(this.$el);
this.$el.attr('data-kba-mid', this.ModuleModel.get('mid'));
_.defer(function () {
that.setupElements();
});
},
setupElements: function () {
this.$inner = this.$('.kbsm-inner');
},
openForm: function () {
if (KB.EditModalModules) {
this.handleFrontend();
} else {
this.handleBackend();
}
},
handleBackend: function () {
if (this.formLoaded) {
this.open();
} else {
var data = {
action: 'getModuleBackendForm',
_ajax_nonce: Config.getNonce('read'),
module: this.ModuleModel.toJSON()
};
Ajax.send(data, this.success, this);
}
},
handleFrontend: function () {
var model = KB.Modules.get(this.model.get('mid'));
console.log(model);
KB.EditModalModules.openView(model.View, false, true);
},
success: function (res) {
this.$inner.hide().append(res.data.html);
var model = KB.ObjectProxy.add(KB.Modules.add(this.ModuleModel.toJSON()));
_.defer(function () {
Payload.parseAdditionalJSON(res.data.json);
});
this.formLoaded = true;
TinyMCE.addEditor(this.$el);
this.ModuleModel = model;
this.open();
},
open: function () {
if (!Config.get('frontend')) {
if (!this.fsControl) {
this.fsControl = new FullscreenView({model: this.ModuleModel});
}
this.fsControl.open();
}
this.listenToOnce(this.fsControl, 'close', this.getDirty);
UI.repaint(this.fsControl.$el);
},
saveModule: function () {
if (!this.formLoaded){
return false;
}
this.ModuleModel.sync();
this.getClean();
},
getDirty: function () {
this.$el.addClass('is-dirty');
},
getClean: function () {
this.$el.removeClass('is-dirty');
},
removeModule: function (e) {
this.slotView.removeModuleView(e);
}
});