| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- MODx.Component = function(config) {
- config = config || {};
- MODx.Component.superclass.constructor.call(this,config);
- this.config = config;
- this._loadForm();
- if (this.config.tabs) {
- this._loadTabs();
- }
- this._loadComponents();
- this._loadActionButtons();
- MODx.activePage = this;
- };
- Ext.extend(MODx.Component,Ext.Component,{
- fields: {}
- ,form: null
- ,action: false
- ,_loadForm: function() {
- if (!this.config.form) { return false; }
- this.form = new Ext.form.BasicForm(Ext.get(this.config.form),{ errorReader : MODx.util.JSONReader });
- if (this.config.fields) {
- for (var i in this.config.fields) {
- if (this.config.fields.hasOwnProperty(i)) {
- var f = this.config.fields[i];
- if (f.xtype) {
- f = Ext.ComponentMgr.create(f);
- }
- this.fields[i] = f;
- this.form.add(f);
- }
- }
- }
- return this.form.render();
- }
- ,_loadActionButtons: function() {
- if (!this.config.buttons) { return false; }
- this.ab = MODx.load({
- xtype: 'modx-actionbuttons'
- ,form: this.form || null
- ,formpanel: this.config.formpanel || null
- ,actions: this.config.actions || null
- ,items: this.config.buttons || []
- });
- return this.ab;
- }
- ,_loadTabs: function() {
- if (!this.config.tabs) { return false; }
- var o = this.config.tabOptions || {};
- Ext.applyIf(o,{
- xtype: 'modx-tabs'
- ,renderTo: this.config.tabs_div || 'tabs_div'
- ,items: this.config.tabs
- });
- return MODx.load(o);
- }
- ,_loadComponents: function() {
- if (!this.config.components) { return false; }
- var l = this.config.components.length;
- var cp = Ext.getCmp('modx-content');
- for (var i=0;i<l;i=i+1) {
- var a = MODx.load(this.config.components[i]);
- if (cp) {
- cp.add(a);
- }
- }
- if (cp) {
- cp.doLayout();
- }
- return true;
- }
- ,submitForm: function(listeners,options,otherParams) {
- listeners = listeners || {};
- otherParams = otherParams || {};
- if (!this.config.formpanel || !this.config.action) { return false; }
- f = Ext.getCmp(this.config.formpanel);
- if (!f) { return false; }
- for (var i in listeners) {
- if (typeof listeners[i] == 'function') {
- f.on(i,listeners[i],this);
- } else if (listeners[i] && typeof listeners[i] == 'object' && listeners[i].fn) {
- f.on(i,listeners[i].fn,listeners[i].scope || this);
- }
- }
- Ext.apply(f.baseParams,{
- 'action':this.config.action
- });
- Ext.apply(f.baseParams,otherParams);
- options = options || {};
- options.headers = {
- 'Powered-By': 'MODx'
- ,'modAuth': MODx.siteId
- };
- f.submit(options);
- return true;
- }
- });
- Ext.reg('modx-component',MODx.Component);
- MODx.toolbar.ActionButtons = function(config) {
- config = config || {};
- Ext.applyIf(config,{
- actions: { 'close': 'welcome' }
- ,formpanel: false
- ,id: 'modx-action-buttons'
- ,params: {}
- ,items: []
- ,renderTo: Ext.get('modx-action-buttons-container') ? 'modx-action-buttons-container' : 'modx-container'
- });
- if (config.formpanel) {
- this.setupDirtyButtons(config.formpanel);
- }
- this.checkDirtyBtns = [];
- MODx.toolbar.ActionButtons.superclass.constructor.call(this,config);
- this.config = config;
- };
- Ext.extend(MODx.toolbar.ActionButtons,Ext.Toolbar,{
- id: ''
- ,buttons: []
- ,options: { a_close: 'welcome' }
- ,add: function() {
- var a = arguments, l = a.length;
- for(var i = 0; i < l; i++) {
- var el = a[i];
- var ex = ['-','->','<-','',' '];
- if (ex.indexOf(el) != -1 || (el.xtype && el.xtype == 'switch')) {
- MODx.toolbar.ActionButtons.superclass.add.call(this,el);
- continue;
- }
- var id = el.id || Ext.id();
- Ext.applyIf(el,{
- xtype: 'button'
- ,cls: (el.icon ? 'x-btn-icon bmenu' : 'x-btn-text bmenu')
- ,scope: this
- ,disabled: el.checkDirty ? true : false
- ,listeners: {}
- ,id: id
- });
- if (el.button) {
- MODx.toolbar.ActionButtons.superclass.add.call(this,el);
- }
- if (el.handler === null && el.menu === null) {
- el.handler = this.checkConfirm;
- } else if (el.confirm && el.handler) {
- el.handler = function() {
- Ext.Msg.confirm(_('warning'),el.confirm,function(e) {
- if (e === 'yes') { Ext.callback(el.handler,this); }
- },el.scope || this);
- };
- } else if (el.handler) {} else { el.handler = this.handleClick; }
- /* if javascript is specified, run it when button is click, before this.checkConfirm is run */
- if (el.javascript) {
- el.listeners['click'] = {fn:this.evalJS,scope:this};
- }
- /* if checkDirty, disable until field change */
- if (el.xtype == 'button') {
- el.listeners['render'] = {fn:function(btn) {
- if (el.checkDirty && btn) {
- this.checkDirtyBtns.push(btn);
- }
- },scope:this}
- }
- if (el.keys) {
- el.keyMap = new Ext.KeyMap(Ext.get(document));
- for (var j = 0; j < el.keys.length; j++) {
- var key = el.keys[j];
- Ext.applyIf(key,{
- scope: this
- ,stopEvent: true
- ,fn: function(e) {
- var b = Ext.getCmp(id);
- if (b) this.checkConfirm(b,e);
- }
- });
- el.keyMap.addBinding(key);
- }
- el.listeners['destroy'] = {fn:function(btn) {
- btn.keyMap.disable();
- },scope:this}
- }
- /* add button to toolbar */
- MODx.toolbar.ActionButtons.superclass.add.call(this,el);
- }
- }
- ,evalJS: function(itm,e) {
- if (!eval(itm.javascript)) {
- e.stopEvent();
- e.preventDefault();
- }
- }
- ,checkConfirm: function(itm,e) {
- if (itm.confirm !== null && itm.confirm !== undefined) {
- this.confirm(itm,function() {
- this.handleClick(itm,e);
- },this);
- } else { this.handleClick(itm,e); }
- return false;
- }
- ,confirm: function(itm,callback,scope) {
- /* if no message go ahead and redirect...we dont like blank questions */
- if (itm.confirm === null) { return true; }
- Ext.Msg.confirm('',itm.confirm,function(e) {
- /* if the user is okay with the action */
- if (e === 'yes') {
- if (callback === null) { return true; }
- if (typeof(callback) === 'function') { /* if callback is a function, run it, and pass Button */
- Ext.callback(callback,scope || this,[itm]);
- } else { location.href = callback; }
- }
- return true;
- },this);
- return true;
- }
- ,reloadPage: function() {
- location.href = location.href;
- }
- ,handleClick: function(itm,e) {
- var o = this.config;
- if (o.formpanel === false || o.formpanel === undefined || o.formpanel === null) return false;
- if (itm.method === 'remote') { /* if using connectors */
- MODx.util.Progress.reset();
- o.form = Ext.getCmp(o.formpanel);
- if (!o.form) return false;
- var f = o.form.getForm ? o.form.getForm() : o.form;
- var isv = true;
- if (f.items && f.items.items) {
- for (var fld in f.items.items) {
- if (f.items.items[fld] && f.items.items[fld].validate) {
- var fisv = f.items.items[fld].validate();
- if (!fisv) {
- f.items.items[fld].markInvalid();
- isv = false;
- }
- }
- }
- }
- if (isv) {
- Ext.applyIf(o.params,{
- action: itm.process
- });
- Ext.apply(f.baseParams,o.params);
- o.form.on('success',function(r) {
- if (o.form.clearDirty) o.form.clearDirty();
- /* allow for success messages */
- MODx.msg.status({
- title: _('success')
- ,message: r.result.message || _('save_successful')
- ,dontHide: r.result.message != '' ? true : false
- });
- if (itm.redirect != false) {
- var redirect = this.redirect;
- if (typeof itm.redirect == 'function') {
- redirect = itm.redirect;
- }
- Ext.callback(redirect,this,[o,itm,r.result],1000);
- }
- this.resetDirtyButtons(r.result);
- },this);
- o.form.submit({
- headers: {
- 'Powered-By': 'MODx'
- ,'modAuth': MODx.siteId
- }
- });
- } else {
- o.form.fireEvent('failureSubmit');
- Ext.Msg.alert(_('error'),_('correct_errors'));
- }
- } else {
- // if just doing a URL redirect
- var params = itm.params || {};
- Ext.applyIf(params, o.baseParams || {});
- MODx.loadPage('?' + Ext.urlEncode(params));
- }
- return false;
- }
- ,resetDirtyButtons: function(r) {
- for (var i=0;i<this.checkDirtyBtns.length;i=i+1) {
- var btn = this.checkDirtyBtns[i];
- btn.setDisabled(true);
- }
- }
- ,redirect: function(o,itm,res) {
- o = this.config;
- itm.params = itm.params || {};
- Ext.applyIf(itm.params,o.baseParams);
- var url;
- var process = itm.process.substr(itm.process.lastIndexOf('/') + 1);
- if ((process === 'create' || process === 'duplicate' || itm.reload) && res.object.id) {
- itm.params.id = res.object.id;
- if (MODx.request.parent) { itm.params.parent = MODx.request.parent; }
- if (MODx.request.context_key) { itm.params.context_key = MODx.request.context_key; }
- url = Ext.urlEncode(itm.params);
- var action;
- if (o.actions && o.actions.edit) {
- // If an edit action is given, use it (BC)
- action = o.actions.edit;
- } else {
- // Else assume we want the 'update' controller
- action = itm.process.replace('create', 'update');
- }
- MODx.loadPage(action, url);
- } else if (process === 'delete') {
- itm.params.a = o.actions.cancel;
- url = Ext.urlEncode(itm.params);
- MODx.loadPage('?'+url);
- }
- }
- ,refreshTreeNode: function(tree,node,self) {
- var t = parent.Ext.getCmp(tree);
- t.refreshNode(node,self || false);
- return false;
- }
- ,setupDirtyButtons: function(f) {
- var fp = Ext.getCmp(f);
- if (fp) {
- fp.on('fieldChange',function(o) {
- for (var i=0;i<this.checkDirtyBtns.length;i=i+1) {
- var btn = this.checkDirtyBtns[i];
- btn.setDisabled(false);
- }
- },this);
- }
- }
- });
- Ext.reg('modx-actionbuttons',MODx.toolbar.ActionButtons);
|