Passed
Pull Request — master (#489)
by Andrew
05:39
created

app/Resources/markup/src/js/partials/app.js   C

Complexity

Total Complexity 55
Complexity/F 2.12

Size

Lines of Code 328
Function Count 26

Duplication

Duplicated Lines 328
Ratio 100 %

Importance

Changes 0
Metric Value
wmc 55
eloc 190
dl 328
loc 328
rs 6
c 0
b 0
f 0
cc 0
nc 64
mnd 3
bc 59
fnc 26
bpm 2.2692
cpm 2.1153
noi 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like app/Resources/markup/src/js/partials/app.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
$(document).ready(function () {
2
    /**
3
     * Show password
4
     */
5
    $('.icon-password--js').on('click', function () {
6
        var input = $(this).siblings('input'),
7
            type = input.attr('type');
8
9
        $(this).toggleClass("input-password__icon--active");
10
11
        if (type === "password") {
12
            input.attr("type", "text");
13
        } else {
14
            input.attr("type", "password");
15
        }
16
    });
17
18
    /**
19
     * Mask phone input
20
     */
21
    $('.mask-phone-input--js').mask("+38 099 999 99 99",
22
        {
23
            placeholder: " ",
24
            autoclear: false
25
        });
26
27
    /**
28
     * Payment popup
29
     */
30
    $('#add-user-trigger').on('click', function (e) {
31
        e.preventDefault();
32
        $(this).hide();
33
        $('#payment-add-user').show();
34
    });
35
36
    $('#cancel-add-user').on('click', function (e) {
37
        e.preventDefault();
38
        $('#payment-add-user').hide();
39
        $('#add-user-trigger').show();
40
    });
41
42
    $('#promo-code-trigger').on('click', function (e) {
43
        e.preventDefault();
44
        $(this).hide();
45
        $('#add-promo-code').show();
46
    });
47
48
    $('#cancel-promo-code').on('click', function (e) {
49
        e.preventDefault();
50
        $('#add-promo-code').hide();
51
        $('#promo-code-trigger').show();
52
    });
53
54
    /**
55
     * Program navigation for mobile devices
56
     */
57
    $(window).bind('resize load', function () {
58
        if ($(window).width() < 768) {
59
            $('.program-header__td').on('click', function () {
60
                var currentIndex = $(this).index() + 2,
61
                    eventRow = $('.program-body__tr--event');
62
63
                $(this).addClass('program-header__td--active')
64
                    .siblings()
65
                    .removeClass('program-header__td--active');
66
                eventRow.find('.program-body__td').not('.program-body__td:nth-child(1)').hide();
67
                eventRow.find('.program-body__td:nth-child(' + currentIndex + ')').show();
68
            });
69
        }
70
    });
71
72
    /**
73
     * Dropdown for referral link
74
     */
75
    $('#ref-dropdown').on('change', function () {
76
        var value = $('option:selected', this).text(),
77
            ref = $('option:selected', this).data('ref');
78
79
        $('#ref-selected').text(value);
80
        $('#ref-input').val(ref);
81
    });
82
83
    /**
84
     * Referral link copy yo clipboard
85
     */
86
    var isiOs = navigator.userAgent.match(/ipad|ipod|iphone/i);
87
    
88
    $('#ref-copy').on('click', function () {
89
        var input = $('#ref-input');
90
91
        if (isiOs) {
92
            var el = input.get(0),
93
                editable = el.contentEditable,
94
                readOnly = el.readOnly;
95
            el.contentEditable = true;
96
            el.readOnly = false;
97
98
            var range = document.createRange();
99
            range.selectNodeContents(el);
100
101
            var sel = window.getSelection();
102
            sel.removeAllRanges();
103
            sel.addRange(range);
104
            el.setSelectionRange(0, 999999);
105
            el.contentEditable = editable;
106
            el.readOnly = readOnly;
107
        } else {
108
            input.select();
109
        }
110
111
        var clipBoard = document.execCommand('copy');
112
        if (clipBoard) {
113
            $(this).addClass('tooltip-copy--active');
114
        }
115
116
        var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
117
        if (isMobile) {
118
            input.blur();
119
120
            if ($(this).hasClass('tooltip-copy--active')) {
121
                setTimeout(function () {
122
                    $('.tooltip-copy').removeClass('tooltip-copy--active');
123
                }, 2000);
124
            }
125
        }
126
    });
127
128
    $('.tooltip-copy').on('mouseout', function () {
129
        if ($(this).hasClass('tooltip-copy--active')) {
130
            setTimeout(function () {
131
                $('.tooltip-copy').removeClass('tooltip-copy--active');
132
            }, 1000);
133
        }
134
    });
135
136
    /**
137
     * Show the terms of the program for mobile devices
138
     */
139
    $('.share-banner__hint-show').on('click', function () {
140
        $(this).hide();
141
        $('.share-banner__hint').show();
142
    });
143
144
145
    /**
146
     * Fixed event header on scroll for event-page
147
     */
148
    if ($('.event-header').length) {
149
        var eventHeader = $('.event-header'),
150
            eventHeaderFixed = $('.fix-event-header');
151
152
        $(document).bind('scroll load', function () {
153
            var eventHeaderHeight = eventHeader.outerHeight(),
154
                offsetTopEvent = eventHeader.offset().top;
155
156
            if ($(this).scrollTop() > eventHeaderHeight + offsetTopEvent) {
157
                eventHeaderFixed.addClass('fix-event-header--show');
158
            } else {
159
                eventHeaderFixed.removeClass('fix-event-header--show');
160
            }
161
        });
162
    }
163
164
    /**
165
     * Fixed event static header on scroll for review-page, venue-page
166
     */
167
    var eventHeaderFixedStat = $('.fix-event-header--static'),
168
        sectionAfterEventHeader = $('.section-after-event-header');
169
170
    $(document).bind('scroll load', function () {
171
        var headerHeight = $('.header').outerHeight();
172
173
        if ($(this).scrollTop() >= headerHeight) {
174
            sectionAfterEventHeader.addClass('section-after-event-header--mr-t');
175
            eventHeaderFixedStat.addClass('fix-event-header--fixed');
176
        } else {
177
            sectionAfterEventHeader.removeClass('section-after-event-header--mr-t');
178
            eventHeaderFixedStat.removeClass('fix-event-header--fixed');
179
        }
180
    });
181
182
    /**
183
     * Fixed program header on scroll
184
     */
185
    if ($('.program').length) {
186
        var program = $('.program'),
187
            programBody = $('.program-body'),
188
            programHeader = $('.program-header'),
189
            offsetTopNegative;
190
191
        if ($(window).width() >= 768) {
192
            offsetTopNegative = 28;
193
        } else {
194
            offsetTopNegative = 0;
195
        }
196
197
        $(document).bind('scroll load', function () {
198
            var programHeight = program.outerHeight(),
199
                programHeaderHeight = programHeader.outerHeight(),
200
                headerFixedHeight = $('.fix-event-header').outerHeight(),
201
                offsetTopProgram = program.offset().top - headerFixedHeight;
202
203
            if ($(this).scrollTop() > offsetTopProgram + offsetTopNegative && $(this).scrollTop() < offsetTopProgram + offsetTopNegative + programHeight - programHeaderHeight) {
204
                programHeader.addClass('program-header--fixed');
205
                programBody.addClass('program-body--header-fixed');
206
                programHeader.removeClass('program-header--absolute');
207
            } else if ($(this).scrollTop() > offsetTopProgram + programHeight - programHeaderHeight) {
208
                programHeader.addClass('program-header--absolute');
209
                programHeader.removeClass('program-header--fixed');
210
            } else {
211
                programHeader.removeClass('program-header--fixed');
212
                programHeader.removeClass('program-header--absolute');
213
                programBody.removeClass('program-body--header-fixed');
214
            }
215
        });
216
    }
217
218
    /**
219
     * Show event menu for mobile devices
220
     */
221
    var eventMenuTrigger = $('#event-menu-trigger'),
222
        eventMenu = $('.event-menu');
223
224
    eventMenuTrigger.on('click', function () {
225
        $(this).toggleClass('open');
226
        $('body').toggleClass('overlay');
227
        eventMenu.toggleClass('event-menu--open');
228
    });
229
230
    if (eventMenuTrigger.length) {
231
        $(document).bind('click touchstart', function (e) {
232
            var el = $(e.target);
233
234
            if (el.closest('#event-menu-trigger').length || el.closest('.event-menu').length) {
235
                return true;
236
            } else {
237
                closeMenu();
238
            }
239
        });
240
    }
241
242
    /**
243
     * Go to block
244
     */
245
    var goToBlock = $(".go-to-block");
246
247
    goToBlock.click(function (e) {
248
        e.preventDefault();
249
        animateScroll($(this).attr('href'));
250
251
        if (eventMenu.length) {
252
            closeMenu();
253
        }
254
    });
255
256
    /**
257
     * Anchor from another page
258
     */
259
    $(window).bind("load", function () {
260
        if (window.location.hash) {
261
            setTimeout(function () {
262
                animateScroll(window.location.hash);
263
            }, 0);
264
        }
265
    });
266
267
    function animateScroll(target) {
268
        var fixHeader = $('.fix-event-header'),
269
            sumOffset;
270
271
        if (fixHeader.length) {
272
            sumOffset = $('.fix-event-header').outerHeight() + 24;
273
        } else {
274
            sumOffset = 0;
275
        }
276
277
        $('html, body').animate({
278
            scrollTop: ($(target).offset().top - sumOffset)
279
        }, 500);
280
    }
281
282
    function closeMenu() {
283
        $('body').removeClass('overlay');
284
        eventMenuTrigger.removeClass('open');
285
        eventMenu.removeClass('event-menu--open');
286
    }
287
288
    /**
289
     *  Button for scroll top page
290
     */
291
    if ($('.btn-up').length && $(window).width() > 1024) {
292
        $(window).scroll(function () {
293
            var headerHeight = $('.header').outerHeight(),
294
                eventHeaderHeight = $('.event-header').outerHeight(),
295
                windowHeight = $(window).outerHeight(),
296
                footerOffsetTop = $('.footer').offset().top,
297
                scrollHeight = $(this).scrollTop(),
298
                mapOffsetTop = $('.location__map').length ? $('.location__map').offset().top : 0,
299
                mapMrTop = parseInt($('.location__map').length ? $('.location__map').css('margin-top') : 0),
300
                footerMrTop = parseInt($('.footer').css('margin-top')),
301
                sumHeight = scrollHeight + windowHeight;
302
303
            if (scrollHeight > headerHeight + eventHeaderHeight) {
304
                $('.btn-up').addClass('btn-up--visible');
305
            } else {
306
                $('.btn-up').removeClass('btn-up--visible');
307
            }
308
309
            if ($('.location__map').length && sumHeight - 87 > mapOffsetTop - mapMrTop) {
310
                $('.btn-up').css('bottom', sumHeight - (mapOffsetTop - mapMrTop));
311
            }
312
            else if (sumHeight - 87 > footerOffsetTop - footerMrTop) {
313
                $('.btn-up').css('bottom', sumHeight - (footerOffsetTop - footerMrTop));
314
            }
315
            else {
316
                $('.btn-up').css('bottom', 87);
317
            }
318
319
        });
320
321
        $('.btn-up').on('click', function () {
322
            $('body,html').animate({
323
                scrollTop: 0
324
            }, 600);
325
            return false;
326
        });
327
    }
328
});