modx.grid.template.tv.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Loads a grid of TVs assigned to the Template.
  3. *
  4. * @class MODx.grid.TemplateTV
  5. * @extends MODx.grid.Grid
  6. * @param {Object} config An object of options.
  7. * @xtype modx-grid-template-tv
  8. */
  9. MODx.grid.TemplateTV = function(config) {
  10. config = config || {};
  11. var tt = new Ext.ux.grid.CheckColumn({
  12. header: _('access')
  13. ,dataIndex: 'access'
  14. ,width: 70
  15. ,sortable: true
  16. });
  17. Ext.applyIf(config,{
  18. title: _('template_assignedtv_tab')
  19. ,id: 'modx-grid-template-tv'
  20. ,url: MODx.config.connector_url
  21. ,fields: ['id','name','description','tv_rank','access','perm','category_name','category']
  22. ,baseParams: {
  23. action: 'element/template/tv/getlist'
  24. ,template: config.template
  25. ,sort: 'tv_rank'
  26. }
  27. ,saveParams: {
  28. template: config.template
  29. }
  30. ,width: 800
  31. ,paging: true
  32. ,plugins: tt
  33. ,remoteSort: true
  34. ,sortBy: 'category_name, tv_rank'
  35. ,grouping: true
  36. ,groupBy: 'category_name'
  37. ,singleText: _('tv')
  38. ,pluralText: _('tvs')
  39. ,enableDragDrop: true
  40. ,ddGroup : 'template-tvs-ddsort'
  41. ,sm: new Ext.grid.RowSelectionModel({
  42. singleSelect: true
  43. ,listeners: {
  44. beforerowselect: function(sm, idx, keep, record) {
  45. sm.grid.ddText = '<div>'+ record.data.name +'</div>';
  46. }
  47. }
  48. })
  49. ,columns: [{
  50. header: _('name')
  51. ,dataIndex: 'name'
  52. ,width: 150
  53. ,editor: { xtype: 'textfield' ,allowBlank: false }
  54. ,sortable: true
  55. },{
  56. header: _('category')
  57. ,dataIndex: 'category_name'
  58. ,width: 150
  59. ,sortable: true
  60. },{
  61. header: _('description')
  62. ,dataIndex: 'description'
  63. ,width: 350
  64. ,editor: { xtype: 'textfield' }
  65. ,sortable: false
  66. },tt,{
  67. header: _('rank')
  68. ,dataIndex: 'tv_rank'
  69. ,width: 100
  70. ,editor: { xtype: 'textfield' ,allowBlank: false }
  71. ,sortable: true
  72. }]
  73. ,tbar: ['->',{
  74. xtype: 'modx-combo-category'
  75. ,name: 'filter_category'
  76. ,hiddenName: 'filter_category'
  77. ,id: 'modx-temptv-filter-category'
  78. ,emptyText: _('filter_by_category')
  79. ,value: ''
  80. ,allowBlank: true
  81. ,width: 150
  82. ,listeners: {
  83. 'select': {fn: this.filterByCategory, scope:this}
  84. }
  85. },{
  86. xtype: 'textfield'
  87. ,name: 'search'
  88. ,id: 'modx-temptv-search'
  89. ,cls: 'x-form-filter'
  90. ,emptyText: _('search_ellipsis')
  91. ,listeners: {
  92. 'change': {fn: this.search, scope: this}
  93. ,'render': {fn: function(cmp) {
  94. new Ext.KeyMap(cmp.getEl(), {
  95. key: Ext.EventObject.ENTER
  96. ,fn: this.blur
  97. ,scope: cmp
  98. });
  99. },scope:this}
  100. }
  101. },{
  102. xtype: 'button'
  103. ,id: 'modx-filter-clear'
  104. ,cls: 'x-form-filter-clear'
  105. ,text: _('filter_clear')
  106. ,listeners: {
  107. 'click': {fn: this.clearFilter, scope: this},
  108. 'mouseout': { fn: function(evt){
  109. this.removeClass('x-btn-focus');
  110. }
  111. }
  112. }
  113. }]
  114. });
  115. MODx.grid.TemplateTV.superclass.constructor.call(this,config);
  116. this.on('render', this.prepareDDSort, this);
  117. };
  118. Ext.extend(MODx.grid.TemplateTV,MODx.grid.Grid,{
  119. getMenu: function() {
  120. var r = this.getSelectionModel().getSelected();
  121. var p = r.data.perm;
  122. var m = [];
  123. if (p.indexOf('pedit') != -1) {
  124. m.push({
  125. text: _('edit_tv')
  126. ,handler: this.updateTV
  127. });
  128. }
  129. return m;
  130. }
  131. ,updateTV: function(itm,e) {
  132. MODx.loadPage('element/tv/update', 'id='+this.menu.record.id);
  133. }
  134. ,filterByCategory: function(cb,rec,ri) {
  135. this.getStore().baseParams['category'] = cb.getValue();
  136. this.getBottomToolbar().changePage(1);
  137. //this.refresh();
  138. }
  139. ,search: function(tf,newValue,oldValue) {
  140. var nv = newValue || tf;
  141. this.getStore().baseParams.search = Ext.isEmpty(nv) || Ext.isObject(nv) ? '' : nv;
  142. Ext.getCmp('modx-temptv-filter-category').setValue('');
  143. this.getBottomToolbar().changePage(1);
  144. //this.refresh();
  145. return true;
  146. }
  147. ,clearFilter: function() {
  148. this.getStore().baseParams = {
  149. action: 'element/template/tv/getList'
  150. ,template: this.config.template
  151. };
  152. Ext.getCmp('modx-temptv-filter-category').reset();
  153. Ext.getCmp('modx-temptv-search').setValue('');
  154. this.getBottomToolbar().changePage(1);
  155. //this.refresh();
  156. }
  157. ,prepareDDSort: function(grid) {
  158. this.dropTarget = new Ext.dd.DropTarget(grid.getView().mainBody, {
  159. ddGroup: 'template-tvs-ddsort'
  160. ,copy: false
  161. ,notifyOver: function(dragSource, e, data) {
  162. if (dragSource.getDragData(e)) {
  163. var targetNode = dragSource.getDragData(e).selections[0];
  164. var sourceNode = data.selections[0];
  165. if ((sourceNode.data['category_name'] != targetNode.data['category_name']) ||
  166. !sourceNode.data['access'] ||
  167. !targetNode.data['access'] ||
  168. (sourceNode.data['id'] == targetNode.data['id'])
  169. ) {
  170. return this.dropNotAllowed;
  171. }
  172. return this.dropAllowed;
  173. }
  174. return this.dropNotAllowed;
  175. }
  176. ,notifyDrop : function(dragSource, e, data) {
  177. if (dragSource.getDragData(e)) {
  178. var targetNode = dragSource.getDragData(e).selections[0];
  179. var sourceNode = data.selections[0];
  180. if ((targetNode.id != sourceNode.id) &&
  181. (targetNode.get('category_name') === sourceNode.get('category_name')) &&
  182. sourceNode.get('access')
  183. ) {
  184. grid.sortTVs(sourceNode, targetNode);
  185. }
  186. }
  187. }
  188. });
  189. }
  190. ,sortTVs: function(sourceNode, targetNode) {
  191. var store = this.getStore();
  192. var sourceIdx = store.indexOf(sourceNode);
  193. var targetIdx = store.indexOf(targetNode);
  194. // Insert the selection to the target (and remove original selection)
  195. store.removeAt(sourceIdx);
  196. store.insert(targetIdx, sourceNode);
  197. // Extract the store items with the same category_name as the sourceNode to start the index at 0 for each category
  198. var filteredStore = store.queryBy(function(rec, id) {
  199. if (rec.get('category_name') === sourceNode.get('category_name')) {
  200. return true;
  201. }
  202. return false;
  203. }, this);
  204. // Loop trough the filtered store and re-apply the re-calculated ranks to the store records
  205. Ext.each(filteredStore.items, function(item, index, allItems) {
  206. if (sourceNode.get('category_name') === item.get('category_name')) {
  207. var record = store.getById(item.id);
  208. record.set('tv_rank', index);
  209. }
  210. }, this);
  211. }
  212. });
  213. Ext.reg('modx-grid-template-tv',MODx.grid.TemplateTV);