1/** 2 * notify - jQuery notification system 3 * v2.0 4 * 5 * @author Sujeet <[email protected]> 6 * @link https://github.com/sujeet-kumar/jquery-notify 7 */ 8 9(function ($) { 10 $.notify = (function () { 11 var color = { 12 'info': '#31708F', 13 'success': '#3C763D', 14 'warning': '#8A6D3B', 15 'error': '#A94442' 16 }; 17 18 var bg_color = { 19 'info': '#D9EDF7', 20 'success': '#DFF0D8', 21 'warning': '#FCF8E3', 22 'error': '#F2DEDE' 23 }; 24 25 var settings = { 26 'app_title': '', 27 'app_icon': '', 28 'position': { 29 'ver': 'bottom', 30 'hor': 'right' 31 }, 32 'z_index': '2048', 33 'container_layout': '<div style="padding:5px; margin:0px; width:400px;"></div>', 34 'message_layout': '<div style="border-radius:8px; box-shadow:0 0 5px rgba(51, 51, 51, 0.4); font-weight:bold; padding:10px; margin:8px 5px;"></div>', 35 'service_worker_path': '', 36 'verbose_mode': false 37 }; 38 39 var log = function (msg) { 40 if (settings.verbose_mode && console && 'log' in console) { 41 console.log('notify.js:> ' + msg); 42 } 43 }; 44 45 var container = function () { 46 if (!$('div#__notifycontainer').length) { 47 var $container_layout = $(settings.container_layout); 48 $container_layout.attr('id', '__notifycontainer'); 49
50 var container_css = { 51 'z-index': settings.z_index, 52 'position': 'fixed' 53 }; 54 container_css[settings.position.ver] = '0'; 55 container_css[settings.position.hor] = '0'; 56 $container_layout.css(container_css); 57 58 $('body').append($container_layout); 59 } 60 return $('div#__notifycontainer'); 61 }; 62 63 var remove = function ($element) { 64 $element.fadeOut('fast', function () { 65 $(this).remove(); 66 }); 67 }; 68 69 var remove_msg = function ($msg_element) { 70 if (container().children().length < 2) { 71 remove(container()); 72 } else { 73 remove($msg_element); 74 } 75 }; 76 77 var template = function (type, msg, temp) { 78 temp = (typeof temp == 'undefined') ? true : temp; 79 80 var $tpl = $(settings.message_layout); 81 $tpl.html(msg); 82 $tpl.addClass('__notify_msg ' + type); 83 $tpl.css({'color': color[type], 'background-color': bg_color[type], 'border': '1px solid ' + color[type]}); 84 85 if (temp) { 86 $tpl.css({'cursor': 'pointer'}); 87 $tpl.click(function (e) { 88 e.stopPropagation(); 89 remove_msg($(this)); 90 }); 91 92 if (parseInt(temp) > 1) { 93 setTimeout(function () { 94 remove_msg($tpl); 95 }, parseInt(temp) * 1000); 96 } 97 } 98 return $tpl; 99 }; 100 101 var default_api = { 102 'info': function (msg, temp) { 103 var $msg_tpl = template('info', msg, temp); 104 container().append($msg_tpl); 105 return { 106 'remove': function () { 107 remove_msg($msg_tpl); 108 } 109 }; 110 }, 111 'success': function (msg, temp) { 112 var $msg_tpl = template('success', msg, temp); 113 container().append($msg_tpl); 114 return { 115 'remove': function () { 116 remove_msg($msg_tpl); 117 } 118 }; 119 }, 120 'warning': function (msg, temp) { 121 var $msg_tpl = template('warning', msg, temp); 122 container().append($msg_tpl); 123 return { 124 'remove': function () { 125 remove_msg($msg_tpl); 126 } 127 }; 128 }, 129 'error': function (msg, temp) { 130 var $msg_tpl = template('error', msg, temp); 131 container().append($msg_tpl); 132 return { 133 'remove': function () { 134 remove_msg($msg_tpl); 135 } 136 }; 137 }, 138 'clear': function () { 139 remove(container()); 140 } 141 }; 142 143 var has_service_worker = false;
144 145 var permissions = { 146 DEFAULT: 'default', 147 GRANTED: 'granted', 148 DENIED: 'denied' 149 }; 150 151 var sys_alert_pool = []; 152 153 var registerServiceWorker = function (service_worker_path) { 154 if ('serviceWorker' in navigator) { 155 navigator.serviceWorker.register(service_worker_path).then(function (registration) { 156 has_service_worker = true; 157 log('serviceWorker registration successful having scope: ' + registration.scope); 158 }).catch(function (err) { 159 has_service_worker = false; 160 log('serviceWorker registration warning: ' + err); 161 }); 162 } else { 163 log('serviceWorker not supported.'); 164 } 165 }; 166 167 var notify_system = function (type, msg, temp, title, icon) { 168 temp = (typeof temp === 'undefined') ? true : temp; 169 title = title || $.trim(settings.app_title + ': ' + type.charAt(0).toUpperCase() + type.substr(1)); 170 icon = icon || settings.app_icon; 171 172 var options = {body: msg}; 173 174 if (icon) { 175 options.icon = icon; 176 } 177 178 if (!temp) { 179 options.requireInteraction = true; 180 } 181 182 if (has_service_worker === true) { 183 navigator.serviceWorker.ready.then(function (registration) { 184 185 registration.showNotification(title, options).then(function () { 186 187 registration.getNotifications().then(function (notifications) { 188 if (notifications.length) { 189 var n = notifications[(notifications.length - 1)]; 190 if (parseInt(temp) > 1) { 191 setTimeout(n.close.bind(n), parseInt(temp) * 1000); 192 } 193 sys_alert_pool.push(n); 194 } 195 }); 196 197 }); 198 199 }); 200 } else { 201 var n = new Notification(title, options); 202 if (parseInt(temp) > 1) { 203 setTimeout(n.close.bind(n), parseInt(temp) * 1000); 204 } 205 sys_alert_pool.push(n); 206 } 207 }; 208 209 var system_alert = function (type, msg, temp, title, icon) { 210 if ('Notification' in window) { 211 if (Notification.permission === permissions.GRANTED) { 212 notify_system(type, msg, temp, title, icon); 213 } else if (Notification.permission !== permissions.DENIED && Notification.requestPermission) { 214 Notification.requestPermission(function (permission) { 215 permissionRequestCallback(permission); 216 217 if (permission === permissions.GRANTED) { 218 notify_system(type, msg, temp, title, icon); 219 } else { 220 default_api[type](msg, temp); 221 } 222 }); 223 } else { 224 default_api[type](msg, temp); 225 log('System Notification not allowed.'); 226 } 227 } else { 228 default_api[type](msg, temp); 229 log('System Notification not supported.'); 230 } 231 }; 232 233 var system_alert_clear = function () { 234 if ('Notification' in window && Notification.permission === permissions.GRANTED) { 235 for (var i in sys_alert_pool) { 236 sys_alert_pool[i].close(); 237 } 238 } else { 239 default_api.clear(); 240 } 241 }; 242 243 default_api.system = { 244 'info': function (msg, temp, title, icon) { 245 system_alert('info', msg, temp, title, icon); 246 }, 247 'success': function (msg, temp, title, icon) { 248 system_alert('success', msg, temp, title, icon); 249 }, 250 'warning': function (msg, temp, title, icon) { 251 system_alert('warning', msg, temp, title, icon); 252 }, 253 'error': function (msg, temp, title, icon) { 254 system_alert('error', msg, temp, title, icon); 255 }, 256 'clear': function () { 257 system_alert_clear(); 258 } 259 }; 260 261 default_api.system.onPermissionGranted = function () {}; 262 default_api.system.onPermissionDenied = function () {}; 263 264 var permissionRequestCallback = function (permission, onGrantedCallback, onDeniedCallback) { 265 onGrantedCallback = onGrantedCallback || default_api.system.onPermissionGranted; 266 onDeniedCallback = onDeniedCallback || default_api.system.onPermissionDenied; 267 268 switch (permission) { 269 case permissions.GRANTED: 270 log('Permission Granted !'); 271 if (typeof onGrantedCallback === 'function') { 272 onGrantedCallback(); 273 } 274 break; 275 case permissions.DENIED: 276 log('Permission Denied !'); 277 if (typeof onDeniedCallback === 'function') { 278 onDeniedCallback(); 279 } 280 break; 281 }; 282 }; 283 284 default_api.system.requestPermission = function (onGranted, onDenied) { 285 if ('Notification' in window && Notification.permission && Notification.requestPermission) { 286 if (Notification.permission === permissions.DEFAULT) { 287 Notification.requestPermission(function (permission) { 288 permissionRequestCallback(permission, onGranted, onDenied); 289 }); 290 } else { 291 log('Permission already requested: ' + Notification.permission.toUpperCase()); 292 } 293 } else { 294 log('System Notification not supported.'); 295 } 296 }; 297 298 default_api.setup = function (options) { 299 if (options.app_title) { 300 settings.app_title = options.app_title; 301 } 302 if (options.app_icon) { 303 settings.app_icon = options.app_icon; 304 } 305 if (options.container_layout) { 306 settings.container_layout = options.container_layout; 307 } 308 if (options.message_layout) { 309 settings.message_layout = options.message_layout; 310 } 311 if (options.position && options.position.ver) { 312 settings.position.ver = options.position.ver; 313 } 314 if (options.position && options.position.hor) { 315 settings.position.hor = options.position.hor; 316 } 317 if (options.z_index) { 318 settings.z_index = options.z_index; 319 } 320 if (options.service_worker_path) { 321 registerServiceWorker(options.service_worker_path); 322 } 323 if ('verbose_mode' in options) { 324 settings.verbose_mode = !!options.verbose_mode; 325 } 326 }; 327 328 return default_api; 329 })(); 330}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.