~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/ext/source/widgets/Button.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Ext JS Library 2.2
 
3
 * Copyright(c) 2006-2008, Ext JS, LLC.
 
4
 * licensing@extjs.com
 
5
 * 
 
6
 * http://extjs.com/license
 
7
 */
 
8
 
 
9
 
 
10
/**
 
11
 * @class Ext.Button
 
12
 * @extends Ext.Component
 
13
 * Simple Button class
 
14
 * @cfg {String} text The button text
 
15
 * @cfg {String} icon The path to an image to display in the button (the image will be set as the background-image
 
16
 * CSS property of the button by default, so if you want a mixed icon/text button, set cls:"x-btn-text-icon")
 
17
 * @cfg {Function} handler A function called when the button is clicked (can be used instead of click event)
 
18
 * @cfg {Object} scope The scope of the handler
 
19
 * @cfg {Number} minWidth The minimum width for this button (used to give a set of buttons a common width)
 
20
 * @cfg {String/Object} tooltip The tooltip for the button - can be a string or QuickTips config object
 
21
 * @cfg {Boolean} hidden True to start hidden (defaults to false)
 
22
 * @cfg {Boolean} disabled True to start disabled (defaults to false)
 
23
 * @cfg {Boolean} pressed True to start pressed (only if enableToggle = true)
 
24
 * @cfg {String} toggleGroup The group this toggle button is a member of (only 1 per group can be pressed, only
 
25
 * applies if enableToggle = true)
 
26
 * @cfg {Boolean/Object} repeat True to repeat fire the click event while the mouse is down. This can also be
 
27
  an {@link Ext.util.ClickRepeater} config object (defaults to false).
 
28
 * @constructor
 
29
 * Create a new button
 
30
 * @param {Object} config The config object
 
31
 */
 
