modx.view.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Abstract class for Ext.DataView creation in MODx
  3. *
  4. * This is primarily used for the media browser
  5. * but in untestable cases also in a modx-package-browser-thumbs-view
  6. * in the package browser (when a node in the tree has the attribute "templated")
  7. *
  8. * @class MODx.DataView
  9. * @extends Ext.DataView
  10. * @constructor
  11. * @param {Object} config An object of options.
  12. * @xtype modx-dataview
  13. */
  14. MODx.DataView = function(config) {
  15. config = config || {};
  16. this._loadStore(config);
  17. Ext.applyIf(config.listeners || {},{
  18. 'loadexception': {fn:this.onLoadException, scope: this}
  19. ,'beforeselect': {fn:function(view){ return view.store.getRange().length > 0;}}
  20. ,'contextmenu': {fn:this._showContextMenu, scope: this}
  21. });
  22. Ext.applyIf(config,{
  23. store: this.store
  24. ,singleSelect: true
  25. ,overClass: 'x-view-over'
  26. ,emptyText: '<div style="padding:10px;">'+_('file_err_filter')+'</div>'
  27. ,closeAction: 'hide'
  28. });
  29. MODx.DataView.superclass.constructor.call(this,config);
  30. this.config = config;
  31. this.cm = new Ext.menu.Menu();
  32. };
  33. Ext.extend(MODx.DataView,Ext.DataView,{
  34. lookup: {}
  35. ,onLoadException: function(){
  36. this.getEl().update('<div style="padding:10px;">'+_('data_err_load')+'</div>');
  37. }
  38. /**
  39. * Add context menu items to the dataview.
  40. * @param {Object, Array} items Either an Object config or array of Object configs.
  41. */
  42. ,_addContextMenuItem: function(items) {
  43. var a = items, l = a.length;
  44. for(var i=0;i<l;i=i+1) {
  45. var options = a[i];
  46. if (options === '-') {
  47. this.cm.add('-');
  48. continue;
  49. }
  50. var h = Ext.emptyFn;
  51. if (options.handler) {
  52. h = eval(options.handler);
  53. } else {
  54. h = function(itm,e) {
  55. var o = itm.options;
  56. var id = this.cm.activeNode.id.split('_'); id = id[1];
  57. var w = Ext.get('modx_content');
  58. if (o.confirm) {
  59. Ext.Msg.confirm('',o.confirm,function(e) {
  60. if (e === 'yes') {
  61. var a = Ext.urlEncode(o.params || {action: o.action});
  62. var s = '?id='+id+'&'+a;
  63. if (w === null) {
  64. location.href = s;
  65. } else { w.dom.src = s; }
  66. }
  67. },this);
  68. } else {
  69. var a = Ext.urlEncode(o.params);
  70. var s = '?id='+id+'&'+a;
  71. if (w === null) {
  72. location.href = s;
  73. } else { w.dom.src = s; }
  74. }
  75. };
  76. }
  77. this.cm.add({
  78. id: options.id
  79. ,text: options.text
  80. ,scope: this
  81. ,options: options
  82. ,handler: h
  83. });
  84. }
  85. }
  86. ,_loadStore: function(config) {
  87. this.store = new Ext.data.JsonStore({
  88. url: config.url
  89. ,baseParams: config.baseParams || {
  90. action: 'browser/directory/getList'
  91. ,wctx: config.wctx || MODx.ctx
  92. ,dir: config.openTo || ''
  93. ,source: config.source || 0
  94. }
  95. ,root: config.root || 'results'
  96. ,fields: config.fields
  97. ,totalProperty: 'total'
  98. ,listeners: {
  99. 'load': {fn:function(){ this.select(0); }, scope:this, single:true}
  100. }
  101. });
  102. this.store.load();
  103. }
  104. ,_showContextMenu: function(v,i,n,e) {
  105. e.preventDefault();
  106. var data = this.lookup[n.id];
  107. var m = this.cm;
  108. m.removeAll();
  109. if (data.menu) {
  110. this._addContextMenuItem(data.menu);
  111. m.showAt(e.xy);
  112. }
  113. m.activeNode = n;
  114. }
  115. });
  116. Ext.reg('modx-dataview',MODx.DataView);