Conditions | 1 |
Paths | 1 |
Total Lines | 96 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 | (function ($) { |
||
2 | "use strict"; |
||
3 | |||
4 | $.fn.gdprCookieLawPopup = (function () { |
||
5 | var _self = this; |
||
6 | |||
7 | _self.params = { |
||
8 | agreementExpiresInDays: 30, |
||
9 | autoAcceptCookiePolicy: false |
||
10 | }; |
||
11 | |||
12 | _self.vars = { |
||
13 | INITIALISED: false, |
||
14 | COOKIE_NAME: 'GDPR_COOKIE_LAW_CONSENT' |
||
15 | }; |
||
16 | |||
17 | // Overwrite default parameters if any of those is present |
||
18 | var parseParameters = function (settings) { |
||
19 | if (settings) { |
||
20 | if (typeof settings.agreementExpiresInDays !== 'undefined') { |
||
21 | _self.params.agreementExpiresInDays = settings.agreementExpiresInDays; |
||
22 | } |
||
23 | if (typeof settings.autoAcceptCookiePolicy !== 'undefined') { |
||
24 | _self.params.autoAcceptCookiePolicy = settings.autoAcceptCookiePolicy; |
||
25 | } |
||
26 | } |
||
27 | }; |
||
28 | |||
29 | // Storing the consent in a cookie |
||
30 | var setUserAcceptsCookies = function (consent) { |
||
31 | var d = new Date(); |
||
32 | var expiresInDays = _self.params.agreementExpiresInDays * 24 * 60 * 60 * 1000; |
||
33 | d.setTime(d.getTime() + expiresInDays); |
||
34 | var expires = "expires=" + d.toGMTString(); |
||
35 | document.cookie = _self.vars.COOKIE_NAME + '=' + consent + "; " + expires + ";path=/"; |
||
36 | }; |
||
37 | |||
38 | // Let's see if we have a consent cookie already |
||
39 | var userAlreadyAcceptedCookies = function () { |
||
40 | var userAcceptedCookies = false; |
||
41 | var cookies = document.cookie.split(";"); |
||
42 | for (var i = 0; i < cookies.length; i++) { |
||
43 | var c = cookies[i].trim(); |
||
44 | if (c.indexOf(_self.vars.COOKIE_NAME) == 0) { |
||
|
|||
45 | userAcceptedCookies = c.substring(_self.vars.COOKIE_NAME.length + 1, c.length); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | return userAcceptedCookies; |
||
50 | }; |
||
51 | |||
52 | var hideContainer = function () { |
||
53 | $('.gdprpopup-container').animate({ |
||
54 | opacity: 0, |
||
55 | height: 0 |
||
56 | }, 200, function () { |
||
57 | $('.gdprpopup-container').hide(0); |
||
58 | }); |
||
59 | }; |
||
60 | |||
61 | return { |
||
62 | init: function (settings) { |
||
63 | parseParameters(settings); |
||
64 | |||
65 | // No need to display this if user already accepted the policy |
||
66 | if (userAlreadyAcceptedCookies()) { |
||
67 | return; |
||
68 | } |
||
69 | |||
70 | // We should initialise only once |
||
71 | if (_self.vars.INITIALISED) { |
||
72 | return; |
||
73 | } |
||
74 | _self.vars.INITIALISED = true; |
||
75 | |||
76 | $('.gdprpopup-button-confirm').click(function () { |
||
77 | setUserAcceptsCookies(true); |
||
78 | hideContainer(); |
||
79 | return false; |
||
80 | }); |
||
81 | $('.gdprpopup-closebutton').click(function () { |
||
82 | hideContainer(); |
||
83 | return false; |
||
84 | }); |
||
85 | |||
86 | // Ready to start! |
||
87 | $('.gdprpopup-container').show(); |
||
88 | |||
89 | // In case it's alright to just display the message once |
||
90 | if (_self.params.autoAcceptCookiePolicy) { |
||
91 | setUserAcceptsCookies(true); |
||
92 | } |
||
93 | } |
||
94 | }; |
||
95 | }); |
||
96 | }(jQuery)); |
||
97 | |||
102 |