32
Ext.Button = Ext.extend(Ext.Component, {
 
33
    /**
 
34
     * Read-only. True if this button is hidden
 
35
     * @type Boolean
 
36
     */
 
37
    hidden : false,
 
38
    /**
 
39
     * Read-only. True if this button is disabled
 
40
     * @type Boolean
 
41
     */
 
42
    disabled : false,
 
43
    /**
 
44
     * Read-only. True if this button is pressed (only if enableToggle = true)
 
45
     * @type Boolean
 
46
     */
 
47
    pressed : false,
 
48
    /**
 
49
     * The Button's owner {@link Ext.Panel} (defaults to undefined, and is set automatically when
 
50
     * the Button is added to a container).  Read-only.
 
51
     * @type Ext.Panel
 
52
     * @property ownerCt
 
53
     */
 
54
 
 
55
    /**
 
56
     * @cfg {Number} tabIndex Set a DOM tabIndex for this button (defaults to undefined)
 
57
     */
 
58
 
 
59
    /**
 
60
     * @cfg {Boolean} allowDepress
 
61
     * False to not allow a pressed Button to be depressed (defaults to undefined). Only valid when {@link #enableToggle} is true.
 
62
     */
 
63
 
 
64
    /**
 
65
     * @cfg {Boolean} enableToggle
 
66
     * True to enable pressed/not pressed toggling (defaults to false)
 
67
     */
 
68
    enableToggle: false,
 
69
    /**
 
70
     * @cfg {Function} toggleHandler
 
71
     * Function called when a Button with {@link #enableToggle} set to true is clicked. Two arguments are passed:<ul class="mdetail-params">
 
72
     * <li><b>button</b> : Ext.Button<div class="sub-desc">this Button object</div></li>
 
73
     * <li><b>state</b> : Boolean<div class="sub-desc">The next state if the Button, true means pressed.</div></li>
 
74
     * </ul>
 
75
     */
 
76
    /**
 
77
     * @cfg {Mixed} menu
 
78
     * Standard menu attribute consisting of a reference to a menu object, a menu id or a menu config blob (defaults to undefined).
 
79
     */
 
80
    /**
 
81
     * @cfg {String} menuAlign
 
82
     * The position to align the menu to (see {@link Ext.Element#alignTo} for more details, defaults to 'tl-bl?').
 
83
     */
 
84
    menuAlign : "tl-bl?",
 
85
 
 
86
    /**
 
87
     * @cfg {String} iconCls
 
88
     * A css class which sets a background image to be used as the icon for this button
 
89
     */
 
90
    /**
 
91
     * @cfg {String} type
 
92
     * submit, reset or button - defaults to 'button'
 
93
     */
 
94
    type : 'button',
 
95
 
 
96
    // private
 
97
    menuClassTarget: 'tr',
 
98
 
 
99
    /**
 
100
     * @cfg {String} clickEvent
 
101
     * The type of event to map to the button's event handler (defaults to 'click')
 
102
     */
 
103
    clickEvent : 'click',
 
104
 
 
105
    /**
 
106
     * @cfg {Boolean} handleMouseEvents
 
107
     * False to disable visual cues on mouseover, mouseout and mousedown (defaults to true)
 
108
     */
 
109
    handleMouseEvents : true,
 
110
 
 
111
    /**
 
112
     * @cfg {String} tooltipType
 
113
     * The type of tooltip to use. Either "qtip" (default) for QuickTips or "title" for title attribute.
 
114
     */
 
115
    tooltipType : 'qtip',
 
116
 
 
117
    buttonSelector : "button:first",
 
118
 
 
119
    /**
 
120
     * @cfg {Ext.Template} template (Optional)
 
121
     * An {@link Ext.Template} with which to create the Button's main element. This Template must
 
122
     * contain numeric substitution parameter 0 if it is to display the text property. Changing the template could
 
123
     * require code modifications if required elements (e.g. a button) aren't present.
 
124
     */
 
125
    /**
 
126
     * @cfg {String} cls
 
127
     * A CSS class string to apply to the button's main element.
 
128
     */
 
129
 
 
130
    initComponent : function(){
 
131
        Ext.Button.superclass.initComponent.call(this);
 
132
 
 
133
        this.addEvents(
 
134
            /**
 
135
             * @event click
 
136
             * Fires when this button is clicked
 
137
             * @param {Button} this
 
138
             * @param {EventObject} e The click event
 
139
             */
 
140
            "click",
 
141
            /**
 
142
             * @event toggle
 
143
             * Fires when the "pressed" state of this button changes (only if enableToggle = true)
 
144
             * @param {Button} this
 
145
             * @param {Boolean} pressed
 
146
             */
 
147
            "toggle",
 
148
            /**
 
149
             * @event mouseover
 
150
             * Fires when the mouse hovers over the button
 
151
             * @param {Button} this
 
152
             * @param {Event} e The event object
 
153
             */
 
154
            'mouseover',
 
155
            /**
 
156
             * @event mouseout
 
157
             * Fires when the mouse exits the button
 
158
             * @param {Button} this
 
159
             * @param {Event} e The event object
 
160
             */
 
161
            'mouseout',
 
162
            /**
 
163
             * @event menushow
 
164
             * If this button has a menu, this event fires when it is shown
 
165
             * @param {Button} this
 
166
             * @param {Menu} menu
 
167
             */
 
168
            'menushow',
 
169
            /**
 
170
             * @event menuhide
 
171
             * If this button has a menu, this event fires when it is hidden
 
172
             * @param {Button} this
 
173
             * @param {Menu} menu
 
174
             */
 
175
            'menuhide',
 
176
            /**
 
177
             * @event menutriggerover
 
178
             * If this button has a menu, this event fires when the mouse enters the menu triggering element
 
179
             * @param {Button} this
 
180
             * @param {Menu} menu
 
181
             * @param {EventObject} e
 
182
             */
 
183
            'menutriggerover',
 
184
            /**
 
185
             * @event menutriggerout
 
186
             * If this button has a menu, this event fires when the mouse leaves the menu triggering element
 
187
             * @param {Button} this
 
188
             * @param {Menu} menu
 
189
             * @param {EventObject} e
 
190
             */
 
191
            'menutriggerout'
 
192
        );
 
193
        if(this.menu){
 
194
            this.menu = Ext.menu.MenuMgr.get(this.menu);
 
195
        }
 
196
        if(typeof this.toggleGroup === 'string'){
 
197
            this.enableToggle = true;
 
198
        }
 
199
    },
 
200
 
 
201
    // private
 
202
    onRender : function(ct, position){
 
203
        if(!this.template){
 
204
            if(!Ext.Button.buttonTemplate){
 
205
                // hideous table template
 
206
                Ext.Button.buttonTemplate = new Ext.Template(
 
207
                    '<table border="0" cellpadding="0" cellspacing="0" class="x-btn-wrap"><tbody><tr>',
 
208
                    '<td class="x-btn-left"><i>&#160;</i></td><td class="x-btn-center"><em unselectable="on"><button class="x-btn-text" type="{1}">{0}</button></em></td><td class="x-btn-right"><i>&#160;</i></td>',
 
209
                    "</tr></tbody></table>");
 
210
            }
 
211
            this.template = Ext.Button.buttonTemplate;
 
212
        }
 
213
        var btn, targs = [this.text || '&#160;', this.type];
 
214
 
 
215
        if(position){
 
216
            btn = this.template.insertBefore(position, targs, true);
 
217
        }else{
 
218
            btn = this.template.append(ct, targs, true);
 
219
        }
 
220
        var btnEl = btn.child(this.buttonSelector);
 
221
        btnEl.on('focus', this.onFocus, this);
 
222
        btnEl.on('blur', this.onBlur, this);
 
223
 
 
224
        this.initButtonEl(btn, btnEl);
 
225
 
 
226
        if(this.menu){
 
227
            this.el.child(this.menuClassTarget).addClass("x-btn-with-menu");
 
228
        }
 
229
        Ext.ButtonToggleMgr.register(this);
 
230
    },
 
231
 
 
232
    // private
 
233
    initButtonEl : function(btn, btnEl){
 
234
 
 
235
        this.el = btn;
 
236
        btn.addClass("x-btn");
 
237
 
 
238
        if(this.icon){
 
239
            btnEl.setStyle('background-image', 'url(' +this.icon +')');
 
240
        }
 
241
        if(this.iconCls){
 
242
            btnEl.addClass(this.iconCls);
 
243
            if(!this.cls){
 
244
                btn.addClass(this.text ? 'x-btn-text-icon' : 'x-btn-icon');
 
245
            }
 
246
        }
 
247
        if(this.tabIndex !== undefined){
 
248
            btnEl.dom.tabIndex = this.tabIndex;
 
249
        }
 
250
        if(this.tooltip){
 
251
            if(typeof this.tooltip == 'object'){
 
252
                Ext.QuickTips.register(Ext.apply({
 
253
                      target: btnEl.id
 
254
                }, this.tooltip));
 
255
            } else {
 
256
                btnEl.dom[this.tooltipType] = this.tooltip;
 
257
            }
 
258
        }
 
259
 
 
260
        if(this.pressed){
 
261
            this.el.addClass("x-btn-pressed");
 
262
        }
 
263
 
 
264
        if(this.handleMouseEvents){
 
265
            btn.on("mouseover", this.onMouseOver, this);
 
266
            // new functionality for monitoring on the document level
 
267
            //btn.on("mouseout", this.onMouseOut, this);
 
268
            btn.on("mousedown", this.onMouseDown, this);
 
269
        }
 
270
 
 
271
        if(this.menu){
 
272
            this.menu.on("show", this.onMenuShow, this);
 
273
            this.menu.on("hide", this.onMenuHide, this);
 
274
        }
 
275
 
 
276
        if(this.id){
 
277
            this.el.dom.id = this.el.id = this.id;
 
278
        }
 
279
 
 
280
        if(this.repeat){
 
281
            var repeater = new Ext.util.ClickRepeater(btn,
 
282
                typeof this.repeat == "object" ? this.repeat : {}
 
283
            );
 
284
            repeater.on("click", this.onClick,  this);
 
285
        }
 
286
 
 
287
        btn.on(this.clickEvent, this.onClick, this);
 
288
    },
 
289
 
 
290
    // private
 
291
    afterRender : function(){
 
292
        Ext.Button.superclass.afterRender.call(this);
 
293
        if(Ext.isIE6){
 
294
            this.autoWidth.defer(1, this);
 
295
        }else{
 
296
            this.autoWidth();
 
297
        }
 
298
    },
 
299
 
 
300
    /**
 
301
     * Sets the CSS class that provides a background image to use as the button's icon.  This method also changes
 
302
     * the value of the {@link iconCls} config internally.
 
303
     * @param {String} cls The CSS class providing the icon image
 
304
     */
 
305
    setIconClass : function(cls){
 
306
        if(this.el){
 
307
            this.el.child(this.buttonSelector).replaceClass(this.iconCls, cls);
 
308
        }
 
309
        this.iconCls = cls;
 
310
    },
 
311
 
 
312
    // private
 
313
    beforeDestroy: function(){
 
314
        if(this.rendered){
 
315
                var btn = this.el.child(this.buttonSelector);
 
316
                if(btn){
 
317
                    btn.removeAllListeners();
 
318
                }
 
319
            }
 
320
        if(this.menu){
 
321
            Ext.destroy(this.menu);
 
322
        }
 
323
    },
 
324
 
 
325
    // private
 
326
    onDestroy : function(){
 
327
        if(this.rendered){
 
328
            Ext.ButtonToggleMgr.unregister(this);
 
329
        }
 
330
    },
 
331
 
 
332
    // private
 
333
    autoWidth : function(){
 
334
        if(this.el){
 
335
            this.el.setWidth("auto");
 
336
            if(Ext.isIE7 && Ext.isStrict){
 
337
                var ib = this.el.child(this.buttonSelector);
 
338
                if(ib && ib.getWidth() > 20){
 
339
                    ib.clip();
 
340
                    ib.setWidth(Ext.util.TextMetrics.measure(ib, this.text).width+ib.getFrameWidth('lr'));
 
341
                }
 
342
            }
 
343
            if(this.minWidth){
 
344
                if(this.el.getWidth() < this.minWidth){
 
345
                    this.el.setWidth(this.minWidth);
 
346
                }
 
347
            }
 
348
        }
 
349
    },
 
350
 
 
351
    /**
 
352
     * Assigns this button's click handler
 
353
     * @param {Function} handler The function to call when the button is clicked
 
354
     * @param {Object} scope (optional) Scope for the function passed in
 
355
     */
 
356
    setHandler : function(handler, scope){
 
357
        this.handler = handler;
 
358
        this.scope = scope;
 
359
    },
 
360
 
 
361
    /**
 
362
     * Sets this button's text
 
363
     * @param {String} text The button text
 
364
     */
 
365
    setText : function(text){
 
366
        this.text = text;
 
367
        if(this.el){
 
368
            this.el.child("td.x-btn-center " + this.buttonSelector).update(text);
 
369
        }
 
370
        this.autoWidth();
 
371
    },
 
372
 
 
373
    /**
 
374
     * Gets the text for this button
 
375
     * @return {String} The button text
 
376
     */
 
377
    getText : function(){
 
378
        return this.text;
 
379
    },
 
380
 
 
381
    /**
 
382
     * If a state it passed, it becomes the pressed state otherwise the current state is toggled.
 
383
     * @param {Boolean} state (optional) Force a particular state
 
384
     */
 
385
    toggle : function(state){
 
386
        state = state === undefined ? !this.pressed : state;
 
387
        if(state != this.pressed){
 
388
            if(state){
 
389
                this.el.addClass("x-btn-pressed");
 
390
                this.pressed = true;
 
391
                this.fireEvent("toggle", this, true);
 
392
            }else{
 
393
                this.el.removeClass("x-btn-pressed");
 
394
                this.pressed = false;
 
395
                this.fireEvent("toggle", this, false);
 
396
            }
 
397
            if(this.toggleHandler){
 
398
                this.toggleHandler.call(this.scope || this, this, state);
 
399
            }
 
400
        }
 
401
    },
 
402
 
 
403
    /**
 
404
     * Focus the button
 
405
     */
 
406
    focus : function(){
 
407
        this.el.child(this.buttonSelector).focus();
 
408
    },
 
409
 
 
410
    // private
 
411
    onDisable : function(){
 
412
        if(this.el){
 
413
            if(!Ext.isIE6 || !this.text){
 
414
                this.el.addClass(this.disabledClass);
 
415
            }
 
416
            this.el.dom.disabled = true;
 
417
        }
 
418
        this.disabled = true;
 
419
    },
 
420
 
 
421
    // private
 
422
    onEnable : function(){
 
423
        if(this.el){
 
424
            if(!Ext.isIE6 || !this.text){
 
425
                this.el.removeClass(this.disabledClass);
 
426
            }
 
427
            this.el.dom.disabled = false;
 
428
        }
 
429
        this.disabled = false;
 
430
    },
 
431
 
 
432
    /**
 
433
     * Show this button's menu (if it has one)
 
434
     */
 
435
    showMenu : function(){
 
436
        if(this.menu){
 
437
            this.menu.show(this.el, this.menuAlign);
 
438
        }
 
439
        return this;
 
440
    },
 
441
 
 
442
    /**
 
443
     * Hide this button's menu (if it has one)
 
444
     */
 
445
    hideMenu : function(){
 
446
        if(this.menu){
 
447
            this.menu.hide();
 
448
        }
 
449
        return this;
 
450
    },
 
451
 
 
452
    /**
 
453
     * Returns true if the button has a menu and it is visible
 
454
     * @return {Boolean}
 
455
     */
 
456
    hasVisibleMenu : function(){
 
457
        return this.menu && this.menu.isVisible();
 
458
    },
 
459
 
 
460
    // private
 
461
    onClick : function(e){
 
462
        if(e){
 
463
            e.preventDefault();
 
464
        }
 
465
        if(e.button != 0){
 
466
            return;
 
467
        }
 
468
        if(!this.disabled){
 
469
            if(this.enableToggle && (this.allowDepress !== false || !this.pressed)){
 
470
                this.toggle();
 
471
            }
 
472
            if(this.menu && !this.menu.isVisible() && !this.ignoreNextClick){
 
473
                this.showMenu();
 
474
            }
 
475
            this.fireEvent("click", this, e);
 
476
            if(this.handler){
 
477
                //this.el.removeClass("x-btn-over");
 
478
                this.handler.call(this.scope || this, this, e);
 
479
            }
 
480
        }
 
481
    },
 
482
 
 
483
    // private
 
484
    isMenuTriggerOver : function(e, internal){
 
485
        return this.menu && !internal;
 
486
    },
 
487
 
 
488
    // private
 
489
    isMenuTriggerOut : function(e, internal){
 
490
        return this.menu && !internal;
 
491
    },
 
492
 
 
493
    // private
 
494
    onMouseOver : function(e){
 
495
        if(!this.disabled){
 
496
            var internal = e.within(this.el,  true);
 
497
            if(!internal){
 
498
                this.el.addClass("x-btn-over");
 
499
                if(!this.monitoringMouseOver){
 
500
                    Ext.getDoc().on('mouseover', this.monitorMouseOver, this);
 
501
                    this.monitoringMouseOver = true;
 
502
                }
 
503
                this.fireEvent('mouseover', this, e);
 
504
            }
 
505
            if(this.isMenuTriggerOver(e, internal)){
 
506
                this.fireEvent('menutriggerover', this, this.menu, e);
 
507
            }
 
508
        }
 
509
    },
 
510
 
 
511
    // private
 
512
    monitorMouseOver : function(e){
 
513
        if(e.target != this.el.dom && !e.within(this.el)){
 
514
            if(this.monitoringMouseOver){
 
515
                Ext.getDoc().un('mouseover', this.monitorMouseOver, this);
 
516
                this.monitoringMouseOver = false;
 
517
            }
 
518
            this.onMouseOut(e);
 
519
        }
 
520
    },
 
521
 
 
522
    // private
 
523
    onMouseOut : function(e){
 
524
        var internal = e.within(this.el) && e.target != this.el.dom;
 
525
        this.el.removeClass("x-btn-over");
 
526
        this.fireEvent('mouseout', this, e);
 
527
        if(this.isMenuTriggerOut(e, internal)){
 
528
            this.fireEvent('menutriggerout', this, this.menu, e);
 
529
        }
 
530
    },
 
531
    // private
 
532
    onFocus : function(e){
 
533
        if(!this.disabled){
 
534
            this.el.addClass("x-btn-focus");
 
535
        }
 
536
    },
 
537
    // private
 
538
    onBlur : function(e){
 
539
        this.el.removeClass("x-btn-focus");
 
540
    },
 
541
 
 
542
    // private
 
543
    getClickEl : function(e, isUp){
 
544
       return this.el;
 
545
    },
 
546
 
 
547
    // private
 
548
    onMouseDown : function(e){
 
549
        if(!this.disabled && e.button == 0){
 
550
            this.getClickEl(e).addClass("x-btn-click");
 
551
            Ext.getDoc().on('mouseup', this.onMouseUp, this);
 
552
        }
 
553
    },
 
554
    // private
 
555
    onMouseUp : function(e){
 
556
        if(e.button == 0){
 
557
            this.getClickEl(e, true).removeClass("x-btn-click");
 
558
            Ext.getDoc().un('mouseup', this.onMouseUp, this);
 
559
        }
 
560
    },
 
561
    // private
 
562
    onMenuShow : function(e){
 
563
        this.ignoreNextClick = 0;
 
564
        this.el.addClass("x-btn-menu-active");
 
565
        this.fireEvent('menushow', this, this.menu);
 
566
    },
 
567
    // private
 
568
    onMenuHide : function(e){
 
569
        this.el.removeClass("x-btn-menu-active");
 
570
        this.ignoreNextClick = this.restoreClick.defer(250, this);
 
571
        this.fireEvent('menuhide', this, this.menu);
 
572
    },
 
573
 
 
574
    // private
 
575
    restoreClick : function(){
 
576
        this.ignoreNextClick = 0;
 
577
    }
 
578
 
 
579
 
 
580
 
 
581
    /**
 
582
     * @cfg {String} autoEl @hide
 
583
     */
 
584
});
 
