| Conditions | 2 |
| Paths | 6 |
| Total Lines | 290 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /*! HTML5 Notification - v3.0.0 - 2016-09-19 |
||
| 21 | (function() { |
||
| 22 | /* |
||
| 23 | Safari native methods required for Notifications do NOT run in strict mode. |
||
| 24 | */ |
||
| 25 | //'use strict'; |
||
| 26 | // local variables |
||
| 27 | var PERMISSION_DEFAULT = "default"; |
||
| 28 | // The user decision is unknown; in this case the application will act as if permission was denied. |
||
| 29 | var PERMISSION_GRANTED = "granted"; |
||
| 30 | // The user has explicitly granted permission for the current origin to display system notifications. |
||
| 31 | var PERMISSION_DENIED = "denied"; |
||
| 32 | // The user has explicitly denied permission for the current origin to display system notifications. |
||
| 33 | var PERMISSION_NOTSUPPORTED = "notsupported"; |
||
| 34 | // The Notification API is not supported on current environment |
||
| 35 | // map for the old permission values |
||
| 36 | var PERMISSIONS = [ PERMISSION_GRANTED, PERMISSION_DEFAULT, PERMISSION_DENIED, PERMISSION_NOTSUPPORTED ]; |
||
| 37 | var DIRESCTIONS = [ "auto", "ltr", "rtl" ]; |
||
| 38 | /* |
||
| 39 | IE does not support Notifications in the same meaning as other modern browsers. |
||
| 40 | On the other side, IE9+(except MS Edge) implement flashing pinned site taskbar buttons. |
||
| 41 | Each time new IE Notification is create, previous flashing and icon overlay is cleared. |
||
| 42 | So, we need to keep track of the notification that calls close method. |
||
| 43 | */ |
||
| 44 | var IENotificationIndex = -1; |
||
| 45 | var IECloseNotificationEvents = [ "click", "scroll", "focus" ]; |
||
| 46 | var getIco = function(icon) { |
||
| 47 | var lastIndex = icon.lastIndexOf("."); |
||
| 48 | return (lastIndex !== -1 ? icon.substr(0, lastIndex) : icon) + ".ico"; |
||
| 49 | }; |
||
| 50 | /* |
||
| 51 | * Internal Notificaiton constructor. Keeps the original Notification |
||
| 52 | * constructor if any or use empty function constructor for browsers |
||
| 53 | * that do not support Notifications |
||
| 54 | */ |
||
| 55 | var _Notification = window.Notification || /* Opera Mobile/Android Browser */ |
||
| 56 | window.webkitNotifications && WebKitNotification || /* IE9+ pinned site */ |
||
| 57 | "external" in window && "msIsSiteMode" in window.external && window.external.msIsSiteMode() !== undefined && IENotification || /* Notifications Not supported. Return dummy constructor */ |
||
| 58 | DummyNotification; |
||
| 59 | /** |
||
| 60 | * @constructor DummyNotification |
||
| 61 | */ |
||
| 62 | function DummyNotification() { |
||
| 63 | var dummyElement = document.createElement("div"); |
||
| 64 | this.addEventListener = function(eventName, callback) { |
||
| 65 | dummyElement.addEventListener(eventName, callback.bind(this)); |
||
| 66 | }; |
||
| 67 | this.removeEventListener = function(eventName, callback) { |
||
| 68 | dummyElement.removeEventListener(eventName, callback.bind(this)); |
||
| 69 | }; |
||
| 70 | this.dispatchEvent = function(eventName) { |
||
| 71 | if (typeof eventName !== "string") { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | try { |
||
| 75 | dummyElement.dispatchEvent(new Event(eventName)); |
||
| 76 | } catch (e) { |
||
| 77 | var event = document.createEvent("Event"); |
||
| 78 | event.initEvent(eventName, false, true); |
||
| 79 | dummyElement.dispatchEvent(event); |
||
| 80 | } |
||
| 81 | }; |
||
| 82 | } |
||
| 83 | Object.defineProperty(DummyNotification, "permission", { |
||
| 84 | enumerable: true, |
||
| 85 | get: function() { |
||
| 86 | return PERMISSION_NOTSUPPORTED; |
||
| 87 | } |
||
| 88 | }); |
||
| 89 | Object.defineProperty(DummyNotification, "requestPermission", { |
||
| 90 | enumerable: true, |
||
| 91 | writable: true, |
||
| 92 | value: function(callback) { |
||
| 93 | callback(this.permission); |
||
| 94 | } |
||
| 95 | }); |
||
| 96 | /** |
||
| 97 | * @constructor IENotification |
||
| 98 | */ |
||
| 99 | function IENotification(title, options) { |
||
| 100 | DummyNotification.call(this); |
||
| 101 | var notificationIndex = IENotificationIndex; |
||
| 102 | Object.defineProperties(this, { |
||
| 103 | close: { |
||
| 104 | value: function(event) { |
||
| 105 | if (notificationIndex === IENotificationIndex) { |
||
| 106 | window.external.msSiteModeClearIconOverlay(); |
||
| 107 | // Remove close events |
||
| 108 | IECloseNotificationEvents.forEach(function(event) { |
||
| 109 | window.removeEventListener(event, this.close); |
||
| 110 | }.bind(this)); |
||
| 111 | this.dispatchEvent("click"); |
||
| 112 | this.dispatchEvent("close"); |
||
| 113 | notificationIndex = null; |
||
| 114 | } |
||
| 115 | }.bind(this) |
||
| 116 | } |
||
| 117 | }); |
||
| 118 | // Clear any previous icon overlay |
||
| 119 | this.close(); |
||
| 120 | // Set icon |
||
| 121 | if (this.icon) { |
||
| 122 | window.external.msSiteModeSetIconOverlay(getIco(this.icon), this.description || this.title); |
||
| 123 | } |
||
| 124 | // Blink icon |
||
| 125 | window.external.msSiteModeActivate(); |
||
| 126 | // Trigger show event |
||
| 127 | this.dispatchEvent("show"); |
||
| 128 | // Attach close event to window |
||
| 129 | IECloseNotificationEvents.forEach(function(event) { |
||
| 130 | window.addEventListener(event, this.close); |
||
| 131 | }.bind(this)); |
||
| 132 | // Increment notification index |
||
| 133 | notificationIndex = ++IENotificationIndex; |
||
| 134 | } |
||
| 135 | Object.defineProperty(IENotification, "permission", { |
||
| 136 | enumerable: true, |
||
| 137 | get: function() { |
||
| 138 | var isTabPinned = window.external.msIsSiteMode(); |
||
| 139 | return isTabPinned ? PERMISSION_GRANTED : PERMISSION_DENIED; |
||
| 140 | } |
||
| 141 | }); |
||
| 142 | Object.defineProperty(IENotification, "requestPermission", { |
||
| 143 | enumerable: true, |
||
| 144 | writable: true, |
||
| 145 | value: function(callback) { |
||
| 146 | return new Promise(function(resolve, reject) { |
||
| 147 | if (this.permission === PERMISSION_DENIED) { |
||
| 148 | alert(this.PERMISSION_REQUEST_MESSAGE); |
||
| 149 | } |
||
| 150 | resolve(this.permission); |
||
| 151 | }.bind(this)); |
||
| 152 | } |
||
| 153 | }); |
||
| 154 | Object.defineProperty(IENotification, "PERMISSION_REQUEST_MESSAGE", { |
||
| 155 | writable: true, |
||
| 156 | value: "IE supports notifications in pinned mode only. Pin this page on your taskbar to receive notifications." |
||
| 157 | }); |
||
| 158 | /** |
||
| 159 | * @constructor WebKitNotification |
||
| 160 | */ |
||
| 161 | function WebKitNotification() {} |
||
| 162 | Object.defineProperty(WebKitNotification, "permission", { |
||
| 163 | enumerable: true, |
||
| 164 | get: function() { |
||
| 165 | return PERMISSIONS[window.webkitNotifications.checkPermission()]; |
||
| 166 | } |
||
| 167 | }); |
||
| 168 | Object.defineProperty(WebKitNotification, "requestPermission", { |
||
| 169 | enumerable: true, |
||
| 170 | writable: true, |
||
| 171 | value: function(callback) { |
||
| 172 | return new Promise(function(resolve, reject) { |
||
| 173 | window.webkitNotifications.requestPermission(function(permission) { |
||
| 174 | resolve(permission); |
||
| 175 | }); |
||
| 176 | }); |
||
| 177 | } |
||
| 178 | }); |
||
| 179 | /* |
||
| 180 | [Safari] Safari6 do not support Notification.permission. |
||
| 181 | Instead, it support Notification.permissionLevel() |
||
| 182 | */ |
||
| 183 | if (!_Notification.permission) { |
||
| 184 | Object.defineProperty(_Notification, "permission", { |
||
| 185 | enumerable: true, |
||
| 186 | get: function() { |
||
| 187 | return _Notification.permissionLevel && _Notification.permissionLevel(); |
||
| 188 | } |
||
| 189 | }); |
||
| 190 | } |
||
| 191 | /** |
||
| 192 | * @constructor Notification |
||
| 193 | */ |
||
| 194 | function Notification(title, options) { |
||
| 195 | var dir; |
||
| 196 | var notification; |
||
| 197 | if (!arguments.length) { |
||
| 198 | throw TypeError('Failed to construct "Notification": 1 argument required, but only 0 present.'); |
||
| 199 | } |
||
| 200 | /* |
||
| 201 | Chrome display notifications when title is empty screen, but |
||
| 202 | Safari do NOT. |
||
| 203 | |||
| 204 | Set title to non-display characted in order to display notifications |
||
| 205 | in Safari as well when title is empty. |
||
| 206 | */ |
||
| 207 | if (title === "") { |
||
| 208 | title = "\b"; |
||
| 209 | } |
||
| 210 | if (arguments.length > 1 && "object" !== typeof options) { |
||
| 211 | throw TypeError('Failed to construct "Notification": parameter 2 ("options") is not an object.'); |
||
| 212 | } |
||
| 213 | dir = Object(options).dir; |
||
| 214 | if (dir !== undefined && DIRESCTIONS.indexOf(dir) === -1) { |
||
| 215 | throw TypeError('Failed to construct "Notification": The provided value "' + dir + '" is not a valid enum value of type NotificationDirection.'); |
||
| 216 | } |
||
| 217 | options = Object(options); |
||
| 218 | notification = new _Notification(title, options); |
||
| 219 | /* TODO: actions property */ |
||
| 220 | /* TODO: badge property */ |
||
| 221 | if (!notification.body) { |
||
| 222 | Object.defineProperty(notification, "body", { |
||
| 223 | value: String(options.body || "") |
||
| 224 | }); |
||
| 225 | } |
||
| 226 | if (!notification.data) { |
||
| 227 | Object.defineProperty(notification, "data", { |
||
| 228 | value: options.data || null |
||
| 229 | }); |
||
| 230 | } |
||
| 231 | if (!notification.dir) { |
||
| 232 | Object.defineProperty(notification, "dir", { |
||
| 233 | value: dir || DIRESCTIONS[0] |
||
| 234 | }); |
||
| 235 | } |
||
| 236 | if (!notification.icon) { |
||
| 237 | Object.defineProperty(notification, "icon", { |
||
| 238 | value: String(options.icon || "") |
||
| 239 | }); |
||
| 240 | } |
||
| 241 | if (!notification.lang) { |
||
| 242 | Object.defineProperty(notification, "lang", { |
||
| 243 | value: String(options.lang || "") |
||
| 244 | }); |
||
| 245 | } |
||
| 246 | /* TODO: noscreen property */ |
||
| 247 | /* TODO: renotify property */ |
||
| 248 | if (!notification.requireInteraction) { |
||
| 249 | Object.defineProperty(notification, "requireInteraction", { |
||
| 250 | value: Boolean(options.requireInteraction) |
||
| 251 | }); |
||
| 252 | } |
||
| 253 | /* TODO: sound property */ |
||
| 254 | if (!notification.silent) { |
||
| 255 | Object.defineProperty(notification, "silent", { |
||
| 256 | value: Boolean(options.silent) |
||
| 257 | }); |
||
| 258 | } |
||
| 259 | if (!notification.tag) { |
||
| 260 | Object.defineProperty(notification, "tag", { |
||
| 261 | value: String(options.tag || "") |
||
| 262 | }); |
||
| 263 | } |
||
| 264 | if (!notification.title) { |
||
| 265 | Object.defineProperty(notification, "title", { |
||
| 266 | value: String(title) |
||
| 267 | }); |
||
| 268 | } |
||
| 269 | if (!notification.timestamp) { |
||
| 270 | Object.defineProperty(notification, "timestamp", { |
||
| 271 | value: new Date().getTime() |
||
| 272 | }); |
||
| 273 | } |
||
| 274 | /* TODO: vibrate property */ |
||
| 275 | return notification; |
||
| 276 | } |
||
| 277 | Object.defineProperty(Notification, "permission", { |
||
| 278 | enumerable: true, |
||
| 279 | get: function() { |
||
| 280 | return _Notification.permission; |
||
| 281 | } |
||
| 282 | }); |
||
| 283 | /* |
||
| 284 | Notification.requestPermission should return a Promise(by spec). |
||
| 285 | Keep the original method and replace it with a custom one that |
||
| 286 | checks if the call of Notification.requestPermission returns a promise |
||
| 287 | and if not, then return a custom object that simulates the Promise object. |
||
| 288 | |||
| 289 | Specification: |
||
| 290 | Notification.requestPermission().then(callback); |
||
| 291 | |||
| 292 | Old Spec: |
||
| 293 | Notification.requestPermission(callback); |
||
| 294 | */ |
||
| 295 | Object.defineProperty(Notification, "requestPermission", { |
||
| 296 | enumerable: true, |
||
| 297 | value: function() { |
||
| 298 | return new Promise(function(resolve, reject) { |
||
| 299 | var promise = _Notification.requestPermission(function(permission) { |
||
| 300 | resolve(permission); |
||
| 301 | }); |
||
| 302 | if (!(promise instanceof Promise)) { |
||
| 303 | return; |
||
| 304 | } |
||
| 305 | resolve(promise); |
||
| 306 | }); |
||
| 307 | } |
||
| 308 | }); |
||
| 309 | window.Notification = Notification; |
||
| 310 | })(); |
||
| 311 | |||
| 396 |