modx.panel.template.js 17 KB

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