585
Ext.reg('button', Ext.Button);
 
586
 
 
587
// Private utility class used by Button
 
588
Ext.ButtonToggleMgr = function(){
 
589
   var groups = {};
 
590
 
 
591
   function toggleGroup(btn, state){
 
592
       if(state){
 
593
           var g = groups[btn.toggleGroup];
 
594
           for(var i = 0, l = g.length; i < l; i++){
 
595
               if(g[i] != btn){
 
596
                   g[i].toggle(false);
 
597
               }
 
598
           }
 
599
       }
 
600
   }
 
601
 
 
602
   return {
 
603
       register : function(btn){
 
604
           if(!btn.toggleGroup){
 
605
               return;
 
606
           }
 
607
           var g = groups[btn.toggleGroup];
 
608
           if(!g){
 
609
               g = groups[btn.toggleGroup] = [];
 
610
           }
 
611
           g.push(btn);
 
612
           btn.on("toggle", toggleGroup);
 
613
       },
 
614
 
 
615
       unregister : function(btn){
 
616
           if(!btn.toggleGroup){
 
617
               return;
 
618
           }
 
619
           var g = groups[btn.toggleGroup];
 
620
           if(g){
 
621
               g.remove(btn);
 
622
               btn.un("toggle", toggleGroup);
 
623
           }
 
624
       }
 
625
   };
 
626
}();
 
 
b'\\ No newline at end of file'