modx.layout.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /**
  2. * Loads the MODx Ext-driven Layout
  3. *
  4. * @class MODx.Layout
  5. * @extends Ext.Viewport
  6. * @param {Object} config An object of config options.
  7. * @xtype modx-layout
  8. */
  9. Ext.apply(Ext, {
  10. isFirebug: (window.console && window.console.firebug)
  11. });
  12. MODx.Layout = function(config){
  13. config = config || {};
  14. Ext.BLANK_IMAGE_URL = MODx.config.manager_url+'assets/ext3/resources/images/default/s.gif';
  15. Ext.Ajax.defaultHeaders = {
  16. 'modAuth': config.auth
  17. };
  18. Ext.Ajax.extraParams = {
  19. 'HTTP_MODAUTH': config.auth
  20. };
  21. MODx.siteId = config.auth;
  22. MODx.expandHelp = !!+MODx.config.inline_help;
  23. var sp = new MODx.HttpProvider();
  24. Ext.state.Manager.setProvider(sp);
  25. sp.initState(MODx.defaultState);
  26. config.showTree = false;
  27. Ext.applyIf(config, {
  28. layout: 'border'
  29. ,id: 'modx-layout'
  30. ,stateSave: true
  31. ,items: this.buildLayout(config)
  32. });
  33. MODx.Layout.superclass.constructor.call(this,config);
  34. this.config = config;
  35. this.addEvents({
  36. 'afterLayout': true
  37. ,'loadKeyMap': true
  38. ,'loadTabs': true
  39. });
  40. this.loadKeys();
  41. if (!config.showTree) {
  42. Ext.getCmp('modx-leftbar-tabs').collapse(false);
  43. Ext.get('modx-leftbar').hide();
  44. Ext.get('modx-leftbar-tabs-xcollapsed').setStyle('display','none');
  45. }
  46. this.fireEvent('afterLayout');
  47. };
  48. Ext.extend(MODx.Layout, Ext.Viewport, {
  49. /**
  50. * Wrapper method to build the layout regions
  51. *
  52. * @param {Object} config
  53. *
  54. * @returns {Array}
  55. */
  56. buildLayout: function(config) {
  57. var items = []
  58. ,north = this.getNorth(config)
  59. ,west = this.getWest(config)
  60. ,center = this.getCenter(config)
  61. ,south = this.getSouth(config)
  62. ,east = this.getEast(config);
  63. if (north && Ext.isObject(north)) {
  64. items.push(north);
  65. }
  66. if (west && Ext.isObject(west)) {
  67. items.push(west);
  68. }
  69. if (center && Ext.isObject(center)) {
  70. items.push(center);
  71. }
  72. if (south && Ext.isObject(south)) {
  73. items.push(south);
  74. }
  75. if (east && Ext.isObject(east)) {
  76. items.push(east);
  77. }
  78. return items;
  79. }
  80. /**
  81. * Build the north region (header)
  82. *
  83. * @param {Object} config
  84. *
  85. * @returns {Object|void}
  86. */
  87. ,getNorth: function(config) {
  88. return {
  89. xtype: 'box'
  90. ,region: 'north'
  91. ,applyTo: 'modx-header'
  92. //,height: 55
  93. };
  94. }
  95. /**
  96. * Build the west region (trees)
  97. *
  98. * @param {Object} config
  99. *
  100. * @returns {Object|void}
  101. */
  102. ,getWest: function(config) {
  103. var tabs = [];
  104. if (MODx.perm.resource_tree) {
  105. tabs.push({
  106. title: _('resources')
  107. ,xtype: 'modx-tree-resource'
  108. ,id: 'modx-resource-tree'
  109. });
  110. config.showTree = true;
  111. }
  112. if (MODx.perm.element_tree) {
  113. tabs.push({
  114. title: _('elements')
  115. ,xtype: 'modx-tree-element'
  116. ,id: 'modx-tree-element'
  117. });
  118. config.showTree = true;
  119. }
  120. if (MODx.perm.file_tree) {
  121. tabs.push({
  122. title: _('files')
  123. ,xtype: 'modx-panel-filetree'
  124. ,id: 'modx-file-tree'
  125. });
  126. config.showTree = true;
  127. }
  128. var activeTab = 0;
  129. return {
  130. region: 'west'
  131. ,applyTo: 'modx-leftbar'
  132. ,id: 'modx-leftbar-tabs'
  133. ,split: true
  134. ,width: 310
  135. ,minSize: 288
  136. ,autoScroll: true
  137. ,unstyled: true
  138. ,collapseMode: 'mini'
  139. ,useSplitTips: true
  140. ,monitorResize: true
  141. ,layout: 'anchor'
  142. ,items: [{
  143. xtype: 'modx-tabs'
  144. ,plain: true
  145. ,defaults: {
  146. autoScroll: true
  147. ,fitToFrame: true
  148. }
  149. ,id: 'modx-leftbar-tabpanel'
  150. ,border: false
  151. ,anchor: '100%'
  152. ,activeTab: activeTab
  153. ,stateful: true
  154. //,stateId: 'modx-leftbar-tabs'
  155. ,stateEvents: ['tabchange']
  156. ,getState:function() {
  157. return {
  158. activeTab: this.items.indexOf(this.getActiveTab())
  159. };
  160. }
  161. ,items: tabs
  162. }]
  163. ,getState: function() {
  164. // The region's attributes we want to save/restore
  165. return {
  166. collapsed: this.collapsed
  167. ,width: this.width
  168. };
  169. }
  170. ,listeners:{
  171. beforestatesave: this.onBeforeSaveState
  172. ,scope: this
  173. }
  174. };
  175. }
  176. /**
  177. * Build the center region (main content)
  178. *
  179. * @param {Object} config
  180. *
  181. * @returns {Object|void}
  182. */
  183. ,getCenter: function(config) {
  184. return {
  185. region: 'center'
  186. ,applyTo: 'modx-content'
  187. ,padding: '0 1px 0 0'
  188. ,bodyStyle: 'background-color:transparent;'
  189. ,id: 'modx-content'
  190. ,border: false
  191. ,autoScroll: true
  192. };
  193. }
  194. /**
  195. * Build the south region (footer)
  196. *
  197. * @param {Object} config
  198. *
  199. * @returns {Object|void}
  200. */
  201. ,getSouth: function(config) {
  202. }
  203. /**
  204. * Build the east region
  205. *
  206. * @param {Object} config
  207. *
  208. * @returns {Object|void}
  209. */
  210. ,getEast: function(config) {
  211. }
  212. /**
  213. * Convenient method to target the west region
  214. *
  215. * @returns {Ext.Component|void}
  216. */
  217. ,getLeftBar: function() {
  218. var nav = Ext.getCmp('modx-leftbar-tabpanel');
  219. if (nav) {
  220. return nav;
  221. }
  222. return null;
  223. }
  224. /**
  225. * Add the given item(s) to the west container
  226. *
  227. * @param {Object|Array} items
  228. */
  229. ,addToLeftBar: function(items) {
  230. var nav = this.getLeftBar();
  231. if (nav && items) {
  232. nav.add(items);
  233. this.onAfterLeftBarAdded(nav, items);
  234. }
  235. }
  236. /**
  237. * Method executed after some item(s) has been added to the west container
  238. *
  239. * @param {Ext.Component} nav The container
  240. * @param {Object|Array} items Added item(s)
  241. */
  242. ,onAfterLeftBarAdded: function(nav, items) {
  243. }
  244. /**
  245. * Set keyboard shortcuts
  246. */
  247. ,loadKeys: function() {
  248. Ext.KeyMap.prototype.stopEvent = true;
  249. var k = new Ext.KeyMap(Ext.get(document));
  250. // ctrl + shift + h : toggle left bar
  251. k.addBinding({
  252. key: Ext.EventObject.H
  253. ,ctrl: true
  254. ,shift: true
  255. ,fn: this.toggleLeftbar
  256. ,scope: this
  257. ,stopEvent: true
  258. });
  259. // ctrl + shift + n : new document
  260. k.addBinding({
  261. key: Ext.EventObject.N
  262. ,ctrl: true
  263. ,shift: true
  264. ,fn: function() {
  265. var t = Ext.getCmp('modx-resource-tree');
  266. if (t) { t.quickCreate(document,{},'modDocument','web',0); }
  267. }
  268. ,stopEvent: true
  269. });
  270. // ctrl + shift + u : clear cache
  271. k.addBinding({
  272. key: Ext.EventObject.U
  273. ,ctrl: true
  274. ,shift: true
  275. ,alt: false
  276. ,fn: MODx.clearCache
  277. ,scope: this
  278. ,stopEvent: true
  279. });
  280. this.fireEvent('loadKeyMap',{
  281. keymap: k
  282. });
  283. }
  284. /**
  285. * Wrapper method to refresh all available trees
  286. */
  287. ,refreshTrees: function() {
  288. var t;
  289. t = Ext.getCmp('modx-resource-tree');
  290. if (t && t.rendered) {
  291. t.refresh();
  292. }
  293. t = Ext.getCmp('modx-tree-element');
  294. if (t && t.rendered) {
  295. t.refresh();
  296. }
  297. t = Ext.getCmp('modx-file-tree');
  298. if (t && t.rendered) {
  299. // Iterate over panel's items (trees) to refresh them
  300. t.items.each(function(tree, idx) {
  301. tree.refresh();
  302. });
  303. }
  304. }
  305. // Why here & why assuming visible ??
  306. ,leftbarVisible: true
  307. /**
  308. * Toggle left bar
  309. */
  310. ,toggleLeftbar: function() {
  311. this.leftbarVisible ? this.hideLeftbar(true) : this.showLeftbar(true);
  312. // Toggle the left bar visibility
  313. this.leftbarVisible = !this.leftbarVisible;
  314. }
  315. /**
  316. * Hide the left bar
  317. *
  318. * @param {Boolean} [anim] Whether or not to animate the transition
  319. * @param {Boolean} [state] Whether or not to save the component's state
  320. */
  321. ,hideLeftbar: function(anim, state) {
  322. Ext.getCmp('modx-leftbar-tabs').collapse(anim);
  323. if (Ext.isBoolean(state)) {
  324. this.stateSave = state;
  325. }
  326. }
  327. /**
  328. * Show the left bar
  329. *
  330. * @param {Boolean} [anim] Whether or not to animate the transition
  331. */
  332. ,showLeftbar: function(anim) {
  333. Ext.getCmp('modx-leftbar-tabs').expand(anim);
  334. }
  335. /**
  336. * Actions performed before we save the component state
  337. *
  338. * @param {Ext.Component} component
  339. * @param {Object} state
  340. */
  341. ,onBeforeSaveState: function(component, state) {
  342. var collapsed = state.collapsed;
  343. if (collapsed && !this.stateSave) {
  344. // Stateful status changed to prevent saving the state
  345. this.stateSave = true;
  346. return false;
  347. }
  348. if (!collapsed) {
  349. var wrap = Ext.get('modx-leftbar').down('div');
  350. if (!wrap.isVisible()) {
  351. // Set the "masking div" to visible
  352. wrap.setVisible(true);
  353. Ext.getCmp('modx-leftbar-tabpanel').expand(true);
  354. }
  355. }
  356. }
  357. });
  358. /**
  359. * Handles layout functions. In module format for easier privitization.
  360. * @class MODx.LayoutMgr
  361. */
  362. MODx.LayoutMgr = function() {
  363. var _activeMenu = 'menu0';
  364. return {
  365. loadPage: function(action, parameters) {
  366. // Handles url, passed as first argument
  367. var parts = [];
  368. if (action) {
  369. if (isNaN(parseInt(action)) && (action.substr(0,1) == '?' || (action.substr(0, "index.php?".length) == 'index.php?'))) {
  370. parts.push(action);
  371. } else {
  372. parts.push('?a=' + action);
  373. }
  374. }
  375. if (parameters) {
  376. parts.push(parameters);
  377. }
  378. var url = parts.join('&');
  379. if (MODx.fireEvent('beforeLoadPage', url)) {
  380. var e = window.event;
  381. var middleMouseButtonClick = (e && (e.button === 4 || e.which === 2));
  382. var keyboardKeyPressed = (e && (e.button === 1 || e.ctrlKey === true || e.metaKey === true || e.shiftKey === true));
  383. if (middleMouseButtonClick || keyboardKeyPressed) {
  384. // Middle mouse button click or keyboard key pressed,
  385. // let the browser handle the way it should be opened (new tab/window)
  386. return window.open(url);
  387. }
  388. location.href = url;
  389. }
  390. return false;
  391. }
  392. ,changeMenu: function(a,sm) {
  393. if (sm === _activeMenu) return false;
  394. Ext.get(sm).addClass('active');
  395. var om = Ext.get(_activeMenu);
  396. if (om) om.removeClass('active');
  397. _activeMenu = sm;
  398. return false;
  399. }
  400. }
  401. }();
  402. /* aliases for quicker reference */
  403. MODx.loadPage = MODx.LayoutMgr.loadPage;
  404. MODx.showDashboard = MODx.LayoutMgr.showDashboard;
  405. MODx.hideDashboard = MODx.LayoutMgr.hideDashboard;
  406. MODx.changeMenu = MODx.LayoutMgr.changeMenu;