Advertisement
Guest User

a porra toda

a guest
Jun 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var WIDTH = 350;
  2. var FOCUSOUT = 0.2;
  3. var FOCUSIN = 1;
  4. var IMG_HIDE2LEFT  = "resource/cli/icon_hide2left_16px.png";
  5. var IMG_SHOW2RIGHT = "resource/cli/icon_show2right_16px.png";
  6.  
  7. qx.Class.define("cli.TreeMenu", {
  8.     extend: qx.ui.container.Composite,
  9.  
  10.     events : {
  11.         "executed" : "qx.event.type.Data"
  12.     },
  13.  
  14.     construct: function(menu) {
  15.         this.base(arguments);
  16.         this.setLayout(new qx.ui.layout.Canvas());
  17.         this.setWidth(WIDTH);
  18.  
  19.         var decorator = new qx.ui.decoration.Decorator().set({
  20.             backgroundColor: "#F2F2F2",
  21.             color: "#dddddd",
  22.             styleRight: "solid",
  23.             widthRight: 3
  24.         });
  25.         this.setDecorator(decorator);
  26.  
  27.         this.progs = [];
  28.         this.nodes = this.create_nodes(menu);
  29.         this.create_tree(this.nodes);
  30.  
  31.         var that = this;
  32.  
  33.         this.tree.addListener('focus', function() {
  34.             this.has_focus = true;
  35.             this.animate_fadein();
  36.         }, this);
  37.         this.tree.addListener('blur',  function() {
  38.             this.has_focus = false;
  39.             this.animate_fadeout();
  40.         }, this);
  41.         var timer = null;
  42.         this.addListener('mouseover', function() {
  43.             //console.log('mouseover');
  44.             if (timer) {
  45.                 clearTimeout(timer);
  46.                 timer = null
  47.             }
  48.             this.animate_fadein();
  49.         }, this);
  50.         this.addListener('mouseout', function() {
  51.             //console.log('mouseout');
  52.             timer = setTimeout(function() {
  53.                 that.animate_fadeout()
  54.             }, 3000);
  55.         }, this);
  56.         this.setOpacity(FOCUSOUT);
  57.  
  58.         this.create_btn_close();
  59.         this.addListenerOnce('appear', function() {
  60.             var state = window.localStorage.getItem('menu-tree') || 'opened';
  61.             if (state == 'closed') {
  62.                 setTimeout(this.hide_me.bind(this), 2000);
  63.             }
  64.         }, this);
  65.     },
  66.  
  67.     members: {
  68.         nodes : null,
  69.         progs : null,
  70.         tree  : null,
  71.         close_btn : null,
  72.         is_visible : true,
  73.  
  74.         EVENT_ENABLED : true,
  75.  
  76.         create_btn_close : function() {
  77.             var decorator = new qx.ui.decoration.Decorator();
  78.             var btn = new qx.ui.form.Button(null, IMG_HIDE2LEFT).set({
  79.                 show: "icon",
  80.                 margin : 0,
  81.                 padding: 0,
  82.                 decorator: decorator
  83.             });
  84.             this.add(btn, {top:5, right:5});
  85.  
  86.             btn.addListener('execute', function() {
  87.                 if (this.is_visible) {
  88.                     this.hide_me();
  89.                 } else {
  90.                     this.show_me();
  91.                 }
  92.             }, this);
  93.             this.close_btn = btn;
  94.             this.tree.getChildControl('scrollbar-y').set({
  95.                 marginTop: 22,
  96.                 opacity: 0.5
  97.             });
  98.         },
  99.  
  100.         hide_me : function() {
  101.             this.is_visible = false;
  102.             var desc = {
  103.                 duration: 300,
  104.                 timing: "ease-out",
  105.                 keyFrames : {
  106.                     0   : { width: WIDTH+'px' },
  107.                     100 : { width: '30px' }
  108.                 }
  109.             };
  110.             var box = this.getContentElement().getDomElement();
  111.             var handle = qx.bom.element.Animation.animate(box, desc);
  112.             handle.addListener("end", function(){
  113.                 this.setWidth(28);
  114.                 this.setMaxHeight(28);
  115.                 this.tree.setVisibility('excluded');
  116.                 this.close_btn.setIcon(IMG_SHOW2RIGHT);
  117.             }, this);
  118.             window.localStorage.setItem('menu-tree', 'closed');
  119.         },
  120.  
  121.         show_me : function() {
  122.             this.is_visible = true;
  123.             var that = this;
  124.             this.setMaxHeight(9999);
  125.             this.tree.setVisibility('visible');
  126.             setTimeout(function() {
  127.                 var desc = {
  128.                     duration: 300,
  129.                     timing: "ease-in",
  130.                     keyFrames : {
  131.                         0     : { width: '30px' },
  132.                         100   : { width: WIDTH+'px' }
  133.                     }
  134.                 };
  135.                 var box = that.getContentElement().getDomElement();
  136.                 var handle = qx.bom.element.Animation.animate(box, desc);
  137.                 handle.addListener("end", function(){
  138.                     that.setWidth(WIDTH);
  139.                     that.close_btn.setIcon(IMG_HIDE2LEFT);
  140.                 });
  141.             }, 10);
  142.             window.localStorage.setItem('menu-tree', 'opened');
  143.         },
  144.  
  145.         animate_fadein : function() {
  146.             if (this.getOpacity() == FOCUSIN) return;
  147.             var desc = {
  148.                 duration: 300,
  149.                 timing: "ease-out",
  150.                 keyFrames : {
  151.                     0   : { opacity: FOCUSOUT },
  152.                     100 : { opacity: FOCUSIN  }
  153.                 }
  154.             };
  155.             var box = this.getContentElement().getDomElement();
  156.             var handle = qx.bom.element.Animation.animate(box, desc);
  157.             handle.addListener("end", function(){
  158.                 this.setOpacity(FOCUSIN);
  159.             }, this);
  160.         },
  161.         animate_fadeout : function() {
  162.             if (this.getOpacity() != FOCUSIN) return;
  163.             if (this.has_focus) return;
  164.             var desc = {
  165.                 duration: 300,
  166.                 timing: "ease-in",
  167.                 keyFrames : {
  168.                     0   : { opacity: FOCUSIN  },
  169.                     100 : { opacity: FOCUSOUT }
  170.                 }
  171.             };
  172.             var box = this.getContentElement().getDomElement();
  173.             var handle = qx.bom.element.Animation.animate(box, desc);
  174.             handle.addListener("end", function(){
  175.                 this.setOpacity(FOCUSOUT);
  176.             }, this);
  177.         },
  178.  
  179.         select_from_hash : function(hash) {
  180.             console.log('select_from_hash:', hash);
  181.             var that = this;
  182.             var find = function(sub, hash) {
  183.                 //console.log('sub', sub);
  184.                 var ar = sub.toArray();
  185.                 //console.log(ar.length);
  186.                 for(var i=0; i<ar.length; i++) {
  187.                     var index = ar[i].getProgIndex();
  188.                     //console.log(i, ar[i], index);
  189.                     var o = that.progs[index];
  190.                     if (o && o.progs && o.progs[0].hashpath==hash) {
  191.                         return ar[i];
  192.                     }
  193.                     if (ar[i].getChildren) {
  194.                         var ch = ar[i].getChildren();
  195.                         //console.log(ch);
  196.                         if (ch) {
  197.                             var r = find(ch, hash);
  198.                             if (r) return r;
  199.                         }
  200.                     }
  201.                 }              
  202.             }
  203.             //console.log('this.nodes:', this.nodes);//, this.nodes.getSize(), this.nodes.getItem(0));
  204.             var elem = find(this.nodes.getItem(0).getChildren(), hash);
  205.             if (elem) {
  206.                 //console.log('elem:', elem);
  207.                 this.nodes.getItem(0).getChildren().forEach(function(i) {
  208.                     that.tree.closeNode(i);
  209.                 });
  210.                 this.tree.openNodeAndParents(elem);
  211.                 this.tree.getSelection().removeAll();
  212.                 this.EVENT_ENABLED = false;
  213.                 this.tree.getSelection().push(elem);
  214.                 this.tree.refresh();
  215.             }
  216.         },
  217.  
  218.         create_nodes : function(menu) {
  219.             var that = this;
  220.             //console.log(menu);
  221.             var process_sub = function(sub) {
  222.                 var r = [];
  223.                 for(var i=0; i<sub.length; i++) {
  224.                     var t = sub[i];
  225.                     that.progs.push(t.prog);
  226.                     var prog_index = that.progs.length-1;
  227.                     var e = {
  228.                         label:     t.label,
  229.                         color:     (!t.enabled ? "#ddd" : (t.color ? t.color : "#75889C")),
  230.                         icon:      (t.icon ? t.icon : "resource/cli/"+(t.sub && t.sub.length>0 ? "icon_arrowgray_right_16px.png" : "icon_dotgray_16px.png")),
  231.                         enabled:   t.enabled,
  232.                         progIndex: prog_index,
  233.                         iconOpened: "resource/cli/icon_arrowgray_down_16px.png"
  234.                     };
  235.                     if (t.sub && t.sub.length>0) {
  236.                         e.children = process_sub(t.sub);
  237.                     }
  238.                     r.push(e);
  239.                 }
  240.                 return r;
  241.             }
  242.             var nodes = process_sub( [{ label:"root", sub:menu, enabled:true, prog: null }] );
  243.             return qx.data.marshal.Json.createModel(nodes);
  244.         },
  245.  
  246.         create_tree : function(nodes) {
  247.             //console.log(nodes.toArray());
  248.             var tree = new qx.ui.tree.VirtualTree(nodes.getItem(0), "label", "children").set({
  249.                 hideRoot: true,
  250.                 openMode: "tap",
  251.                 showTopLevelOpenCloseIcons: true
  252.             });
  253.             tree.setDelegate({
  254.                 bindItem : function(controller, item, id) {
  255.                     controller.bindProperty("label",      "label",      null, item, id);
  256.                     controller.bindProperty("color",      "textColor",  null, item, id);
  257.                     controller.bindProperty("icon",       "icon",       null, item, id);
  258.                     controller.bindProperty("iconOpened", "iconOpened", null, item, id);
  259.                     controller.bindProperty("enabled",    "enabled",    null, item, id);
  260.                 },
  261.                 configureItem : function(item) {
  262.                     item.addListener('changeOpen', function() {
  263.                         var opened = item.getOpen();
  264.                         var font = new qx.bom.Font(12, ["Lucida Grande", "DejaVu Sans", 'Verdana', 'sans-serif']);
  265.                         if (opened) font.setBold(true);
  266.                         var textColor = opened ? '#55606B' : item.getTextColor(); // #590 para verde
  267.                         item.getChildControl('label').set({
  268.                             font: font,
  269.                             textColor: textColor
  270.                         });
  271.                         //console.log(item.getLabel(), item);
  272.                     });
  273.                     item.addListener('changeTextColor', function() {
  274.                         //var textColor = item.getTextColor();
  275.                         //item.getChildControl('label').setTextColor(textColor);
  276.                     });
  277.                 },
  278.                 onPool : function(item) {
  279.                     item.resetIcon();
  280.                     item.resetIconOpened();
  281.                     item.resetOpen();
  282.                     item.resetModel();
  283.                     item.resetLabel();
  284.                     item.resetEnabled();
  285.                     item.resetTextColor();
  286.                     //item.setIcon("resource/cli/icon_dotgray_16px.png");
  287.                     item.getChildControl('icon').resetSource();
  288.                 }
  289.             });
  290.  
  291.             var run_selection = function() {
  292.                 var item = tree.getSelection().getItem(0);
  293.                 console.log('run_selection item:', item);
  294.                 if (item) {
  295.                     var o = this.progs[item.getProgIndex()];
  296.                     console.log("Selection: ", item, o);
  297.                     if (o) {
  298.                         if (this.EVENT_ENABLED) this.fireEvent('executed', qx.event.type.Data, [o]);
  299.                         item.setColor("#22f");
  300.                     }
  301.                 }
  302.                 //tree.getSelection().removeAll();
  303.                 this.EVENT_ENABLED = true;
  304.             };
  305.  
  306.             tree.getSelection().addListener("change", run_selection, this);
  307.             tree.addListener("dblclick", run_selection, this);
  308.  
  309.             this.add(tree, {edge:0});
  310.             this.tree = tree;
  311.         }
  312.  
  313.     }
  314. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement