notify helper: Only include one
[ninja.git] / application / media / js / jquery.notify.js
blob2714c470a0563a9855a5dbbd70cad1689f65eca0
2 ( function ( $, window ) {
4 'use strict';
6 /*
7 Syntax:
9 $.notify( string message [, object options ] )
11 To create a notification popup you may simply state:
13 $.notify( "Where this is the message you wish to prompt!" )
16 var settings = {
18 /* Can be info, warning, critical. Will grant the
19 notification-body the classes: jq-notify-type-<type> */
20 "type": "info",
22 /* true/false, A sticky notification does not fade out and
23 must be removed manually */
24 "sticky": false,
26 /* true/false, Should the notification have a remove button
27 in the upper right corner */
28 "removable": true,
30 /* Milliseconds before a non-sticky notification fades out,
31 set to string "auto" and it will fade out depending on the
32 length of the message. */
33 "fadetime": "auto",
35 /* Should the notification be configurable, i.e. should
36 it be possible to remove this type of notification for
37 the "duration of the session"/"this user" */
38 "configurable": false,
40 /* Buttons can be added to the notification, set the buttons
41 property of the options to an object where each key will
42 be used as title and the value used as the callback */
43 "buttons": false,
45 /**/
46 "signature": false,
48 /**/
49 "remove": false
53 var config_options = {
54 "always_show": {
55 "title": "This notification will show on every page it applies to",
56 "name": "Always show"
60 var zone,
61 notifications = {},
62 template = $( '<div class="jq-notify"><div class="jq-notify-body"></div></div>' ),
63 tpl_buttons = $( '<div class="jq-notify-buttons"></div>' ),
64 tpl_tools = $( '<div class="jq-notify-tools"></div>' ),
65 tpl_remove = $( '<div class="jq-notify-remover">x</div>' ),
66 tpl_config = $( '<form><select class="jq-notify-config" title="Set when this notification is shown">' +
67 '<option value="always_show" title="This notification will show on every page it applies to">Always show</option>' +
68 '<option value="hide_time_15" title="This notification will be hidden for 15 minutes">Hide for 15 minutes</option>' +
69 '<option value="hide_time_60" title="This notification will be hidden for 1 hour">Hide for 1 hour</option>' +
70 '<option value="session_hide" title="This notification will remain hidden until the end of the session">Hide for this session</option>' +
71 '<option value="always_hide" title="This notification will never show for you, can be reset under My Account">Always hide</option>' +
72 '</select></form>' ),
73 tpl_button = $( '<button>' ),
74 basepath = false;
76 $("document").ready( function () {
77 zone = $(".jq-notify-zone");
78 } );
80 var Notification = function ( message, options ) {
82 var opts = $.extend( {}, settings ),
83 opt;
85 this.message = message;
86 this.display = "always_show";
87 this.sessionid = "";
88 this.signature = ( options && options.signature ) ? options.signature : this.sign( );
90 if ( $.notify.configured[ this.signature ] ) {
92 var matches,
93 cfg = $.notify.configured[ this.signature ];
95 if ( cfg.display === "session_hide" && cfg.sessionid === $.notify.sessionid ) {
96 return false;
97 } else if ( cfg.display === "always_hide" ) {
98 return false;
99 } else if ( matches = cfg.display.match( /hide_time_(\d+)/ ) ) {
101 var time = cfg.timestamp + ( parseInt( matches[1], 10 ) * 60 * 1000 ),
102 now = Date.now();
104 if ( time - now > 0) {
105 return false;
112 this.wrapper = template.clone();
113 this.body = this.wrapper.find( ".jq-notify-body" );
114 this.btnbar = tpl_buttons.clone();
115 this.toolbar = tpl_tools.clone();
117 if ( typeof( options ) != "undefined" && typeof( options ) == "object" ) {
118 for ( opt in opts ) {
119 if ( typeof(options[ opt ]) != "undefined" )
120 opts[opt] = options[ opt ];
122 } else if ( typeof( options ) == "string" ) {
123 opts.type = options.toLowerCase();
126 this.wrapper.attr( "data-signature", this.signature );
128 this.options = opts;
129 this.apply();
131 this.body.append( "<p>" + this.message + "</p>" );
132 this.body.append( this.toolbar );
134 zone.css( "display", "block" );
135 zone.prepend( this.wrapper );
137 this.wrapper.slideDown( 200 );
138 notifications[ this.signature ] = this;
140 return this;
144 Notification.prototype = {
146 wrapper: false,
147 body: false,
148 btnbar: false,
149 toolbar: false,
151 /* DON'T CHANGE, IT WILL INVALIDATE SETTINGS FOR
152 AUTO-SIGNED NOTIFICATIONS */
153 sign: function () {
155 var reduce = function ( a, p ) {
157 while ( a.length > 64 ) {
158 p = ( a.length / 3 ) | 0;
159 a[ p ] = a[ p ] + a.splice( p * 2, 1 )[0];
160 a[ p ] = a[ p ] & a[ p ];
162 return a;
166 var s = this.message;
167 while ( s.length < 128 ) s += s;
169 var h = [], i = s.length;
171 for ( i; i--; ) h.push( s.charCodeAt( i ) );
172 h = reduce( h );
174 for ( i = h.length; i--; ) {
175 h[i] = ( (h[i] > 300) ? (h[i] / 2) | 0 : h[i] ).toString( 36 );
176 h[i] = ( i % 2 == 0 ) ? h[i].toUpperCase() : h[i];
179 return h.join( "" ).substr( 32, 64 );
183 remove: function () {
185 if ( basepath === false ) {
186 basepath = _site_domain + _index_page;
189 var self = this;
190 this.wrapper.slideUp( 200, function () {
192 self.wrapper.remove();
193 if ( self.options.remove !== false && typeof( self.options.remove ) == "function" ) {
194 self.options.remove();
197 if ( zone.children().length == 0 ) {
198 zone.css( "display", "none" );
201 var note, blob = $.notify.configured;
202 for ( var signature in notifications ) {
204 note = notifications[ signature ];
205 blob[ signature ] = {
206 "display": note.display,
207 "sessionid": note.sessionid,
208 "timestamp": Date.now()
213 $.ajax( basepath + '/ajax/save_page_setting', {
215 data: {
216 'type': 'notifications',
217 'page': 'notifications_facility',
218 'setting': JSON.stringify( blob )
221 type: 'POST',
222 complete: function ( request ) {
232 getAutoFadetime: function ( ) {
234 var size = this.message.split( " " ).length,
235 time = 500 + ( size * 500 );
236 time = ( time > 4000 ) ? 4000 : time;
237 return time;
241 apply: function () {
243 var button, title, self = this;
245 if ( this.options && this.options.buttons && typeof(this.options.buttons) === "object" ) {
247 for ( title in this.options.buttons ) {
248 button = tpl_button.clone().append( title );
249 button.bind( "click", this.options.buttons[ title ] );
250 this.btnbar.append( button );
253 this.options.sticky = true;
257 this.body.append( this.btnbar );
258 this.body.addClass( "jq-notify-type-" + this.options.type );
260 var configer;
261 if ( this.options.configurable !== false && this.options.removable !== false ) {
263 configer = tpl_config.clone();
264 configer.find("select").bind( "change", function ( e ) {
265 var value = this.value;
266 self.display = value;
267 self.sessionid = $.notify.sessionid;
268 } );
270 this.toolbar.append( configer );
274 if ( this.options.removable !== false ) {
276 this.toolbar.append( tpl_remove.clone() );
278 this.toolbar.bind( "click", function ( e ) {
279 var target = $( e.target );
280 if ( target.hasClass( "jq-notify-remover" ) )
281 self.remove();
282 } );
286 if ( this.options.sticky === false ) {
288 if ( this.options.fadetime === "auto" )
289 this.options.fadetime = this.getAutoFadetime();
291 var timer = setTimeout( function () {
292 self.remove();
293 clearTimeout( timer );
294 }, this.options.fadetime );
302 $.notify = function ( message, options ) {
303 return ( new Notification( message, options ) );
306 $.notify.sessionid = "";
307 $.notify.configured = {};
309 })( jQuery, window );