Export 0.1.62
[platform/framework/web/web-ui-fw.git] / src / widgets / notification / js / jquery.mobile.tizen.notification.js
1 /* ***************************************************************************
2  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  * ***************************************************************************
22  *
23  *      Author: Minkyu Kang <mk7.kang@samsung.com>
24  */
25
26 /*
27  * Notification widget
28  *
29  * HTML Attributes
30  *
31  *  data-role: set to 'notification'.
32  *  data-type: 'ticker' or 'popup'.
33  *  data-interval: time to showing. If don't set, will show infinitely.
34  *
35  * APIs
36  *
37  *  open(): open the notification.
38  *  close(): close the notification.
39  *  text(text0, text1): set texts or get texts
40  *  icon(src): set the icon (tickernoti only)
41  *
42  * Events
43  *
44  *  N/A
45  *
46  * Examples
47  *
48  * // tickernoti
49  * <div data-role="notification" id="notification" data-type="ticker" data-interval="3000">
50  *      <img src="icon01.png">
51  *      <p>Hello World</p>
52  *      <p>Denis</p>
53  * </div>
54  *
55  * // smallpopup
56  * <div data-role="notification" id="notification" data-type="popup" data-interval="3000">
57  *      <p>Hello World</p>
58  * </div>
59  *
60  */
61
62 (function ( $, window ) {
63         $.widget( "tizen.notification", $.mobile.widget, {
64                 btn: null,
65                 text_bg: [],
66                 icon_img: [],
67                 interval: null,
68                 seconds: null,
69                 running: false,
70
71                 _get_text: function () {
72                         var text = new Array( 2 );
73
74                         if ( this.type === 'ticker' ) {
75                                 text[0] = $( this.text_bg[0] ).text();
76                                 text[1] = $( this.text_bg[1] ).text();
77                         } else {
78                                 text[0] = $( this.text_bg[0] ).text();
79                         }
80
81                         return text;
82                 },
83
84                 _set_text: function ( text0, text1 ) {
85                         var _set = function ( elem, text ) {
86                                 if ( !text ) {
87                                         return;
88                                 }
89                                 elem.text( text );
90                         };
91
92                         if ( this.type === 'ticker' ) {
93                                 _set( $( this.text_bg[0] ), text0 );
94                                 _set( $( this.text_bg[1] ), text1 );
95                         } else {
96                                 _set( $( this.text_bg[0] ), text0 );
97                         }
98                 },
99
100                 text: function ( text0, text1 ) {
101                         if ( text0 === undefined && text1 === undefined ) {
102                                 return this._get_text();
103                         }
104
105                         this._set_text( text0, text1 );
106                 },
107
108                 icon: function ( src ) {
109                         if ( src === undefined ) {
110                                 return;
111                         }
112
113                         this.icon_img.detach();
114                         this.icon_img = $( "<img src='" + src + "' class='ui-ticker-icon'>" );
115                         $( this.element ).find(".ui-ticker").append( this.icon_img );
116                 },
117
118                 _refresh: function () {
119                         var container = this._get_container();
120
121                         $( container ).addClass("fix")
122                                         .removeClass("show")
123                                         .removeClass("hide");
124
125                         this._set_interval();
126                 },
127
128                 open: function () {
129                         var container = this._get_container();
130
131                         if ( this.running ) {
132                                 this._refresh();
133                                 return;
134                         }
135
136                         $( container ).addClass("show")
137                                         .removeClass("hide")
138                                         .removeClass("fix");
139
140                         this.running = true;
141
142                         if ( this.type === 'popup' ) {
143                                 this._set_position();
144                         }
145
146                         this._set_interval();
147                 },
148
149                 close: function () {
150                         var container = this._get_container();
151
152                         if ( !this.running ) {
153                                 return;
154                         }
155
156                         $( container ).addClass("hide")
157                                         .removeClass("show")
158                                         .removeClass("fix");
159
160                         this.running = false;
161                         clearInterval( this.interval );
162                 },
163
164                 destroy: function () {
165                         var container = this._get_container();
166
167                         $( container ).removeClass("show")
168                                         .removeClass("hide")
169                                         .removeClass("fix");
170
171                         this._del_event();
172
173                         this.running = false;
174                 },
175
176                 _get_container: function () {
177                         if ( this.type === 'ticker' ) {
178                                 return $( this.element ).find(".ui-ticker");
179                         }
180
181                         return $( this.element ).find(".ui-smallpopup");
182                 },
183
184                 _set_interval: function () {
185                         var self = this;
186
187                         clearInterval( this.interval );
188
189                         if ( this.seconds !== undefined && this.second !== 0 ) {
190                                 this.interval = setInterval( function () {
191                                         self.close();
192                                 }, this.seconds );
193                         }
194                 },
195
196                 _add_event: function () {
197                         var self = this,
198                                 container = this._get_container();
199
200                         if ( this.type === 'ticker' ) {
201                                 container.find(".ui-ticker-btn").append( this.btn ).trigger("create");
202
203                                 this.btn.bind( "vmouseup", function () {
204                                         self.close();
205                                 });
206                         }
207
208                         container.bind( 'vmouseup', function () {
209                                 self.close();
210                         });
211                 },
212
213                 _del_event: function () {
214                         var container = this._get_container();
215
216                         if ( this.type === 'ticker' ) {
217                                 this.btn.unbind("vmouseup");
218                         }
219                         container.unbind('vmouseup');
220                         clearInterval( this.interval );
221                 },
222
223                 _set_position: function () {
224                         var container = this._get_container(),
225                                 $footer = $('.ui-page-active').children('.ui-footer'),
226                                 footer_h = $footer.outerHeight() || 0;
227
228                         container.css( 'bottom', footer_h);
229                 },
230
231                 _create: function () {
232                         var self = this,
233                                 elem = $( this.element ),
234                                 i;
235
236                         this.btn = $('<div data-role="button" data-inline="true">Close</div>');
237
238                         this.seconds = elem.jqmData('interval');
239                         this.type = elem.jqmData('type') || 'popup';
240
241                         if ( this.type === 'ticker' ) {
242                                 elem.wrapInner("<div class='ui-ticker'></div>");
243                                 elem.find(".ui-ticker").append("<div class='ui-ticker-body'></div>" +
244                                                         "<div class='ui-ticker-btn'></div>");
245                                 this.text_bg = elem.find("p");
246
247                                 if ( this.text_bg.length < 2 ) {
248                                         elem.find(".ui-ticker").append("<p></p><p></p>");
249                                         this.text_bg = elem.find("p");
250                                 } else if ( this.text_bg.length > 2 ) {
251                                         for ( i = 2; i < this.text_bg.length; i++ ) {
252                                                 $( this.text_bg[i] ).css( "display", "none" );
253                                         }
254                                 }
255
256                                 $( this.text_bg[0] ).addClass("ui-ticker-text1-bg");
257                                 $( this.text_bg[1] ).addClass("ui-ticker-text2-bg");
258
259                                 this.icon_img = elem.find("img");
260
261                                 if ( this.icon_img.length ) {
262                                         $( this.icon_img ).addClass("ui-ticker-icon");
263
264                                         for ( i = 1; i < this.icon_img.length; i++ ) {
265                                                 $( this.icon_img[i] ).css( "display", "none" );
266                                         }
267                                 }
268                         } else {
269                                 elem.wrapInner("<div class='ui-smallpopup'></div>");
270                                 this.text_bg = elem.find("p").addClass("ui-smallpopup-text-bg");
271
272                                 if ( this.text_bg.length < 1 ) {
273                                         elem.find(".ui-smallpopup")
274                                                 .append("<p class='ui-smallpopup-text-bg'></p>");
275                                         this.text_bg = elem.find("p");
276                                 } else if ( this.text_bg.length > 1 ) {
277                                         for ( i = 1; i < this.text_bg.length; i++ ) {
278                                                 $( this.text_bg[i] ).css( "display", "none" );
279                                         }
280                                 }
281
282                                 this._set_position();
283                         }
284
285                         this._add_event();
286
287                         $( window ).bind( "resize", function () {
288                                 if ( !self.running ) {
289                                         return;
290                                 }
291
292                                 self._refresh();
293
294                                 if ( self.type === 'popup' ) {
295                                         self._set_position();
296                                 }
297                         });
298                 }
299         }); // End of widget
300
301         // auto self-init widgets
302         $( document ).bind( "pagecreate create", function ( e ) {
303                 $( e.target ).find(":jqmData(role='notification')").notification();
304         });
305
306         $( document ).bind( "pagebeforehide", function ( e ) {
307                 $( e.target ).find(":jqmData(role='notification')").notification('destroy');
308         });
309 }( jQuery, this ));