/js/ecart/admin/template/box.js

https://code.google.com/p/ecartcommerce/ · JavaScript · 325 lines · 285 code · 18 blank · 22 comment · 19 complexity · 59be683081f90ac82ba650baaa93fa26 MD5 · raw file

  1. /**
  2. * Ecart
  3. *
  4. * This file is part of Ecart.
  5. *
  6. * Ecart is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Ecart is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Ecart. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @copyright Copyright 2008-2009 E-Cart LLC
  20. * @license GNU Public License V3.0
  21. */
  22. Ext.namespace('Ecart', 'Ecart.Template', 'Ecart.Template.Box');
  23. Ext.onReady(function(){
  24. Ext.apply(Ecart.Template.Box, {
  25. templateId: 0,
  26. record: Ext.data.Record.create([
  27. {name: 'id', type: 'int'},
  28. {name: 'block'},
  29. {name: 'box_status', type: 'int'},
  30. {name: 'class'},
  31. {name: 'sort_order', type: 'int'},
  32. {name: 'show'}
  33. ]),
  34. getSelectedId: function() {
  35. var selectedItems = grid.getSelectionModel().selections.items;
  36. if (!selectedItems.length || !selectedItems[0]['data']['id']) {
  37. return false;
  38. }
  39. return selectedItems[0].id;
  40. },
  41. loadGrid: function(templateId) {
  42. Box.templateId = templateId;
  43. ds.commitChanges();
  44. ds.load({params: {tId: templateId}});
  45. },
  46. create: function() {
  47. if (!Box.templateId)
  48. return alert('Select template on the left');
  49. grid.stopEditing();
  50. var record = new Box.record({
  51. 'block': 'content',
  52. 'box_status': 0,
  53. 'class': '',
  54. 'sort_order': 100,
  55. 'show': ''
  56. });
  57. ds.insert(0, record);
  58. grid.startEditing(0, 1);
  59. },
  60. edit: function(id) {
  61. if (!id) {
  62. return;
  63. }
  64. Box.window.show();
  65. Box.window.load({
  66. url: Ecart.getUrl('template_box/edit/'),
  67. params: {
  68. boxId: id,
  69. tId: Box.templateId
  70. },
  71. callback: function() {
  72. Ext.ux.Table.colorize();
  73. $(':checkbox', '#form-box').bind('click', function(){
  74. var type = $(this).attr('id').substring(0, 4);
  75. var pageId = $(this).attr('id').substring(5);
  76. if (type == 'hide'){
  77. $('#show-' + pageId).removeAttr('checked');
  78. } else{
  79. $('#hide-' + pageId).removeAttr('checked');
  80. }
  81. });
  82. }
  83. });
  84. },
  85. remove: function() {
  86. var selectedItems = grid.getSelectionModel().selections.items;
  87. if (!selectedItems.length || !confirm('Are you sure?'.l())) {
  88. return;
  89. }
  90. var data = {};
  91. for (var i = 0; i < selectedItems.length; i++) {
  92. if (!selectedItems[i]['data']['id']) {
  93. continue;
  94. }
  95. data[i] = selectedItems[i]['data']['id'];
  96. }
  97. Ext.Ajax.request({
  98. url: Ecart.getUrl('template_box/delete'),
  99. params: {data: Ext.encode(data)},
  100. callback: function() {
  101. ds.reload();
  102. }
  103. });
  104. },
  105. save: function() {
  106. Ext.Ajax.request({
  107. url: $('#form-box').get(0).action,
  108. form: 'form-box',
  109. success: function() {
  110. Box.window.hide();
  111. ds.reload();
  112. }
  113. });
  114. },
  115. saveMulti: function() {
  116. var data = {};
  117. var modified = ds.getModifiedRecords();
  118. if (!modified.length)
  119. return;
  120. for (var i = 0; i < modified.length; i++) {
  121. data[modified[i]['id']] = modified[i]['data'];
  122. }
  123. Ext.Ajax.request({
  124. url: Ecart.getUrl('template_box/batch-save'),
  125. params: {
  126. data: Ext.encode(data),
  127. tId: Box.templateId
  128. },
  129. callback: function() {
  130. ds.commitChanges();
  131. ds.reload();
  132. }
  133. });
  134. },
  135. window: new Ext.Window({
  136. layout: 'fit',
  137. width: 650,
  138. height: 500,
  139. constrain: true,
  140. maximizable: true,
  141. closeAction: 'hide',
  142. bodyStyle:'background: white',
  143. title: 'Box'.l(),
  144. autoScroll:true,
  145. buttons: [{
  146. text: 'Save'.l(),
  147. handler: function() {
  148. Box.save()
  149. }
  150. },{
  151. text: 'Cancel'.l(),
  152. handler: function(){
  153. Box.window.hide();
  154. }
  155. }]
  156. })
  157. });
  158. var Box = Ecart.Template.Box;
  159. var ds = new Ext.data.Store({
  160. proxy: new Ext.data.HttpProxy({
  161. url: Ecart.getUrl('template_box/list')
  162. }),
  163. reader: new Ext.data.JsonReader({
  164. root: 'data',
  165. id: 'id'
  166. }, Box.record)
  167. });
  168. var dsPages = new Ext.data.Store({
  169. data: Ecart.pages,
  170. reader: new Ext.data.JsonReader({
  171. idProperty: 'id',
  172. fields: [
  173. {name: 'id', type: 'int'},
  174. {name: 'name'}
  175. ]
  176. })
  177. });
  178. var selectModel = new Ext.grid.CheckboxSelectionModel();
  179. var status = new Ecart.grid.CheckColumn({
  180. header: 'Status'.l(),
  181. width: 50,
  182. dataIndex: 'box_status'
  183. });
  184. var actions = new Ext.ux.grid.RowActions({
  185. header:'Actions'.l(),
  186. actions:[{
  187. iconCls:'icon-edit',
  188. tooltip:'Edit'.l()
  189. }],
  190. callbacks: {
  191. 'icon-edit': function(grid, record, action, row, col) {
  192. Box.edit(record.id);
  193. }
  194. }
  195. });
  196. var cm = new Ext.grid.ColumnModel([
  197. selectModel, {
  198. header: "Box".l(),
  199. dataIndex: 'class',
  200. sortable: true,
  201. mode: 'local',
  202. width: 220,
  203. editor: new Ext.form.ComboBox({
  204. typeAhead: true,
  205. triggerAction: 'all',
  206. lazyRender: true,
  207. store: Box.classes,
  208. mode: 'local'
  209. })
  210. }, {
  211. header: 'Layout block'.l(),
  212. dataIndex: 'block',
  213. editor: new Ext.form.TextField({
  214. allowBlank: false
  215. })
  216. }, {
  217. header: "Show on".l(),
  218. sortable: true,
  219. dataIndex: 'show',
  220. width: 200,
  221. editor: new Ext.ux.Andrie.Select({
  222. multiSelect: true,
  223. store: dsPages,
  224. valueField: 'id',
  225. displayField: 'name',
  226. triggerAction: 'all',
  227. mode: 'local'
  228. })
  229. ,
  230. renderer: function(value, meta) {
  231. if (typeof(value) == 'undefined' || value == '') {
  232. return 'None'.l();
  233. }
  234. var ret = new Array();
  235. value = value.split(',');
  236. for (var i = 0, n = value.length; i < n; i++) {
  237. if (value[i] != '') {
  238. ret.push(dsPages.getById(value[i]).data.name);
  239. }
  240. }
  241. ret = ret.join(', ');
  242. meta.attr = 'ext:qtip="Used on pages : ' + ret + '"';
  243. return ret;
  244. }
  245. }, {
  246. header: 'Sort Order'.l(),
  247. width: 80,
  248. dataIndex: 'sort_order',
  249. editor: new Ext.form.TextField({
  250. allowBlank: false
  251. })
  252. },
  253. status,
  254. actions
  255. ]);
  256. var grid = new Ext.grid.EditorGridPanel({
  257. title: 'Boxes'.l(),
  258. ds: ds,
  259. cm: cm,
  260. viewConfig: {
  261. forceFit:true,
  262. emptyText: 'No records found'.l()
  263. },
  264. selModel: selectModel,
  265. clicksToEdit: 1,
  266. plugins: [
  267. status,
  268. actions,
  269. new Ext.ux.grid.Search({
  270. mode: 'local',
  271. align: 'left',
  272. iconCls: false,
  273. dateFormat: 'Y-m-d',
  274. width: 200,
  275. minLength: 2
  276. })
  277. ],
  278. bbar: [],
  279. tbar: [{
  280. text: 'Add'.l(),
  281. icon: Ecart.skinUrl + '/images/icons/add.png',
  282. cls: 'x-btn-text-icon',
  283. handler: Box.create
  284. }, {
  285. text: 'Save'.l(),
  286. icon: Ecart.skinUrl + '/images/icons/save_multiple.png',
  287. cls: 'x-btn-text-icon',
  288. handler: Box.saveMulti
  289. }, {
  290. text: 'Delete'.l(),
  291. icon: Ecart.skinUrl + '/images/icons/delete.png',
  292. cls: 'x-btn-text-icon',
  293. handler: Box.remove
  294. }, '->', {
  295. icon: Ecart.skinUrl + '/images/icons/refresh.png',
  296. cls: 'x-btn-icon',
  297. handler: function() {
  298. grid.getStore().reload();
  299. }
  300. }]
  301. });
  302. grid.on('rowdblclick', function(grid, rowIndex, e) {
  303. //Ext.getCmp('window').show();
  304. Box.edit(grid.getStore().getAt(rowIndex).get('id'));
  305. })
  306. Box.grid = grid;
  307. })