| Conditions | 2 |
| Paths | > 20000 |
| Total Lines | 86 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | 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 | /** |
||
| 25 | egw.extend('notification', egw.MODULE_WND_LOCAL, function(_app, _wnd) |
||
| 26 | { |
||
| 27 | "use strict"; |
||
| 28 | |||
| 29 | // Notification permission, the default value is 'default' which is equivalent to 'denied' |
||
| 30 | var permission = 'default'; |
||
| 31 | |||
| 32 | if (typeof Notification != 'undefined') |
||
| 33 | { |
||
| 34 | permission = Notification.permission; |
||
| 35 | } |
||
| 36 | |||
| 37 | return { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * |
||
| 41 | * @param {string} _title a string to be shown as notification message |
||
| 42 | * @param {object} _options an object of Notification possible options: |
||
| 43 | * options = { |
||
| 44 | * dir: // direction of notification to be shown rtl, ltr or auto |
||
| 45 | * lang: // a valid BCP 47 language tag |
||
| 46 | * body: // DOM body |
||
| 47 | * icon: // parse icon URL, default icon is app icon |
||
| 48 | * tag: // a string value used for tagging an instance of notification, default is app name |
||
| 49 | * onclick: // Callback function dispatches on click on notification message |
||
| 50 | * onshow: // Callback function dispatches when notification is shown |
||
| 51 | * onclose: // Callback function dispateches on notification close |
||
| 52 | * onerror: // Callback function dispatches on error, default is a egw.debug log |
||
| 53 | * } |
||
| 54 | * @return {boolean} false if Notification is not supported by browser |
||
| 55 | */ |
||
| 56 | notification: function (_title, _options) |
||
| 57 | { |
||
| 58 | // Check if the notification is supported by browser |
||
| 59 | if (typeof Notification == 'undefined') return false; |
||
| 60 | |||
| 61 | var self = this; |
||
| 62 | // Check and ask for permission |
||
| 63 | if (Notification && Notification.requestPermission && permission === 'default') Notification.requestPermission (function(_permission){ |
||
| 64 | permission = _permission; |
||
| 65 | if (permission === 'granted') self.notification(_title,_options); |
||
| 66 | }); |
||
| 67 | |||
| 68 | // All options including callbacks |
||
| 69 | var options = _options || {}; |
||
| 70 | |||
| 71 | // Options used for creating Notification instane |
||
| 72 | var inst_options = { |
||
| 73 | tag: options.tag || egw.app_name(), |
||
| 74 | dir: options.dir || 'ltr', |
||
| 75 | lang: options.lang || egw.preference('lang', 'common'), |
||
| 76 | body: options.body || '', |
||
| 77 | icon: options.icon || egw.image('navbar', egw.app_name()) |
||
| 78 | }; |
||
| 79 | |||
| 80 | // Create an instance of Notification object |
||
| 81 | var notification = new Notification(_title, inst_options); |
||
|
|
|||
| 82 | |||
| 83 | // Callback function dispatches on click on notification message |
||
| 84 | notification.onclick = options.onclick || ''; |
||
| 85 | // Callback function dispatches when notification is shown |
||
| 86 | notification.onshow = options.onshow || ''; |
||
| 87 | // Callback function dispateches on notification close |
||
| 88 | notification.onclose = options.onclose || ''; |
||
| 89 | // Callback function dispatches on error |
||
| 90 | notification.onerror = options.onerror || function (e) {egw.debug('Notification failed because of ' + e);}; |
||
| 91 | |||
| 92 | }, |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Check Notification availability by browser |
||
| 96 | * |
||
| 97 | * @returns {Boolean} true if notification is supported and permitted otherwise false |
||
| 98 | */ |
||
| 99 | checkNotification: function () { |
||
| 100 | // Ask for permission if there's nothing decided yet |
||
| 101 | if (Notification && Notification.requestPermission && permission == 'default') { |
||
| 102 | Notification.requestPermission (function(_permission){ |
||
| 103 | permission = _permission; |
||
| 104 | }); |
||
| 105 | } |
||
| 106 | return (Notification && Notification.requestPermission && permission == 'granted'); |
||
| 107 | } |
||
| 108 | |||
| 109 | }; |
||
| 110 | }); |