home.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. ClientConfig.page.Home = function(config) {
  2. config = config || {};
  3. Ext.applyIf(config,{
  4. id: 'clientconfig-page-home',
  5. renderTo: 'clientconfig-wrapper-div',
  6. border: false,
  7. components: [{
  8. cls: 'container',
  9. xtype: 'panel',
  10. items: [{
  11. html: '<h2>' + _('clientconfig') + '</h2>',
  12. border: false,
  13. cls: 'modx-page-header',
  14. id: 'clientconfig-header'
  15. }, {
  16. xtype: 'modx-formpanel',
  17. id: 'clientconfig-formpanel-home',
  18. cls: 'form-with-labels',
  19. layout: 'form',
  20. border: !!ClientConfig.config.verticalTabs && !MODx.config.connector_url,
  21. anchor: '100%',
  22. items: [{
  23. xtype: 'hidden',
  24. name: 'context'
  25. },{
  26. xtype: (!!ClientConfig.config.verticalTabs) ? 'modx-vtabs' : 'modx-tabs',
  27. border: false,
  28. deferredRender: false,
  29. defaults: {
  30. border: false,
  31. autoHeight: true,
  32. defaults: {
  33. border: false
  34. }
  35. },
  36. items: this.getTabs(),
  37. stateful: true,
  38. stateId: 'clientconfig-page-home',
  39. stateEvents: ['tabchange'],
  40. getState: function () {
  41. return {
  42. activeTab: this.items.indexOf(this.getActiveTab())
  43. };
  44. },
  45. headerCfg: (ClientConfig.config.verticalTabs) ? {
  46. tag: 'div',
  47. cls: 'x-tab-panel-header vertical-tabs-header',
  48. id: 'modx-resource-vtabs-header',
  49. html: MODx.config.show_tv_categories_header == true ? '<h4 id="modx-resource-vtabs-header-title">' + _('clientconfig.categories') + '</h4>' : ''
  50. } : undefined
  51. }]
  52. }]
  53. }],
  54. buttons: this.getButtons(),
  55. listeners: {
  56. afterrender: this.setup
  57. }
  58. });
  59. ClientConfig.page.Home.superclass.constructor.call(this,config);
  60. };
  61. Ext.extend(ClientConfig.page.Home,MODx.Component,{
  62. rtes: [],
  63. setup: function() {
  64. var rtes = this.rtes;
  65. setTimeout(function() {
  66. if (rtes.length > 0 && MODx.loadRTE) {
  67. Ext.each(rtes, function(id, index) {
  68. MODx.loadRTE(id);
  69. });
  70. }
  71. if (ClientConfig.contextAware && ClientConfig.initialContext && ClientConfig.initialContext.key) {
  72. var contextSelector = Ext.getCmp('clientconfig-combo-contexts');
  73. contextSelector.setValue(ClientConfig.initialContext.key);
  74. contextSelector.fireEvent('select', contextSelector);
  75. }
  76. }, 150)
  77. },
  78. getTabs: function() {
  79. var tabs = [],
  80. rtes = [];
  81. Ext.each(ClientConfig.data, function(tabData) {
  82. var fields = [];
  83. Ext.iterate(tabData.items, function(value) {
  84. var field = {
  85. name: value.key,
  86. xtype: value.xtype,
  87. fieldLabel: value.label + ((value.is_required) ? '*' : ''),
  88. value: (value.value != '') ? value.value : value.default,
  89. description: (ClientConfig.isAdmin) ? '<b>[[++' + value.key + ']]</b>' : undefined,
  90. allowBlank: !value.is_required,
  91. anchor: '60%',
  92. id: 'clientconfig-' + value.key.replace('.','-')
  93. };
  94. if (['textarea'].indexOf(field.xtype) !== -1) {
  95. field.anchor = '100%';
  96. }
  97. if (field.xtype == 'richtext') {
  98. field.xtype = 'textarea';
  99. rtes.push(field.id);
  100. }
  101. if ((field.xtype == 'checkbox') || (field.xtype == 'xcheckbox')) {
  102. field.boxLabel = field.fieldLabel;
  103. field.fieldLabel = null;
  104. field.value = 1;
  105. field.checked = (value.value);
  106. }
  107. if (field.xtype == 'datefield') {
  108. field.format = MODx.config.manager_date_format;
  109. }
  110. if (field.xtype == 'timefield') {
  111. field.format = MODx.config.manager_time_format;
  112. }
  113. if (['modx-panel-tv-image', 'modx-panel-tv-file'].indexOf(field.xtype) !== -1) {
  114. field.tv = value.key;
  115. field.source = value.source || MODx.config.default_media_source;
  116. field.relativeValue = (value.value != '') ? value.value : value.default;
  117. }
  118. if (field.xtype == 'colorpickerfield') {
  119. field.cls = 'x-colorpicker';
  120. }
  121. if (field.xtype == 'password') {
  122. field.xtype = 'textfield';
  123. field.inputType = 'password';
  124. }
  125. if (field.xtype == 'modx-combo') {
  126. var options = value.options.split('||');
  127. var data = [];
  128. Ext.each(options, function(option, index) {
  129. option = option.split('==');
  130. if (option[1]) {
  131. data.push([
  132. option[1],
  133. option[0]
  134. ]);
  135. } else {
  136. data.push([option[0],option[0]]);
  137. }
  138. });
  139. field.store = new Ext.data.ArrayStore({
  140. mode: 'local',
  141. fields: ['value','label'],
  142. data: data
  143. });
  144. field.hiddenName = value.key;
  145. field.valueField = 'value';
  146. field.displayField = 'label';
  147. field.mode = 'local';
  148. }
  149. fields.push(field);
  150. if (value.description && value.description.length > 0) {
  151. var fieldDescription = {
  152. xtype: 'label',
  153. text: value.description,
  154. cls: 'desc-under'
  155. };
  156. fields.push(fieldDescription);
  157. }
  158. });
  159. /* Only create the tab if there are fields in it. */
  160. if (fields.length >= 1) {
  161. var tab = {
  162. id: 'clientconfig-home-tab-'+tabData.id,
  163. title: tabData.label,
  164. items: [],
  165. cls: 'tvs-wrapper'
  166. };
  167. if (tabData.description != '') {
  168. tab.items.push({
  169. bodyCssClass: 'panel-desc',
  170. cls: ' ',
  171. html: '<p>'+tabData.description+'</p>'
  172. });
  173. }
  174. tab.items.push({
  175. cls: 'main-wrapper',
  176. items: fields,
  177. xtype: 'panel',
  178. layout: 'form',
  179. labelAlign: 'top',
  180. labelSeparator: '',
  181. width: '85%'
  182. });
  183. tabs.push(tab);
  184. }
  185. });
  186. this.rtes = rtes;
  187. if (tabs.length < 1) {
  188. return [{
  189. title: _('clientconfig.no_configuration_yet'),
  190. items: [{
  191. html: '<p>'+_('clientconfig.no_configuration_yet.desc')+'</p>',
  192. bodyCssClass: 'panel-desc'
  193. }]
  194. }];
  195. }
  196. return tabs;
  197. },
  198. getButtons: function() {
  199. var buttons = [];
  200. if (ClientConfig.contextAware) {
  201. buttons.push('-',{
  202. xtype: 'panel',
  203. html: '<span style="padding-left: 1em; padding-right: 1em;">' + _('clientconfig.choose_context') + ': </span>'
  204. },{
  205. emptyText: _('clientconfig.choose_context'),
  206. xtype: 'clientconfig-combo-contexts',
  207. id: 'clientconfig-combo-contexts',
  208. listeners: {
  209. change: {fn: this.switchContext, scope: this},
  210. select: {fn: this.switchContext, scope: this}
  211. }
  212. });
  213. }
  214. buttons.push({
  215. text: _('clientconfig.save_config'),
  216. handler: this.save,
  217. cls: 'primary-button',
  218. scope: this,
  219. keys: [{
  220. key: MODx.config.keymap_save || 's',
  221. ctrl: true,
  222. fn: this.save
  223. }]
  224. });
  225. if (ClientConfig.isAdmin) {
  226. buttons.push('-',{
  227. text: '<i class="icon icon-cog"></i> ' + _('clientconfig.admin'),
  228. handler: this.openAdminPanel,
  229. scope: this
  230. })
  231. }
  232. return buttons;
  233. },
  234. switchContext: function(field) {
  235. var ctx = field.getValue(),
  236. fp = Ext.getCmp('clientconfig-formpanel-home'),
  237. heading = Ext.getCmp('clientconfig-header'),
  238. rtes = this.rtes;
  239. fp.el.mask(_('loading'));
  240. MODx.Ajax.request({
  241. url: ClientConfig.config.connectorUrl,
  242. params: {
  243. action: ctx.length > 0 ? 'mgr/settings/getcontextaware' : 'mgr/settings/getglobal',
  244. context: ctx
  245. },
  246. listeners: {
  247. success: {fn: function(r) {
  248. var form = fp.getForm();
  249. if (form) {
  250. // Destroy RTEs before resetting and updating the form
  251. ClientConfig.destroyRTEs(rtes);
  252. // Set the new context-specific values
  253. form.reset();
  254. form.setValues(r.object);
  255. // Re-initialize editors
  256. if (MODx.loadRTE) {
  257. Ext.each(rtes, function(id, index) {
  258. MODx.loadRTE(id);
  259. });
  260. }
  261. }
  262. fp.el.unmask();
  263. var headingText = r.object.context_name.length > 0
  264. ? _('clientconfig.config_for_context', {context: r.object.context_name})
  265. : _('clientconfig');
  266. heading.update('<h2>' + headingText + '</h2>');
  267. }, scope: this},
  268. failure: {fn: function() {
  269. fp.el.unmask();
  270. }, scope: this},
  271. scope: this
  272. }
  273. });
  274. },
  275. save: function() {
  276. var fp = Ext.getCmp('clientconfig-formpanel-home');
  277. if (fp && fp.getForm()) {
  278. var values = fp.getForm().getValues();
  279. // Fix name of image tv
  280. var imagePickers = fp.find('xtype', 'modx-panel-tv-image');
  281. Ext.each(imagePickers, function(imagePicker){
  282. values[imagePicker.name] = values['tv' + imagePicker.name];
  283. }, this);
  284. var filePickers = fp.find('xtype', 'modx-panel-tv-file');
  285. Ext.each(filePickers, function(filePicker){
  286. values[filePicker.name] = values['tv' + filePicker.name];
  287. }, this);
  288. fp.el.mask(_('saving'));
  289. MODx.Ajax.request({
  290. url: ClientConfig.config.connectorUrl,
  291. params: {
  292. action: 'mgr/settings/save',
  293. values: Ext.encode(values)
  294. },
  295. listeners: {
  296. success: {fn: function(r) {
  297. fp.el.unmask();
  298. MODx.msg.status({
  299. title: _('clientconfig.saved'),
  300. message: _('clientconfig.saved.text'),
  301. delay: 4
  302. })
  303. }, scope: this},
  304. failure: {fn: function(r) {
  305. fp.el.unmask();
  306. }, scope: this},
  307. scope: this
  308. }
  309. });
  310. }
  311. },
  312. openAdminPanel: function() {
  313. if (ClientConfig.isAdmin) {
  314. MODx.loadPage('?a=admin&namespace=clientconfig');
  315. }
  316. }
  317. });
  318. Ext.reg('clientconfig-page-home',ClientConfig.page.Home);