TaskBar.js
上传用户:dawnssy
上传日期:2022-08-06
资源大小:9345k
文件大小:14k
源码类别:

JavaScript

开发平台:

JavaScript

  1. /*!  * Ext JS Library 3.1.0  * Copyright(c) 2006-2009 Ext JS, LLC  * licensing@extjs.com  * http://www.extjs.com/license  */ /**
  2.  * @class Ext.ux.TaskBar
  3.  * @extends Ext.util.Observable
  4.  */
  5. Ext.ux.TaskBar = function(app){
  6.     this.app = app;
  7.     this.init();
  8. }
  9. Ext.extend(Ext.ux.TaskBar, Ext.util.Observable, {
  10.     init : function(){
  11.         this.startMenu = new Ext.ux.StartMenu(Ext.apply({
  12.             iconCls: 'user',
  13.             height: 300,
  14.             shadow: true,
  15.             title: 'Jack Slocum',
  16.             width: 300
  17.         }, this.app.startConfig));
  18.         this.startBtn = new Ext.Button({
  19.             text: 'Start',
  20.             id: 'ux-startbutton',
  21.             iconCls:'start',
  22.             menu: this.startMenu,
  23.             menuAlign: 'bl-tl',
  24.             renderTo: 'ux-taskbar-start',
  25.             clickEvent: 'mousedown',
  26.             template: new Ext.Template(
  27.                 '<table cellspacing="0" class="x-btn {3}"><tbody><tr>',
  28.                 '<td class="ux-startbutton-left"><i>&#160;</i></td>',
  29.                 '<td class="ux-startbutton-center"><em class="{5} unselectable="on">',
  30.                     '<button class="x-btn-text {2}" type="{1}" style="height:30px;">{0}</button>',
  31.                 '</em></td>',
  32.                 '<td class="ux-startbutton-right"><i>&#160;</i></td>',
  33.                 "</tr></tbody></table>")
  34.         });
  35.         var width = this.startBtn.getEl().getWidth()+10;
  36.         var sbBox = new Ext.BoxComponent({
  37.             el: 'ux-taskbar-start',
  38.             id: 'TaskBarStart',
  39.             minWidth: width,
  40.             region:'west',
  41.             split: true,
  42.             width: width
  43.         });
  44.         this.tbPanel = new Ext.ux.TaskButtonsPanel({
  45.             el: 'ux-taskbuttons-panel',
  46.             id: 'TaskBarButtons',
  47.             region:'center'
  48.         });
  49.         var container = new Ext.ux.TaskBarContainer({
  50.             el: 'ux-taskbar',
  51.             layout: 'border',
  52.             items: [sbBox,this.tbPanel]
  53.         });
  54.         return this;
  55.     },
  56.     addTaskButton : function(win){
  57.         return this.tbPanel.addButton(win, 'ux-taskbuttons-panel');
  58.     },
  59.     removeTaskButton : function(btn){
  60.         this.tbPanel.removeButton(btn);
  61.     },
  62.     setActiveButton : function(btn){
  63.         this.tbPanel.setActiveButton(btn);
  64.     }
  65. });
  66. /**
  67.  * @class Ext.ux.TaskBarContainer
  68.  * @extends Ext.Container
  69.  */
  70. Ext.ux.TaskBarContainer = Ext.extend(Ext.Container, {
  71.     initComponent : function() {
  72.         Ext.ux.TaskBarContainer.superclass.initComponent.call(this);
  73.         this.el = Ext.get(this.el) || Ext.getBody();
  74.         this.el.setHeight = Ext.emptyFn;
  75.         this.el.setWidth = Ext.emptyFn;
  76.         this.el.setSize = Ext.emptyFn;
  77.         this.el.setStyle({
  78.             overflow:'hidden',
  79.             margin:'0',
  80.             border:'0 none'
  81.         });
  82.         this.el.dom.scroll = 'no';
  83.         this.allowDomMove = false;
  84.         this.autoWidth = true;
  85.         this.autoHeight = true;
  86.         Ext.EventManager.onWindowResize(this.fireResize, this);
  87.         this.renderTo = this.el;
  88.     },
  89.     fireResize : function(w, h){
  90.         this.onResize(w, h, w, h);
  91.     }
  92. });
  93. /**
  94.  * @class Ext.ux.TaskButtonsPanel
  95.  * @extends Ext.BoxComponent
  96.  */
  97. Ext.ux.TaskButtonsPanel = Ext.extend(Ext.BoxComponent, {
  98.     activeButton: null,
  99.     enableScroll: true,
  100.     scrollIncrement: 0,
  101.     scrollRepeatInterval: 400,
  102.     scrollDuration: .35,
  103.     animScroll: true,
  104.     resizeButtons: true,
  105.     buttonWidth: 168,
  106.     minButtonWidth: 118,
  107.     buttonMargin: 2,
  108.     buttonWidthSet: false,
  109.     initComponent : function() {
  110.         Ext.ux.TaskButtonsPanel.superclass.initComponent.call(this);
  111.         this.on('resize', this.delegateUpdates);
  112.         this.items = [];
  113.         this.stripWrap = Ext.get(this.el).createChild({
  114.             cls: 'ux-taskbuttons-strip-wrap',
  115.             cn: {
  116.                 tag:'ul', cls:'ux-taskbuttons-strip'
  117.             }
  118.         });
  119.         this.stripSpacer = Ext.get(this.el).createChild({
  120.             cls:'ux-taskbuttons-strip-spacer'
  121.         });
  122.         this.strip = new Ext.Element(this.stripWrap.dom.firstChild);
  123.         this.edge = this.strip.createChild({
  124.             tag:'li',
  125.             cls:'ux-taskbuttons-edge'
  126.         });
  127.         this.strip.createChild({
  128.             cls:'x-clear'
  129.         });
  130.     },
  131.     addButton : function(win){
  132.         var li = this.strip.createChild({tag:'li'}, this.edge); // insert before the edge
  133.         var btn = new Ext.ux.TaskBar.TaskButton(win, li);
  134.         this.items.push(btn);
  135.         if(!this.buttonWidthSet){
  136.             this.lastButtonWidth = btn.container.getWidth();
  137.         }
  138.         this.setActiveButton(btn);
  139.         return btn;
  140.     },
  141.     removeButton : function(btn){
  142.         var li = document.getElementById(btn.container.id);
  143.         btn.destroy();
  144.         li.parentNode.removeChild(li);
  145.         var s = [];
  146.         for(var i = 0, len = this.items.length; i < len; i++) {
  147.             if(this.items[i] != btn){
  148.                 s.push(this.items[i]);
  149.             }
  150.         }
  151.         this.items = s;
  152.         this.delegateUpdates();
  153.     },
  154.     setActiveButton : function(btn){
  155.         this.activeButton = btn;
  156.         this.delegateUpdates();
  157.     },
  158.     delegateUpdates : function(){
  159.         /*if(this.suspendUpdates){
  160.             return;
  161.         }*/
  162.         if(this.resizeButtons && this.rendered){
  163.             this.autoSize();
  164.         }
  165.         if(this.enableScroll && this.rendered){
  166.             this.autoScroll();
  167.         }
  168.     },
  169.     autoSize : function(){
  170.         var count = this.items.length;
  171.         var ow = this.el.dom.offsetWidth;
  172.         var aw = this.el.dom.clientWidth;
  173.         if(!this.resizeButtons || count < 1 || !aw){ // !aw for display:none
  174.             return;
  175.         }
  176.         var each = Math.max(Math.min(Math.floor((aw-4) / count) - this.buttonMargin, this.buttonWidth), this.minButtonWidth); // -4 for float errors in IE
  177.         var btns = this.stripWrap.dom.getElementsByTagName('button');
  178.         this.lastButtonWidth = Ext.get(btns[0].id).findParent('li').offsetWidth;
  179.         for(var i = 0, len = btns.length; i < len; i++) {
  180.             var btn = btns[i];
  181.             var tw = Ext.get(btns[i].id).findParent('li').offsetWidth;
  182.             var iw = btn.offsetWidth;
  183.             btn.style.width = (each - (tw-iw)) + 'px';
  184.         }
  185.     },
  186.     autoScroll : function(){
  187.         var count = this.items.length;
  188.         var ow = this.el.dom.offsetWidth;
  189.         var tw = this.el.dom.clientWidth;
  190.         var wrap = this.stripWrap;
  191.         var cw = wrap.dom.offsetWidth;
  192.         var pos = this.getScrollPos();
  193.         var l = this.edge.getOffsetsTo(this.stripWrap)[0] + pos;
  194.         if(!this.enableScroll || count < 1 || cw < 20){ // 20 to prevent display:none issues
  195.             return;
  196.         }
  197.         wrap.setWidth(tw); // moved to here because of problem in Safari
  198.         if(l <= tw){
  199.             wrap.dom.scrollLeft = 0;
  200.             //wrap.setWidth(tw); moved from here because of problem in Safari
  201.             if(this.scrolling){
  202.                 this.scrolling = false;
  203.                 this.el.removeClass('x-taskbuttons-scrolling');
  204.                 this.scrollLeft.hide();
  205.                 this.scrollRight.hide();
  206.             }
  207.         }else{
  208.             if(!this.scrolling){
  209.                 this.el.addClass('x-taskbuttons-scrolling');
  210.             }
  211.             tw -= wrap.getMargins('lr');
  212.             wrap.setWidth(tw > 20 ? tw : 20);
  213.             if(!this.scrolling){
  214.                 if(!this.scrollLeft){
  215.                     this.createScrollers();
  216.                 }else{
  217.                     this.scrollLeft.show();
  218.                     this.scrollRight.show();
  219.                 }
  220.             }
  221.             this.scrolling = true;
  222.             if(pos > (l-tw)){ // ensure it stays within bounds
  223.                 wrap.dom.scrollLeft = l-tw;
  224.             }else{ // otherwise, make sure the active button is still visible
  225.                 this.scrollToButton(this.activeButton, true); // true to animate
  226.             }
  227.             this.updateScrollButtons();
  228.         }
  229.     },
  230.     createScrollers : function(){
  231.         var h = this.el.dom.offsetHeight; //var h = this.stripWrap.dom.offsetHeight;
  232.         // left
  233.         var sl = this.el.insertFirst({
  234.             cls:'ux-taskbuttons-scroller-left'
  235.         });
  236.         sl.setHeight(h);
  237.         sl.addClassOnOver('ux-taskbuttons-scroller-left-over');
  238.         this.leftRepeater = new Ext.util.ClickRepeater(sl, {
  239.             interval : this.scrollRepeatInterval,
  240.             handler: this.onScrollLeft,
  241.             scope: this
  242.         });
  243.         this.scrollLeft = sl;
  244.         // right
  245.         var sr = this.el.insertFirst({
  246.             cls:'ux-taskbuttons-scroller-right'
  247.         });
  248.         sr.setHeight(h);
  249.         sr.addClassOnOver('ux-taskbuttons-scroller-right-over');
  250.         this.rightRepeater = new Ext.util.ClickRepeater(sr, {
  251.             interval : this.scrollRepeatInterval,
  252.             handler: this.onScrollRight,
  253.             scope: this
  254.         });
  255.         this.scrollRight = sr;
  256.     },
  257.     getScrollWidth : function(){
  258.         return this.edge.getOffsetsTo(this.stripWrap)[0] + this.getScrollPos();
  259.     },
  260.     getScrollPos : function(){
  261.         return parseInt(this.stripWrap.dom.scrollLeft, 10) || 0;
  262.     },
  263.     getScrollArea : function(){
  264.         return parseInt(this.stripWrap.dom.clientWidth, 10) || 0;
  265.     },
  266.     getScrollAnim : function(){
  267.         return {
  268.             duration: this.scrollDuration,
  269.             callback: this.updateScrollButtons,
  270.             scope: this
  271.         };
  272.     },
  273.     getScrollIncrement : function(){
  274.         return (this.scrollIncrement || this.lastButtonWidth+2);
  275.     },
  276.     /* getBtnEl : function(item){
  277.         return document.getElementById(item.id);
  278.     }, */
  279.     scrollToButton : function(item, animate){
  280.         item = item.el.dom.parentNode; // li
  281.         if(!item){ return; }
  282.         var el = item; //this.getBtnEl(item);
  283.         var pos = this.getScrollPos(), area = this.getScrollArea();
  284.         var left = Ext.fly(el).getOffsetsTo(this.stripWrap)[0] + pos;
  285.         var right = left + el.offsetWidth;
  286.         if(left < pos){
  287.             this.scrollTo(left, animate);
  288.         }else if(right > (pos + area)){
  289.             this.scrollTo(right - area, animate);
  290.         }
  291.     },
  292.     scrollTo : function(pos, animate){
  293.         this.stripWrap.scrollTo('left', pos, animate ? this.getScrollAnim() : false);
  294.         if(!animate){
  295.             this.updateScrollButtons();
  296.         }
  297.     },
  298.     onScrollRight : function(){
  299.         var sw = this.getScrollWidth()-this.getScrollArea();
  300.         var pos = this.getScrollPos();
  301.         var s = Math.min(sw, pos + this.getScrollIncrement());
  302.         if(s != pos){
  303.             this.scrollTo(s, this.animScroll);
  304.         }
  305.     },
  306.     onScrollLeft : function(){
  307.         var pos = this.getScrollPos();
  308.         var s = Math.max(0, pos - this.getScrollIncrement());
  309.         if(s != pos){
  310.             this.scrollTo(s, this.animScroll);
  311.         }
  312.     },
  313.     updateScrollButtons : function(){
  314.         var pos = this.getScrollPos();
  315.         this.scrollLeft[pos == 0 ? 'addClass' : 'removeClass']('ux-taskbuttons-scroller-left-disabled');
  316.         this.scrollRight[pos >= (this.getScrollWidth()-this.getScrollArea()) ? 'addClass' : 'removeClass']('ux-taskbuttons-scroller-right-disabled');
  317.     }
  318. });
  319. /**
  320.  * @class Ext.ux.TaskBar.TaskButton
  321.  * @extends Ext.Button
  322.  */
  323. Ext.ux.TaskBar.TaskButton = function(win, el){
  324.     this.win = win;
  325.     Ext.ux.TaskBar.TaskButton.superclass.constructor.call(this, {
  326.         iconCls: win.iconCls,
  327.         text: Ext.util.Format.ellipsis(win.title, 12),
  328.         renderTo: el,
  329.         handler : function(){
  330.             if(win.minimized || win.hidden){
  331.                 win.show();
  332.             }else if(win == win.manager.getActive()){
  333.                 win.minimize();
  334.             }else{
  335.                 win.toFront();
  336.             }
  337.         },
  338.         clickEvent:'mousedown',
  339.         template: new Ext.Template(
  340.             '<table cellspacing="0" class="x-btn {3}"><tbody><tr>',
  341.             '<td class="ux-taskbutton-left"><i>&#160;</i></td>',
  342.             '<td class="ux-taskbutton-center"><em class="{5} unselectable="on">',
  343.                 '<button class="x-btn-text {2}" type="{1}" style="height:28px;">{0}</button>',
  344.             '</em></td>',
  345.             '<td class="ux-taskbutton-right"><i>&#160;</i></td>',
  346.             "</tr></tbody></table>")
  347.     });
  348. };
  349. Ext.extend(Ext.ux.TaskBar.TaskButton, Ext.Button, {
  350.     onRender : function(){
  351.         Ext.ux.TaskBar.TaskButton.superclass.onRender.apply(this, arguments);
  352.         this.cmenu = new Ext.menu.Menu({
  353.             items: [{
  354.                 text: 'Restore',
  355.                 handler: function(){
  356.                     if(!this.win.isVisible()){
  357.                         this.win.show();
  358.                     }else{
  359.                         this.win.restore();
  360.                     }
  361.                 },
  362.                 scope: this
  363.             },{
  364.                 text: 'Minimize',
  365.                 handler: this.win.minimize,
  366.                 scope: this.win
  367.             },{
  368.                 text: 'Maximize',
  369.                 handler: this.win.maximize,
  370.                 scope: this.win
  371.             }, '-', {
  372.                 text: 'Close',
  373.                 handler: this.closeWin.createDelegate(this, this.win, true),
  374.                 scope: this.win
  375.             }]
  376.         });
  377.         this.cmenu.on('beforeshow', function(){
  378.             var items = this.cmenu.items.items;
  379.             var w = this.win;
  380.             items[0].setDisabled(w.maximized !== true && w.hidden !== true);
  381.             items[1].setDisabled(w.minimized === true);
  382.             items[2].setDisabled(w.maximized === true || w.hidden === true);
  383.         }, this);
  384.         this.el.on('contextmenu', function(e){
  385.             e.stopEvent();
  386.             if(!this.cmenu.el){
  387.                 this.cmenu.render();
  388.             }
  389.             var xy = e.getXY();
  390.             xy[1] -= this.cmenu.el.getHeight();
  391.             this.cmenu.showAt(xy);
  392.         }, this);
  393.     },
  394.     closeWin : function(cMenu, e, win){
  395.         if(!win.isVisible()){
  396.             win.show();
  397.         }else{
  398.             win.restore();
  399.         }
  400.         win.close();
  401.     }
  402. });