tree.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. var galTreeHandlerClass = function(config) {
  2. config = config || {};
  3. Ext.apply(config,{
  4. id: 'gal-tree-handler'
  5. });
  6. galTreeHandlerClass.superclass.constructor.call(this,config);
  7. };
  8. Ext.extend(galTreeHandlerClass,Ext.Component,{
  9. tree: null
  10. ,data: {}
  11. ,windows: {}
  12. ,getMenu: function(t,node,e) {
  13. this.tree = t;
  14. this.data = node.attributes && node.attributes.data ? node.attributes.data : {};
  15. var m = [];
  16. switch (node.attributes.type) {
  17. case 'root':
  18. m = this.getRootMenu(node,e);
  19. break;
  20. case 'gallery-album':
  21. m = this.getAlbumMenu(node,e);
  22. break;
  23. case 'gallery-item':
  24. m = this.getItemMenu(node,e);
  25. break;
  26. }
  27. return m;
  28. }
  29. /* custom methods here */
  30. ,getItemMenu: function(node,e) {
  31. return [{
  32. text: _('gallery.item_update')
  33. ,handler: this.updateItem
  34. ,scope: this
  35. },'-',{
  36. text: _('gallery.item_remove')
  37. ,handler: this.removeItem
  38. ,scope: this
  39. }];
  40. }
  41. ,getAlbumMenu: function(node,e) {
  42. return [{
  43. text: _('gallery.album_create')
  44. ,handler: this.createAlbum
  45. ,scope: this
  46. },'-',{
  47. text: _('gallery.album_update')
  48. ,handler: this.updateAlbum
  49. ,scope: this
  50. },{
  51. text: _('gallery.upload')
  52. ,menu: {
  53. items: [{
  54. text: _('gallery.item_upload')
  55. ,handler: this.uploadItem
  56. ,scope: this
  57. },{
  58. text: _('gallery.multi_item_upload')
  59. ,handler: this.uploadMultiItems
  60. ,scope: this
  61. },{
  62. text: _('gallery.batch_upload')
  63. ,handler: this.batchUpload
  64. ,scope: this
  65. },{
  66. text: _('gallery.zip_upload')
  67. ,handler: this.zipUpload
  68. ,scope: this
  69. }]
  70. }
  71. },'-',{
  72. text: _('gallery.album_remove')
  73. ,handler: this.removeAlbum
  74. ,scope: this
  75. }];
  76. }
  77. ,getRootMenu: function(node,e) {
  78. return [{
  79. text: _('gallery.album_create')
  80. ,handler: this.createAlbum
  81. ,scope: this
  82. }];
  83. }
  84. ,createAlbum: function(btn,e) {
  85. var r;
  86. if (this.data.id) {
  87. var n = this.tree.cm.activeNode.attributes.data;
  88. r = {
  89. 'parent': n.id
  90. ,parent_name: n.name
  91. };
  92. } else {
  93. r = {'parent':0,parent_name:_('none')};
  94. }
  95. if (!this.windows.createAlbum) {
  96. this.windows.createAlbum = MODx.load({
  97. xtype: 'gal-window-album-create'
  98. ,record: r
  99. ,listeners: {
  100. 'success': {fn:function() { this.tree.refreshParentNode(); },scope:this}
  101. }
  102. });
  103. }
  104. this.windows.createAlbum.fp.getForm().reset();
  105. this.windows.createAlbum.setValues(r);
  106. this.windows.createAlbum.show(e.target);
  107. }
  108. ,updateAlbum: function(btn,e) {
  109. var id = this.data.id ? this.data.id : 0;
  110. location.href = '?a='+MODx.action['gallery:index']+'&album='+id+'&action=album/update';
  111. }
  112. ,removeAlbum: function(btn,e) {
  113. MODx.msg.confirm({
  114. text: _('gallery.album_remove_confirm')
  115. ,url: GAL.config.connector_url
  116. ,params: {
  117. action: 'mgr/album/remove'
  118. ,id: this.tree.cm.activeNode.attributes.data.id
  119. }
  120. ,listeners: {
  121. 'success': {fn:function(r) { this.tree.refreshParentNode(); },scope:this}
  122. }
  123. });
  124. }
  125. ,updateItem: function(btn,e) {
  126. this.windows.updateItem = MODx.load({
  127. xtype: 'gal-window-item-update'
  128. ,listeners: {
  129. 'success': {fn:function() { this.tree.refreshParentNode(); },scope:this}
  130. }
  131. });
  132. this.windows.updateItem.setValues(this.data);
  133. this.windows.updateItem.show(e.target);
  134. }
  135. ,removeItem: function(btn,e) {
  136. MODx.msg.confirm({
  137. text: _('gallery.item_delete_confirm')
  138. ,url: GAL.config.connector_url
  139. ,params: {
  140. action: 'mgr/item/remove'
  141. ,id: this.data.id
  142. }
  143. ,listeners: {
  144. 'success': {fn:function(r) { this.tree.refreshParentNode(); },scope:this}
  145. }
  146. });
  147. }
  148. ,uploadMultiItems: function(btn,e) {
  149. var r = {
  150. album: this.data.id
  151. ,active: true
  152. };
  153. if (!this.windows.uploadMultiItems) {
  154. this.windows.uploadMultiItems = MODx.load({
  155. xtype: 'gal-window-multi-item-upload'
  156. ,album: this.data.id
  157. ,record: r
  158. ,listeners: {
  159. 'success': {fn:function() { this.tree.refreshParentNode(); },scope:this}
  160. }
  161. });
  162. }
  163. this.windows.uploadMultiItems.fp.getForm().reset();
  164. this.windows.uploadMultiItems.setValues(r);
  165. this.windows.uploadMultiItems.show(e.target);
  166. }
  167. ,uploadItem: function(btn,e) {
  168. var r = {
  169. album: this.data.id
  170. ,active: true
  171. };
  172. if (!this.windows.uploadItem) {
  173. this.windows.uploadItem = MODx.load({
  174. xtype: 'gal-window-item-upload'
  175. ,record: r
  176. ,listeners: {
  177. 'success': {fn:function() { this.tree.refreshParentNode(); },scope:this}
  178. }
  179. });
  180. }
  181. this.windows.uploadItem.fp.getForm().reset();
  182. this.windows.uploadItem.setValues(r);
  183. this.windows.uploadItem.show(e.target);
  184. }
  185. ,batchUpload: function(btn,e) {
  186. var r = {
  187. album: this.data.id
  188. ,active: true
  189. };
  190. if (!this.windows.batchUpload) {
  191. this.windows.batchUpload = MODx.load({
  192. xtype: 'gal-window-batch-upload'
  193. ,record: r
  194. ,listeners: {
  195. 'success': {fn:function() { this.tree.refreshParentNode(); },scope:this}
  196. }
  197. });
  198. } else {
  199. this.windows.batchUpload.fp.getForm().reset();
  200. }
  201. this.windows.batchUpload.setValues(r);
  202. this.windows.batchUpload.show(e.target);
  203. }
  204. ,zipUpload: function(btn,e) {
  205. var r = {
  206. album: this.data.id
  207. ,active: true
  208. };
  209. if (!this.windows.zipUpload) {
  210. this.windows.zipUpload = MODx.load({
  211. xtype: 'gal-window-zip-upload'
  212. ,record: r
  213. ,listeners: {
  214. 'success': {fn:function() { this.tree.refreshParentNode(); },scope:this}
  215. }
  216. });
  217. } else {
  218. this.windows.zipUpload.fp.getForm().reset();
  219. }
  220. this.windows.zipUpload.setValues(r);
  221. this.windows.zipUpload.show(e.target);
  222. }
  223. ,handleDrop: function(t,dropEvent) {
  224. this.tree = t;
  225. var dropNode = dropEvent.dropNode;
  226. var target = dropEvent.target;
  227. if (!target.attributes.type) return false;
  228. if (target.attributes.type == 'gallery-album' && dropNode.attributes.type == 'gallery-item' && dropEvent.point != 'append') return false;
  229. if (dropNode.attributes.type == 'gallery-album' && target.attributes.type == 'gallery-item') return false;
  230. return true;
  231. }
  232. });
  233. Ext.reg('gal-tree-handler',galTreeHandlerClass);
  234. var galTreeHandler = new galTreeHandlerClass();
  235. var galItemDropHandlerClass = function(config) {
  236. config = config || {};
  237. Ext.apply(config,{
  238. id: 'gallery-item-drop-handler'
  239. });
  240. galItemDropHandlerClass.superclass.constructor.call(this,config);
  241. };
  242. Ext.extend(galItemDropHandlerClass,Ext.Component,{
  243. handle: function(target,opt) {
  244. var na = target.node.attributes;
  245. var cfg = {
  246. ddTargetEl: opt.ddTargetEl
  247. ,cfg: opt.cfg
  248. ,iframe: opt.cfg.iframe
  249. ,iframeEl: opt.cfg.iframeEl
  250. ,onInsert: opt.cfg.onInsert
  251. ,panel: opt.cfg.panel
  252. ,classKey: 'modSnippet'
  253. ,pk: GAL.snippetGalleryItem
  254. ,name: 'GalleryItem'
  255. };
  256. GAL.elProps = {
  257. id: na.data.id
  258. };
  259. MODx.loadInsertElement(cfg);
  260. setTimeout("galTreeWorkaround(GAL.elProps);",600);
  261. return true;
  262. }
  263. });
  264. var galItemDropHandler = new galItemDropHandlerClass();
  265. var galAlbumDropHandlerClass = function(config) {
  266. config = config || {};
  267. Ext.apply(config,{
  268. id: 'gallery-album-drop-handler'
  269. });
  270. galAlbumDropHandlerClass.superclass.constructor.call(this,config);
  271. };
  272. Ext.extend(galAlbumDropHandlerClass,Ext.Component,{
  273. handle: function(target,opt) {
  274. var na = target.node.attributes;
  275. var cfg = {
  276. ddTargetEl: opt.ddTargetEl
  277. ,cfg: opt.cfg
  278. ,iframe: opt.cfg.iframe
  279. ,iframeEl: opt.cfg.iframeEl
  280. ,onInsert: opt.cfg.onInsert
  281. ,panel: opt.cfg.panel
  282. ,classKey: 'modSnippet'
  283. ,pk: GAL.snippetGallery
  284. ,name: 'Gallery'
  285. };
  286. GAL.elProps = {
  287. album: na.data.id
  288. };
  289. MODx.loadInsertElement(cfg);
  290. setTimeout("galTreeWorkaround(GAL.elProps);",600);
  291. return true;
  292. }
  293. });
  294. var galAlbumDropHandler = new galAlbumDropHandlerClass();
  295. /* because im an idiot and forgot win show events in the insert element window */
  296. function galTreeWorkaround(props) {
  297. var w = Ext.getCmp('modx-window-insert-element');
  298. w.modps = [];
  299. if (w) {
  300. for (var k in props) {
  301. var fld = Ext.getCmp('modx-iprop-'+k);
  302. if (fld) {
  303. fld.setValue(props[k]);
  304. w.changeProp(k);
  305. }
  306. }
  307. }
  308. };
  309. /* because im an idiot and prevented drag/drop of non-leaf nodes in modx core */
  310. setTimeout('galleryFixLeafDrag();',1200);
  311. function galleryFixLeafDrag() {
  312. var t = Ext.getCmp('modx-file-tree');
  313. if (t) {
  314. t.on('startdrag',function(t,n,e) {
  315. if (n.attributes.type == 'gallery-album') {
  316. n.attributes.leaf = true;
  317. }
  318. },this);
  319. }
  320. }