Advertisement
Guest User

imagemenu-imagemenu.js

a guest
Feb 24th, 2014
4,550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.33 KB | None | 0 0
  1. //test
  2. Ext.define('Shopware.apps.Emotion.view.components.Imagemenu', {
  3.     extend: 'Shopware.apps.Emotion.view.components.Base',
  4.     alias: 'widget.emotion-imagemenu',
  5.     /**
  6.      * Snippets for the component.
  7.      * @object
  8.      */
  9.     snippets: {
  10.         'select_imagemenu': '{s name=select_images_for_menu}Select image(s){/s}',
  11.         'imagemenu_administration': '{s name=imagemenu_administration}Imagemenu administration{/s}',
  12.         'path': '{s name=path}Image path{/s}',
  13.         'actions': '{s name=actions}Action(s){/s}',
  14.         'link': '{s name=link}Link{/s}',
  15.         'altText': '{s name=altText}Alternative text{/s}',
  16.         'title': '{s name=title}Title{/s}',
  17.  
  18.         imagemenu_title: '{s name=imagemenu_title}Title{/s}',
  19.         imagemenu_arrows: '{s name=imagemenu_arrows}Display arrows{/s}',
  20.         imagemenu_numbers: '{s name=imagemenu_numbers}Display numbers{/s}',
  21.         imagemenu_scrollspeed: '{s name=imagemenu_scrollspeed}Scroll speed{/s}',
  22.         imagemenu_rotation: '{s name=imagemenu_rotation}Rotate automatically{/s}',
  23.         imagemenu_rotatespeed: '{s name=imagemenu_rotatespeed}Rotation speed{/s}'
  24.     },
  25.  
  26.     /**
  27.      * Initiliaze the component.
  28.      *
  29.      * @public
  30.      * @return void
  31.      */
  32.     initComponent: function() {
  33.         var me = this;
  34.         me.callParent(arguments);
  35.  
  36.         me.setDefaultValues();
  37.         me.add(me.createImagemenuFieldset());
  38.         me.getGridData();
  39.         me.refreshHiddenValue();
  40.     },
  41.  
  42.     /**
  43.      * Sets default values if the imagemenu
  44.      * wasn't saved previously.
  45.      *
  46.      * @public
  47.      * @return void
  48.      */
  49.     setDefaultValues: function() {
  50.         var me = this,
  51.             numberfields =  me.query('numberfield');
  52.  
  53.         Ext.each(numberfields, function(field) {
  54.             if(!field.getValue()) {
  55.                 field.setValue(500);
  56.             }
  57.         });
  58.     },
  59.  
  60.     /**
  61.      * Creates the fieldset which holds the imagemenu administration. The method
  62.      * also creates the imagemenu store and registers the drag and drop plugin
  63.      * for the grid.
  64.      *
  65.      * @public
  66.      * @return [object] Ext.form.FieldSet
  67.      */
  68.     createImagemenuFieldset: function() {
  69.         var me = this;
  70.  
  71.         me.mediaSelection = Ext.create('Shopware.form.field.MediaSelection', {
  72.             fieldLabel: me.snippets.select_imagemenu,
  73.             labelWidth: 155,
  74.             listeners: {
  75.                 scope: me,
  76.                 selectMedia: me.onAddImagemenuToGrid
  77.             }
  78.         });
  79.  
  80.         me.imagemenuStore = Ext.create('Ext.data.Store', {
  81.             fields: [ 'position', 'path', 'link', 'altText', 'title', 'mediaId' ]
  82.         });
  83.  
  84.         me.ddGridPlugin = Ext.create('Ext.grid.plugin.DragDrop');
  85.  
  86.         me.cellEditing = Ext.create('Ext.grid.plugin.RowEditing', {
  87.             clicksToEdit: 2
  88.         });
  89.  
  90.         me.imagemenuGrid = Ext.create('Ext.grid.Panel', {
  91.             columns: me.createColumns(),
  92.             autoScroll: true,
  93.             store: me.imagemenuStore,
  94.             height: 200,
  95.             plugins: [ me.cellEditing ],
  96.             viewConfig: {
  97.                 plugins: [ me.ddGridPlugin ],
  98.                 listeners: {
  99.                     scope: me,
  100.                     drop: me.onRepositionImagemenu
  101.                 }
  102.             },
  103.             listeners: {
  104.                 scope: me,
  105.                 edit: function() {
  106.                     me.refreshHiddenValue();
  107.                 }
  108.             }
  109.         });
  110.  
  111.         return me.imagemenuFieldset = Ext.create('Ext.form.FieldSet', {
  112.             title: me.snippets.imagemenu_administration,
  113.             layout: 'anchor',
  114.             defaults: { anchor: '100%' },
  115.             items: [ me.mediaSelection, me.imagemenuGrid ]
  116.         });
  117.     },
  118.  
  119.     /**
  120.      * Helper method which creates the column model
  121.      * for the imagemenu administration grid panel.
  122.      *
  123.      * @public
  124.      * @return [array] computed columns
  125.      */
  126.     createColumns: function() {
  127.         var me = this, snippets = me.snippets;
  128.  
  129.         return [{
  130.             header: '⚌',
  131.             width: 24,
  132.             hideable: false,
  133.             renderer : me.renderSorthandleColumn
  134.         }, {
  135.             dataIndex: 'path',
  136.             header: snippets.path,
  137.             flex: 1
  138.         }, {
  139.             dataIndex: 'link',
  140.             header: snippets.link,
  141.             flex: 1,
  142.             editor: {
  143.                 xtype: 'textfield',
  144.                 allowBlank: true
  145.             }
  146.         }, {
  147.             dataIndex: 'altText',
  148.             header: snippets.altText,
  149.             flex: 1,
  150.             editor: {
  151.                 xtype: 'textfield',
  152.                 allowBlank: true
  153.             }
  154.         }, {
  155.             dataIndex: 'title',
  156.             header: snippets.title,
  157.             flex: 1,
  158.             editor: {
  159.                 xtype: 'textfield',
  160.                 allowBlank: true
  161.             }
  162.         }, {
  163.             xtype: 'actioncolumn',
  164.             header: snippets.actions,
  165.             width: 60,
  166.             items: [{
  167.                 iconCls: 'sprite-minus-circle',
  168.                 action: 'delete-imagemenu',
  169.                 scope: me,
  170.                 handler: me.onDeleteImagemenu
  171.             }]
  172.         }];
  173.     },
  174.  
  175.     /**
  176.      * Event listener method which will be triggered when one (or more)
  177.      * imagemenu are added to the imagemenu.
  178.      *
  179.      * Creates new models based on the selected imagemenus and
  180.      * assigns them to the imagemenu store.
  181.      *
  182.      * @public
  183.      * @event selectMedia
  184.      * @param [object] field - Shopware.MediaManager.MediaSelection
  185.      * @param [array] records - array of the selected media
  186.      */
  187.     onAddImagemenuToGrid: function(field, records) {
  188.         var me = this, store = me.imagemenuStore;
  189.  
  190.         Ext.each(records, function(record) {
  191.             var count = store.getCount();
  192.             var model = Ext.create('Shopware.apps.Emotion.model.Imagemenu', {
  193.                 position: count,
  194.                 path: record.get('path'),
  195.                 mediaId: record.get('id'),
  196.                 link: record.get('link'),
  197.                 altText: record.get('altText'),
  198.                 title: record.get('title')
  199.             });
  200.             store.add(model);
  201.         });
  202.  
  203.         // We need a defer due to early firing of the event
  204.         Ext.defer(function() {
  205.             me.mediaSelection.inputEl.dom.value = '';
  206.             me.refreshHiddenValue();
  207.         }, 10);
  208.  
  209.     },
  210.  
  211.     /**
  212.      * Event listener method which will be triggered when the user
  213.      * deletes a imagemenu from imagemenu administration grid panel.
  214.      *
  215.      * Removes the imagemenu from the imagemenu store.
  216.      *
  217.      * @event click#actioncolumn
  218.      * @param [object] grid - Ext.grid.Panel
  219.      * @param [integer] rowIndex - Index of the clicked row
  220.      * @param [integer] colIndex - Index of the clicked column
  221.      * @param [object] item - DOM node of the clicked row
  222.      * @param [object] eOpts - additional event parameters
  223.      * @param [object] record - Associated model of the clicked row
  224.      */
  225.     onDeleteImagemenu: function(grid, rowIndex, colIndex, item, eOpts, record) {
  226.         var me = this;
  227.         var store = grid.getStore();
  228.         store.remove(record);
  229.         me.refreshHiddenValue();
  230.     },
  231.  
  232.     /**
  233.      * Event listener method which will be fired when the user
  234.      * repositions a imagemenu through drag and drop.
  235.      *
  236.      * Sets the new position of the imagemenu in the imagemenu store
  237.      * and saves the data to an hidden field.
  238.      *
  239.      * @public
  240.      * @event drop
  241.      * @return void
  242.      */
  243.     onRepositionImagemenu: function() {
  244.         var me = this;
  245.  
  246.         var i = 0;
  247.         me.imagemenuStore.each(function(item) {
  248.             item.set('position', i);
  249.             i++;
  250.         });
  251.         me.refreshHiddenValue();
  252.     },
  253.  
  254.     /**
  255.      * Refreshes the mapping field in the model
  256.      * which contains all imagemenus in the grid.
  257.      *
  258.      * @public
  259.      * @return void
  260.      */
  261.     refreshHiddenValue: function() {
  262.         var me = this,
  263.             store = me.imagemenuStore,
  264.             cache = [];
  265.  
  266.         store.each(function(item) {
  267.             cache.push(item.data);
  268.         });
  269.         var record = me.getSettings('record');
  270.         record.set('mapping', cache);
  271.     },
  272.  
  273.     /**
  274.      * Refactor sthe mapping field in the global record
  275.      * which contains all imagemenu in the grid.
  276.      *
  277.      * Adds all imagemenus to the imagemenu administration grid
  278.      * when the user opens the component.
  279.      *
  280.      * @return void
  281.      */
  282.     getGridData: function() {
  283.         var me = this,
  284.             elementStore = me.getSettings('record').get('data'), imagemenu;
  285.  
  286.         Ext.each(elementStore, function(element) {
  287.             if(element.key === 'imagemenu') {
  288.                 imagemenu = element;
  289.                 return false;
  290.             }
  291.         });
  292.  
  293.         if(imagemenu && imagemenu.value) {
  294.             Ext.each(imagemenu.value, function(item) {
  295.                 me.imagemenuStore.add(Ext.create('Shopware.apps.Emotion.model.Imagemenu', item));
  296.             });
  297.         }
  298.     },
  299.  
  300.     /**
  301.      * Renderer for sorthandle-column
  302.      *
  303.      * @param [string] value
  304.      */
  305.     renderSorthandleColumn: function() {
  306.         return '<div style="cursor: move;">&#009868;</div>';
  307.     }
  308. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement