Merge branch 'w41_MDL-28980_m24_expirywarning' of git://github.com/skodak/moodle
[moodle.git] / calendar / yui / eventmanager / eventmanager.js
blob769d7cb10c18cb25baf366921d9b137e93458bfc
1 YUI.add('moodle-calendar-eventmanager', function(Y) {
3     var ENAME = 'Calendar event',
4         EVENTID = 'eventId',
5         EVENTNODE = 'node',
6         EVENTTITLE = 'title',
7         EVENTCONTENT = 'content',
8         EVENTDELAY = 'delay',
9         SHOWTIMEOUT = 'showTimeout',
10         HIDETIMEOUT = 'hideTimeout';
12     var EVENT = function(config) {
13         EVENT.superclass.constructor.apply(this, arguments);
14     }
15     Y.extend(EVENT, Y.Base, {
16         initpanelcalled : false,
17         initializer : function(config){
18             var id = this.get(EVENTID), node = this.get(EVENTNODE);
19             if (!node) {
20                 return false;
21             }
22             var td = node.ancestor('td');
23             this.publish('showevent');
24             this.publish('hideevent');
25             td.on('mouseenter', this.startShow, this);
26             td.on('mouseleave', this.startHide, this);
27             td.on('focus', this.startShow, this);
28             td.on('blur', this.startHide, this);
29             return true;
30         },
31         initPanel : function() {
32             if (!this.initpanelcalled) {
33                 this.initpanelcalled = true;
34                 var node = this.get(EVENTNODE),
35                     td = node.ancestor('td'),
36                     constraint = td.ancestor('div'),
37                     panel;
38                 panel = new Y.Overlay({
39                     constrain : constraint,
40                     align : {
41                         node : td,
42                         points:[Y.WidgetPositionAlign.TL, Y.WidgetPositionAlign.BC]
43                     },
44                     headerContent : Y.Node.create('<h2 class="eventtitle">'+this.get(EVENTTITLE)+'</h2>'),
45                     bodyContent : Y.Node.create('<div class="eventcontent">'+this.get(EVENTCONTENT)+'</div>'),
46                     visible : false,
47                     id : this.get(EVENTID)+'_panel',
48                     width : Math.floor(constraint.get('offsetWidth')*0.9)+"px"
49                 });
50                 panel.render(td);
51                 panel.get('boundingBox').addClass('calendar-event-panel');
52                 panel.get('boundingBox').setAttribute('aria-live', 'off');
53                 this.on('showevent', panel.show, panel);
54                 this.on('showevent', this.setAriashow, panel);
55                 this.on('hideevent', this.setAriahide, panel);
56                 this.on('hideevent', panel.hide, panel);
57             }
58         },
59         startShow : function() {
60             if (this.get(SHOWTIMEOUT) !== null) {
61                 this.cancelShow();
62             }
63             var self = this;
64             this.set(SHOWTIMEOUT, setTimeout(function(){self.show();}, this.get(EVENTDELAY)));
65         },
66         cancelShow : function() {
67             clearTimeout(this.get(SHOWTIMEOUT));
68         },
69         setAriashow : function() {
70             this.get('boundingBox').setAttribute('aria-live', 'assertive');
71         },
72         setAriahide : function() {
73              this.get('boundingBox').setAttribute('aria-live', 'off');
74         },
75         show : function() {
76             this.initPanel();
77             this.fire('showevent');
78         },
79         startHide : function() {
80             if (this.get(HIDETIMEOUT) !== null) {
81                 this.cancelHide();
82             }
83             var self = this;
84             this.set(HIDETIMEOUT, setTimeout(function(){self.hide();}, this.get(EVENTDELAY)));
85         },
86         hide : function() {
87             this.fire('hideevent');
88         },
89         cancelHide : function() {
90             clearTimeout(this.get(HIDETIMEOUT));
91         }
92     }, {
93         NAME : ENAME,
94         ATTRS : {
95             eventId : {
96                 setter : function(nodeid) {
97                     this.set(EVENTNODE, Y.one('#'+nodeid));
98                     return nodeid;
99                 },
100                 validator : Y.Lang.isString
101             },
102             node : {
103                 setter : function(node) {
104                     var n = Y.one(node);
105                     if (!n) {
106                         Y.fail(ENAME+': invalid event node set');
107                     }
108                     return n;
109                 }
110             },
111             title : {
112                 validator : Y.Lang.isString
113             },
114             content : {
115                 validator : Y.Lang.isString
116             },
117             delay : {
118                 value : 300,
119                 validator : Y.Lang.isNumber
120             },
121             showTimeout : {
122                 value : null
123             },
124             hideTimeout : {
125                 value : null
126             }
127         }
128     });
129     Y.augment(EVENT, Y.EventTarget);
131     var EVENTMANAGER = {
132         add_event : function(config) {
133             new EVENT(config);
134         }
135     }
137     M.core_calendar = M.core_calendar || {}
138     Y.mix(M.core_calendar, EVENTMANAGER);
140 }, '@VERSION@', {requires:['base', 'node', 'event-mouseenter', 'overlay', 'moodle-calendar-eventmanager-skin', 'test']});