modx.panel.snippet.js 15 KB

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