partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

Tree.js (1949B)


      1 Ext.define('PartKeepr.Components.Preferences.Tree', {
      2     extend: 'Ext.tree.Panel',
      3     width: 400,
      4     rootVisible: false,
      5     initComponent: function (config) {
      6         var menu = {
      7             root: {
      8                 expanded: true,
      9                 children: []
     10             }
     11         };
     12 
     13         var target, menuItemIterator;
     14 
     15         for (menuItemIterator = 0; menuItemIterator < this.menuItems.length; menuItemIterator++) {
     16             target = Ext.ClassManager.get(this.menuItems[menuItemIterator]);
     17 
     18             if (!target) {
     19                 console.log("Error: " + this.menuItems[menuItemIterator] + " not found!");
     20             }
     21 
     22             if (!target.menuPath) {
     23                 console.log("Error: " + this.menuItems[menuItemIterator] + " has no menuPath defined!");
     24             }
     25 
     26             this.createMenu(target, Ext.clone(target.menuPath), menu.root);
     27         }
     28 
     29         this.store = Ext.create('Ext.data.TreeStore', menu);
     30 
     31         this.callParent(this, config);
     32     },
     33 
     34     createMenu: function (target, menuPath, root) {
     35         var item = menuPath.shift();
     36         var newItem;
     37 
     38         if (item === undefined) {
     39             newItem = {text: target.title, iconCls: target.iconCls, expanded: true, target: target, leaf: true};
     40 
     41             root.children.push(newItem);
     42             return root;
     43         }
     44 
     45         var foundItem = false;
     46 
     47         for (var i = 0; i < root.children.length; i++) {
     48             if (root.children[i].text === item.text) {
     49                 Ext.applyIf(root.children[i], item);
     50                 foundItem = i;
     51             }
     52         }
     53 
     54         if (foundItem === false) {
     55             newItem = {children: [], expanded: true};
     56 
     57             Ext.applyIf(newItem, item);
     58 
     59             var data = this.createMenu(target, menuPath, newItem);
     60             root.children.push(data);
     61         } else {
     62             this.createMenu(target, menuPath, root.children[foundItem]);
     63         }
     64 
     65         return root;
     66     }
     67 });