modx.panel.plugin.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /**
  2. *
  3. * @class MODx.panel.Plugin
  4. * @extends MODx.FormPanel
  5. * @param {Object} config An object of config properties
  6. * @xtype panel-plugin
  7. */
  8. MODx.panel.Plugin = function(config) {
  9. config = config || {};
  10. config.record = config.record || {};
  11. config = MODx.setStaticElementsConfig(config, 'plugin');
  12. Ext.applyIf(config,{
  13. url: MODx.config.connector_url
  14. ,baseParams: {
  15. action: 'element/plugin/get'
  16. }
  17. ,id: 'modx-panel-plugin'
  18. ,cls: 'container form-with-labels'
  19. ,class_key: 'modPlugin'
  20. ,plugin: ''
  21. ,bodyStyle: ''
  22. ,allowDrop: false
  23. ,items: [{
  24. html: _('plugin_new')
  25. ,id: 'modx-plugin-header'
  26. ,xtype: 'modx-header'
  27. },MODx.getPageStructure([{
  28. title: _('plugin_title')
  29. ,layout: 'form'
  30. ,id: 'modx-plugin-form'
  31. ,labelWidth: 150
  32. ,defaults: { border: false ,msgTarget: 'side' }
  33. ,items: [{
  34. html: '<p>'+_('plugin_msg')+'</p>'
  35. ,id: 'modx-plugin-msg'
  36. ,xtype: 'modx-description'
  37. },{
  38. layout: 'column'
  39. ,border: false
  40. ,defaults: {
  41. layout: 'form'
  42. ,labelAlign: 'top'
  43. ,anchor: '100%'
  44. ,border: false
  45. ,cls:'main-wrapper'
  46. ,labelSeparator: ''
  47. }
  48. ,items: [{
  49. columnWidth: .6
  50. ,items: [{
  51. xtype: 'hidden'
  52. ,name: 'id'
  53. ,id: 'modx-plugin-id'
  54. ,value: config.record.id || 0
  55. },{
  56. xtype: 'hidden'
  57. ,name: 'props'
  58. ,id: 'modx-plugin-props'
  59. ,value: config.record.props || null
  60. },{
  61. xtype: 'textfield'
  62. ,fieldLabel: _('name')+'<span class="required">*</span>'
  63. ,description: MODx.expandHelp ? '' : _('plugin_desc_name')
  64. ,name: 'name'
  65. ,id: 'modx-plugin-name'
  66. ,anchor: '100%'
  67. ,maxLength: 255
  68. ,enableKeyEvents: true
  69. ,allowBlank: false
  70. ,value: config.record.name
  71. ,listeners: {
  72. 'keyup': {scope:this,fn:function(f,e) {
  73. var title = Ext.util.Format.stripTags(f.getValue());
  74. title = _('plugin')+': '+Ext.util.Format.htmlEncode(title);
  75. if (MODx.request.a !== 'element/plugin/create' && MODx.perm.tree_show_element_ids === 1) {
  76. title = title+ ' <small>('+this.config.record.id+')</small>';
  77. }
  78. Ext.getCmp('modx-plugin-header').getEl().update(title);
  79. MODx.setStaticElementPath('plugin');
  80. }}
  81. }
  82. },{
  83. xtype: MODx.expandHelp ? 'label' : 'hidden'
  84. ,forId: 'modx-plugin-name'
  85. ,html: _('plugin_desc_name')
  86. ,cls: 'desc-under'
  87. },{
  88. xtype: 'textarea'
  89. ,fieldLabel: _('plugin_desc')
  90. ,description: MODx.expandHelp ? '' : _('plugin_desc_description')
  91. ,name: 'description'
  92. ,id: 'modx-plugin-description'
  93. ,anchor: '100%'
  94. ,maxLength: 255
  95. ,value: config.record.description
  96. },{
  97. xtype: MODx.expandHelp ? 'label' : 'hidden'
  98. ,forId: 'modx-plugin-description'
  99. ,html: _('plugin_desc_description')
  100. ,cls: 'desc-under'
  101. },{
  102. xtype: 'modx-combo-browser'
  103. ,browserEl: 'modx-browser'
  104. ,fieldLabel: _('static_file')
  105. ,description: MODx.expandHelp ? '' : _('static_file_msg')
  106. ,name: 'static_file'
  107. // ,hideFiles: true
  108. ,source: config.record.source != null ? config.record.source : MODx.config.default_media_source
  109. ,openTo: config.record.openTo || ''
  110. ,id: 'modx-plugin-static-file'
  111. ,triggerClass: 'x-form-code-trigger'
  112. ,anchor: '100%'
  113. ,maxLength: 255
  114. ,value: config.record.static_file || ''
  115. ,hidden: !config.record['static']
  116. ,hideMode: 'offsets'
  117. ,validator: function(value){
  118. if (Ext.getCmp('modx-plugin-static').getValue() === true) {
  119. if (Ext.util.Format.trim(value) != '') {
  120. return true;
  121. } else {
  122. return _('static_file_ns');
  123. }
  124. }
  125. return true;
  126. }
  127. },{
  128. xtype: MODx.expandHelp ? 'label' : 'hidden'
  129. ,forId: 'modx-plugin-static-file'
  130. ,id: 'modx-plugin-static-file-help'
  131. ,html: _('static_file_msg')
  132. ,cls: 'desc-under'
  133. ,hidden: !config.record['static']
  134. ,hideMode: 'offsets'
  135. },{
  136. html: MODx.onPluginFormRender
  137. ,border: false
  138. }]
  139. },{
  140. columnWidth: .4
  141. ,items: [{
  142. xtype: 'modx-combo-category'
  143. ,fieldLabel: _('category')
  144. ,description: MODx.expandHelp ? '' : _('plugin_desc_category')
  145. ,name: 'category'
  146. ,id: 'modx-plugin-category'
  147. ,anchor: '100%'
  148. ,value: config.record.category || 0
  149. ,listeners: {
  150. 'afterrender': {scope:this,fn:function(f,e) {
  151. setTimeout(function(){
  152. MODx.setStaticElementPath('plugin');
  153. }, 200);
  154. }}
  155. ,'change': {scope:this,fn:function(f,e) {
  156. MODx.setStaticElementPath('plugin');
  157. }}
  158. }
  159. },{
  160. xtype: MODx.expandHelp ? 'label' : 'hidden'
  161. ,forId: 'modx-plugin-category'
  162. ,html: _('plugin_desc_category')
  163. ,cls: 'desc-under'
  164. },{
  165. xtype: 'xcheckbox'
  166. ,description: MODx.expandHelp ? '' : _('plugin_disabled_msg')
  167. ,hideLabel: true
  168. ,boxLabel: _('plugin_disabled')
  169. ,name: 'disabled'
  170. ,id: 'modx-plugin-disabled'
  171. ,inputValue: 1
  172. ,checked: config.record.disabled || 0
  173. },{
  174. xtype: MODx.expandHelp ? 'label' : 'hidden'
  175. ,forId: 'modx-plugin-disabled'
  176. ,html: _('plugin_disabled_msg')
  177. ,cls: 'desc-under'
  178. },{
  179. xtype: 'xcheckbox'
  180. ,boxLabel: _('plugin_lock')
  181. ,description: MODx.expandHelp ? '' : _('plugin_lock_msg')
  182. ,hideLabel: true
  183. ,name: 'locked'
  184. ,id: 'modx-plugin-locked'
  185. ,inputValue: 1
  186. ,checked: config.record.locked || 0
  187. },{
  188. xtype: MODx.expandHelp ? 'label' : 'hidden'
  189. ,forId: 'modx-plugin-locked'
  190. ,html: _('plugin_lock_msg')
  191. ,cls: 'desc-under'
  192. },{
  193. xtype: 'xcheckbox'
  194. ,boxLabel: _('clear_cache_on_save')
  195. ,description: MODx.expandHelp ? '' : _('clear_cache_on_save_msg')
  196. ,hideLabel: true
  197. ,name: 'clearCache'
  198. ,id: 'modx-plugin-clear-cache'
  199. ,inputValue: 1
  200. ,checked: Ext.isDefined(config.record.clearCache) || true
  201. },{
  202. xtype: MODx.expandHelp ? 'label' : 'hidden'
  203. ,forId: 'modx-plugin-clear-cache'
  204. ,html: _('clear_cache_on_save_msg')
  205. ,cls: 'desc-under'
  206. },{
  207. xtype: 'xcheckbox'
  208. ,hideLabel: true
  209. ,boxLabel: _('is_static')
  210. ,description: MODx.expandHelp ? '' : _('is_static_msg')
  211. ,name: 'static'
  212. ,id: 'modx-plugin-static'
  213. ,inputValue: 1
  214. ,checked: config.record['static'] || false
  215. },{
  216. xtype: MODx.expandHelp ? 'label' : 'hidden'
  217. ,forId: 'modx-plugin-static'
  218. ,id: 'modx-plugin-static-help'
  219. ,html: _('is_static_msg')
  220. ,cls: 'desc-under'
  221. },{
  222. xtype: 'modx-combo-source'
  223. ,fieldLabel: _('static_source')
  224. ,description: MODx.expandHelp ? '' : _('static_source_msg')
  225. ,name: 'source'
  226. ,id: 'modx-plugin-static-source'
  227. ,anchor: '100%'
  228. ,maxLength: 255
  229. ,value: config.record.source != null ? config.record.source : MODx.config.default_media_source
  230. ,hidden: !config.record['static']
  231. ,hideMode: 'offsets'
  232. ,baseParams: {
  233. action: 'source/getList'
  234. ,showNone: true
  235. ,streamsOnly: true
  236. }
  237. ,listeners: {
  238. select: {
  239. fn: this.changeSource
  240. ,scope: this
  241. }
  242. }
  243. },{
  244. xtype: MODx.expandHelp ? 'label' : 'hidden'
  245. ,forId: 'modx-plugin-static-source'
  246. ,id: 'modx-plugin-static-source-help'
  247. ,html: _('static_source_msg')
  248. ,cls: 'desc-under'
  249. ,hidden: !config.record['static']
  250. ,hideMode: 'offsets'
  251. }]
  252. }]
  253. },{
  254. xtype: 'panel'
  255. ,border: false
  256. ,layout: 'form'
  257. ,cls:'main-wrapper'
  258. ,labelAlign: 'top'
  259. ,items: [{
  260. xtype: 'textarea'
  261. ,fieldLabel: _('plugin_code')
  262. ,name: 'plugincode'
  263. ,id: 'modx-plugin-plugincode'
  264. ,anchor: '100%'
  265. ,height: 400
  266. ,value: config.record.plugincode || "<?php\n"
  267. }]
  268. }]
  269. },{
  270. title: _('system_events')
  271. ,id: 'modx-plugin-sysevents'
  272. ,items: [{
  273. html: '<p>'+_('plugin_event_msg')+'</p>'
  274. ,id: 'modx-plugin-sysevents-msg'
  275. ,xtype: 'modx-description'
  276. },{
  277. xtype: 'modx-grid-plugin-event'
  278. ,cls:'main-wrapper'
  279. ,preventRender: true
  280. ,plugin: config.record.id || 0
  281. ,listeners: {
  282. 'updateEvent': {fn:this.markDirty,scope:this}
  283. ,'rowclick': {fn:this.markDirty,scope:this}
  284. }
  285. }]
  286. },{
  287. xtype: 'modx-panel-element-properties'
  288. ,elementPanel: 'modx-panel-plugin'
  289. ,elementId: config.plugin
  290. ,elementType: 'modPlugin'
  291. ,record: config.record
  292. }],{
  293. id: 'modx-plugin-tabs'
  294. })]
  295. ,useLoadingMask: true
  296. ,listeners: {
  297. 'setup': {fn:this.setup,scope:this}
  298. ,'success': {fn:this.success,scope:this}
  299. ,'beforeSubmit': {fn:this.beforeSubmit,scope:this}
  300. ,'failureSubmit': {
  301. fn: function () {
  302. this.showErroredTab(['modx-plugin-form'], 'modx-plugin-tabs')
  303. },
  304. scope: this
  305. }
  306. }
  307. });
  308. MODx.panel.Plugin.superclass.constructor.call(this,config);
  309. var isStatic = Ext.getCmp('modx-plugin-static');
  310. if (isStatic) { isStatic.on('check',this.toggleStaticFile); }
  311. };
  312. Ext.extend(MODx.panel.Plugin,MODx.FormPanel,{
  313. initialized: false
  314. ,setup: function() {
  315. if (this.initialized) { this.clearDirty(); return true; }
  316. this.getForm().setValues(this.config.record);
  317. if (!Ext.isEmpty(this.config.record.name)) {
  318. var title = _('plugin')+': '+this.config.record.name;
  319. if (MODx.perm.tree_show_element_ids === 1) {
  320. title = title+ ' <small>('+this.config.record.id+')</small>';
  321. }
  322. Ext.getCmp('modx-plugin-header').getEl().update(title);
  323. }
  324. if (!Ext.isEmpty(this.config.record.properties)) {
  325. var d = this.config.record.properties;
  326. var g = Ext.getCmp('modx-grid-element-properties');
  327. if (g) {
  328. g.defaultProperties = d;
  329. g.getStore().loadData(d);
  330. }
  331. }
  332. this.fireEvent('ready',this.config.record);
  333. if (MODx.onLoadEditor) { MODx.onLoadEditor(this); }
  334. this.clearDirty();
  335. MODx.fireEvent('ready');
  336. this.initialized = true;
  337. }
  338. /**
  339. * Set the browser window "media source" source
  340. */
  341. ,changeSource: function() {
  342. var browser = Ext.getCmp('modx-plugin-static-file')
  343. ,source = Ext.getCmp('modx-plugin-static-source').getValue();
  344. browser.config.source = source;
  345. }
  346. ,beforeSubmit: function(o) {
  347. var g = Ext.getCmp('modx-grid-plugin-event');
  348. Ext.apply(o.form.baseParams,{
  349. events: g.encodeModified()
  350. ,propdata: Ext.getCmp('modx-grid-element-properties').encode()
  351. });
  352. this.cleanupEditor();
  353. return this.fireEvent('save',{
  354. values: this.getForm().getValues()
  355. ,stay: MODx.config.stay
  356. });
  357. }
  358. ,success: function(o) {
  359. if (MODx.request.id) Ext.getCmp('modx-grid-element-properties').save();
  360. Ext.getCmp('modx-grid-plugin-event').getStore().commitChanges();
  361. this.getForm().setValues(o.result.object);
  362. var t = Ext.getCmp('modx-tree-element');
  363. if (t) {
  364. var c = Ext.getCmp('modx-plugin-category').getValue();
  365. var u = c != '' && c != null && c != 0 ? 'n_plugin_category_'+c : 'n_type_plugin';
  366. var node = t.getNodeById('n_plugin_element_' + Ext.getCmp('modx-plugin-id').getValue() + '_' + o.result.object.previous_category);
  367. if (node) node.destroy();
  368. t.refreshNode(u,true);
  369. }
  370. }
  371. ,changeEditor: function() {
  372. this.cleanupEditor();
  373. this.on('success',function(o) {
  374. var id = o.result.object.id;
  375. var w = Ext.getCmp('modx-plugin-which-editor').getValue();
  376. MODx.request.a = 'element/plugin/update';
  377. location.href = '?'+Ext.urlEncode(MODx.request)+'&which_editor='+w+'&id='+id;
  378. });
  379. this.submit();
  380. }
  381. ,cleanupEditor: function() {
  382. if (MODx.onSaveEditor) {
  383. var fld = Ext.getCmp('modx-plugin-plugincode');
  384. MODx.onSaveEditor(fld);
  385. }
  386. }
  387. ,toggleStaticFile: function(cb) {
  388. var flds = ['modx-plugin-static-file','modx-plugin-static-file-help','modx-plugin-static-source','modx-plugin-static-source-help'];
  389. var fld,i;
  390. if (cb.checked) {
  391. for (i in flds) {
  392. fld = Ext.getCmp(flds[i]);
  393. if (fld) { fld.show(); }
  394. }
  395. } else {
  396. for (i in flds) {
  397. fld = Ext.getCmp(flds[i]);
  398. if (fld) { fld.hide(); }
  399. }
  400. }
  401. }
  402. });
  403. Ext.reg('modx-panel-plugin',MODx.panel.Plugin);