modx.rte.browser.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. MODx.browser.RTE = function(config) {
  2. config = config || {};
  3. this.ident = Ext.id();
  4. Ext.Ajax.defaultHeaders = {
  5. 'modAuth': config.auth
  6. };
  7. Ext.Ajax.extraParams = {
  8. 'HTTP_MODAUTH': config.auth
  9. };
  10. this.ident = 'modx-browser-'+Ext.id();
  11. this.view = MODx.load({
  12. xtype: 'modx-browser-view'
  13. ,onSelect: {fn: this.onSelect, scope: this}
  14. ,ident: this.ident
  15. ,source: config.source || MODx.config.default_media_source
  16. ,id: this.ident+'-view'
  17. });
  18. MODx.browserOpen = true;
  19. this.tree = MODx.load({
  20. xtype: 'modx-tree-directory'
  21. ,onUpload: function() { this.view.run(); }
  22. ,scope: this
  23. ,source: config.source || MODx.config.default_media_source
  24. ,useDefaultToolbar: false
  25. ,hideFiles: true
  26. ,openTo: config.openTo || ''
  27. ,ident: this.ident
  28. ,rootId: '/'
  29. ,rootName: _('files')
  30. ,rootVisible: true
  31. ,id: this.ident+'-tree'
  32. ,listeners: {
  33. 'afterUpload': {fn:function() { this.view.run(); },scope:this}
  34. ,'changeSource': {fn:function(s) {
  35. this.config.source = s;
  36. this.view.config.source = s;
  37. this.view.baseParams.source = s;
  38. this.view.dir = '/';
  39. this.view.run();
  40. },scope:this}
  41. ,afterrender: {
  42. fn: function(tree) {
  43. tree.root.expand();
  44. }
  45. ,scope: this
  46. }
  47. }
  48. });
  49. this.tree.on('click',function(node,e) {
  50. this.load(node.id);
  51. },this);
  52. Ext.applyIf(config,{
  53. title: _('modx_browser')
  54. ,layout: 'border'
  55. ,renderTo: document.body
  56. ,id: this.ident+'-viewport'
  57. ,onSelect: MODx.onBrowserReturn || function(data) {}
  58. ,items: [{
  59. id: this.ident+'-browser-tree'
  60. ,cls: 'modx-browser-tree'
  61. ,region: 'west'
  62. ,width: 250
  63. ,height: '100%'
  64. ,split: true
  65. ,items: this.tree
  66. ,autoScroll: true
  67. },{
  68. id: this.ident+'-browser-view'
  69. ,cls: 'modx-browser-view-ct'
  70. ,region: 'center'
  71. ,autoScroll: true
  72. ,width: 450
  73. ,items: this.view
  74. ,tbar: this.getToolbar()
  75. },{
  76. id: this.ident+'-img-detail-panel'
  77. ,cls: 'modx-browser-details-ct'
  78. ,region: 'east'
  79. ,split: true
  80. ,width: 200
  81. ,minWidth: 200
  82. ,maxWidth: 300
  83. },{
  84. id: this.ident+'-south'
  85. ,cls: 'modx-browser-buttons'
  86. ,region: 'south'
  87. ,split: false
  88. ,bbar: ['->',{
  89. id: this.ident+'-ok-btn'
  90. ,text: _('ok')
  91. ,handler: this.onSelect
  92. ,scope: this
  93. ,width: 200
  94. },{
  95. text: _('cancel')
  96. ,handler: this.closeWindow
  97. ,scope: this
  98. ,width: 200
  99. }]
  100. }]
  101. });
  102. MODx.browser.RTE.superclass.constructor.call(this,config);
  103. this.config = config;
  104. };
  105. Ext.extend(MODx.browser.RTE,Ext.Viewport,{
  106. filter : function(){
  107. var filter = Ext.getCmp('filter');
  108. this.view.store.filter('name', filter.getValue(),true);
  109. this.view.select(0);
  110. }
  111. ,setReturn: function(el) {
  112. this.returnEl = el;
  113. }
  114. ,load: function(dir) {
  115. dir = dir || '';
  116. var t = Ext.getCmp(this.ident+'-tree');
  117. if (t) {
  118. this.config.source = t.config.baseParams.source;
  119. }
  120. this.view.run({
  121. dir: dir
  122. ,wctx: MODx.ctx
  123. ,source: this.config.source || MODx.config.default_media_source
  124. });
  125. }
  126. ,sortStore: function(){
  127. var v = Ext.getCmp(this.ident+'sortSelect').getValue();
  128. this.view.store.sort(v, v == 'name' ? 'ASC' : 'DESC');
  129. this.view.select(0);
  130. }
  131. ,changeViewmode: function() {
  132. var v = Ext.getCmp(this.ident+'viewSelect').getValue();
  133. this.view.setTemplate(v);
  134. this.view.select(0);
  135. }
  136. ,reset: function(){
  137. if(this.rendered){
  138. Ext.getCmp('filter').reset();
  139. this.view.getEl().dom.scrollTop = 0;
  140. }
  141. this.view.store.clearFilter();
  142. this.view.select(0);
  143. }
  144. ,getToolbar: function() {
  145. return [{
  146. text: _('filter')+':'
  147. },{
  148. xtype: 'textfield'
  149. ,id: 'filter'
  150. ,selectOnFocus: true
  151. ,width: 200
  152. ,listeners: {
  153. 'render': {fn:function(){
  154. Ext.getCmp('filter').getEl().on('keyup', function(){
  155. this.filter();
  156. }, this, {buffer:500});
  157. }, scope:this}
  158. }
  159. }, ' ', {
  160. text: _('sort_by')+':'
  161. }, {
  162. id: this.ident+'sortSelect'
  163. ,xtype: 'combo'
  164. ,typeAhead: true
  165. ,triggerAction: 'all'
  166. ,width: 100
  167. ,editable: false
  168. ,mode: 'local'
  169. ,displayField: 'desc'
  170. ,valueField: 'name'
  171. ,lazyInit: false
  172. ,value: MODx.config.modx_browser_default_sort || 'name'
  173. ,store: new Ext.data.SimpleStore({
  174. fields: ['name', 'desc'],
  175. data : [['name',_('name')],['size',_('file_size')],['lastmod',_('last_modified')]]
  176. })
  177. ,listeners: {
  178. 'select': {fn:this.sortStore, scope:this}
  179. }
  180. },{
  181. icon: MODx.config.template_url+'images/restyle/icons/refresh.png'
  182. ,cls: 'x-btn-icon'
  183. ,tooltip: {text: _('tree_refresh')}
  184. ,handler: function() { this.load(); }
  185. ,scope: this
  186. }, '-', {
  187. text: _('files_viewmode')+':'
  188. ,xtype: 'label'
  189. }, '-', {
  190. id: this.ident+'viewSelect'
  191. ,xtype: 'combo'
  192. ,typeAhead: false
  193. ,triggerAction: 'all'
  194. ,width: 100
  195. ,editable: false
  196. ,mode: 'local'
  197. ,displayField: 'desc'
  198. ,valueField: 'type'
  199. ,lazyInit: false
  200. ,value: MODx.config.modx_browser_default_viewmode || 'grid'
  201. ,store: new Ext.data.SimpleStore({
  202. fields: ['type', 'desc'],
  203. data : [['grid', _('files_viewmode_grid')],['list', _('files_viewmode_list')]]
  204. })
  205. ,listeners: {
  206. 'select': {fn:this.changeViewmode, scope:this}
  207. }
  208. }];
  209. }
  210. ,onSelect: function(data) {
  211. var selNode = this.view.getSelectedNodes()[0];
  212. var callback = this.config.onSelect || this.onSelectHandler;
  213. var lookup = this.view.lookup;
  214. var scope = this.config.scope;
  215. if(selNode && callback) {
  216. data = lookup[selNode.id];
  217. Ext.callback(callback,scope || this,[data]);
  218. this.fireEvent('select',data);
  219. if (window.top.opener) {
  220. window.top.close();
  221. window.top.opener.focus();
  222. }
  223. }
  224. }
  225. ,onSelectHandler: function(data) {
  226. Ext.get(this.returnEl).dom.value = unescape(data.url);
  227. }
  228. ,closeWindow: function () {
  229. var callback = this.config.onSelect || this.onSelectHandler;
  230. var scope = this.config.scope;
  231. Ext.callback(callback,scope || this,[null]);
  232. this.fireEvent('select',null);
  233. }
  234. });
  235. Ext.reg('modx-browser-rte',MODx.browser.RTE);