-
-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathapp.js
1275 lines (1072 loc) · 44.5 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* OpenMage
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available at https://opensource.org/license/afl-3-0-php
*
* @category design
* @package rwd_default
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
* @copyright Copyright (c) 2019-2023 The OpenMage Contributors (https://www.openmage.org)
* @license https://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
// =============================================
// Primary Break Points
// =============================================
// These should be used with the bp (max-width, xx) mixin
// where a min-width is used, remember to +1 to break correctly
// If these are changed, they must also be updated in _var.scss
var bp = {
xsmall: 479,
small: 599,
medium: 770,
large: 979,
xlarge: 1199
};
// ==============================================
// Search
// ==============================================
/**
* Implements a custom validation style for the search form. When the form is invalidly submitted, the validation-failed
* class gets added to the input, but the "This is a required field." text does not display
*/
Varien.searchForm.prototype.initialize = function (form, field, emptyText) {
this.form = $(form);
this.field = $(field);
this.emptyText = emptyText;
Event.observe(this.form, 'submit', this.submit.bind(this));
Event.observe(this.field, 'change', this.change.bind(this));
Event.observe(this.field, 'focus', this.focus.bind(this));
Event.observe(this.field, 'blur', this.blur.bind(this));
this.blur();
};
Varien.searchForm.prototype.submit = function (event) {
if (this.field.value == this.emptyText || this.field.value == ''){
Event.stop(event);
this.field.addClassName('validation-failed');
this.field.focus();
return false;
}
return true;
};
Varien.searchForm.prototype.change = function (event) {
if (
this.field.value != this.emptyText
&& this.field.value != ''
&& this.field.hasClassName('validation-failed')
) {
this.field.removeClassName('validation-failed');
}
};
Varien.searchForm.prototype.blur = function (event) {
if (this.field.hasClassName('validation-failed')) {
this.field.removeClassName('validation-failed');
}
};
// ==============================================
// Pointer abstraction
// ==============================================
/**
* This class provides an easy and abstracted mechanism to determine the
* best pointer behavior to use -- that is, is the user currently interacting
* with their device in a touch manner, or using a mouse.
*
* Since devices may use either touch or mouse or both, there is no way to
* know the user's preferred pointer type until they interact with the site.
*
* To accommodate this, this class provides a method and two events
* to determine the user's preferred pointer type.
*
* - getPointer() returns the last used pointer type, or, if the user has
* not yet interacted with the site, falls back to a Modernizr test.
*
* - The mouse-detected event is triggered on the window object when the user
* is using a mouse pointer input, or has switched from touch to mouse input.
* It can be observed in this manner: $j(window).on('mouse-detected', function(event) { // custom code });
*
* - The touch-detected event is triggered on the window object when the user
* is using touch pointer input, or has switched from mouse to touch input.
* It can be observed in this manner: $j(window).on('touch-detected', function(event) { // custom code });
*/
var PointerManager = {
MOUSE_POINTER_TYPE: 'mouse',
TOUCH_POINTER_TYPE: 'touch',
POINTER_EVENT_TIMEOUT_MS: 500,
standardTouch: false,
touchDetectionEvent: null,
lastTouchType: null,
pointerTimeout: null,
pointerEventLock: false,
getPointerEventsSupported: function() {
return this.standardTouch;
},
getPointerEventsInputTypes: function() {
if (window.navigator.pointerEnabled) { //IE 11+
//return string values from http://msdn.microsoft.com/en-us/library/windows/apps/hh466130.aspx
return {
MOUSE: 'mouse',
TOUCH: 'touch',
PEN: 'pen'
};
} else if (window.navigator.msPointerEnabled) { //IE 10
//return numeric values from http://msdn.microsoft.com/en-us/library/windows/apps/hh466130.aspx
return {
MOUSE: 0x00000004,
TOUCH: 0x00000002,
PEN: 0x00000003
};
} else { //other browsers don't support pointer events
return {}; //return empty object
}
},
/**
* If called before init(), get best guess of input pointer type
* using Modernizr test.
* If called after init(), get current pointer in use.
*/
getPointer: function() {
// On iOS devices, always default to touch, as this.lastTouchType will intermittently return 'mouse' if
// multiple touches are triggered in rapid succession in Safari on iOS
if(Modernizr.ios) {
return this.TOUCH_POINTER_TYPE;
}
if(this.lastTouchType) {
return this.lastTouchType;
}
return Modernizr.touch ? this.TOUCH_POINTER_TYPE : this.MOUSE_POINTER_TYPE;
},
setPointerEventLock: function() {
this.pointerEventLock = true;
},
clearPointerEventLock: function() {
this.pointerEventLock = false;
},
setPointerEventLockTimeout: function() {
var that = this;
if(this.pointerTimeout) {
clearTimeout(this.pointerTimeout);
}
this.setPointerEventLock();
this.pointerTimeout = setTimeout(function() { that.clearPointerEventLock(); }, this.POINTER_EVENT_TIMEOUT_MS);
},
triggerMouseEvent: function(originalEvent) {
if(this.lastTouchType == this.MOUSE_POINTER_TYPE) {
return; //prevent duplicate events
}
this.lastTouchType = this.MOUSE_POINTER_TYPE;
$j(window).trigger('mouse-detected', originalEvent);
},
triggerTouchEvent: function(originalEvent) {
if(this.lastTouchType == this.TOUCH_POINTER_TYPE) {
return; //prevent duplicate events
}
this.lastTouchType = this.TOUCH_POINTER_TYPE;
$j(window).trigger('touch-detected', originalEvent);
},
initEnv: function() {
if (window.navigator.pointerEnabled) {
this.standardTouch = true;
this.touchDetectionEvent = 'pointermove';
} else if (window.navigator.msPointerEnabled) {
this.standardTouch = true;
this.touchDetectionEvent = 'MSPointerMove';
} else {
this.touchDetectionEvent = 'touchstart';
}
},
wirePointerDetection: function() {
var that = this;
if(this.standardTouch) { //standard-based touch events. Wire only one event.
//detect pointer event
$j(window).on(this.touchDetectionEvent, function(e) {
switch(e.originalEvent.pointerType) {
case that.getPointerEventsInputTypes().MOUSE:
that.triggerMouseEvent(e);
break;
case that.getPointerEventsInputTypes().TOUCH:
case that.getPointerEventsInputTypes().PEN:
// intentionally group pen and touch together
that.triggerTouchEvent(e);
break;
}
});
} else { //non-standard touch events. Wire touch and mouse competing events.
//detect first touch
$j(window).on(this.touchDetectionEvent, function(e) {
if(that.pointerEventLock) {
return;
}
that.setPointerEventLockTimeout();
that.triggerTouchEvent(e);
});
//detect mouse usage
$j(document).on('mouseover', function(e) {
if(that.pointerEventLock) {
return;
}
that.setPointerEventLockTimeout();
that.triggerMouseEvent(e);
});
}
},
init: function() {
this.initEnv();
this.wirePointerDetection();
}
};
/**
* This class manages the main navigation and supports infinite nested
* menus which support touch, mouse click, and hover correctly.
*
* The following is the expected behavior:
*
* - Hover with an actual mouse should expand the menu (at any level of nesting)
* - Click with an actual mouse will follow the link, regardless of any children
* - Touch will follow links without children, and toggle submenus of links with children
*
* Caveats:
* - According to Mozilla's documentation (https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Touch_events),
* Firefox has disabled Apple-style touch events on desktop, so desktop devices using Firefox will not support
* the desired touch behavior.
*/
var MenuManager = {
// These variables are used to detect incorrect touch / mouse event order
mouseEnterEventObserved: false,
touchEventOrderIncorrect: false,
cancelNextTouch: false,
/**
* This class manages touch scroll detection
*/
TouchScroll: {
/**
* Touch which moves the screen vertically more than
* this many pixels will be considered a scroll.
*/
TOUCH_SCROLL_THRESHOLD: 20,
touchStartPosition: null,
/**
* Note scroll position so that scroll action can be detected later.
* Should probably be called on touchstart (or similar) event.
*/
reset: function() {
this.touchStartPosition = $j(window).scrollTop();
},
/**
* Determines if touch was actually a scroll. Should probably be checked
* on touchend (or similar) event.
* @returns {boolean}
*/
shouldCancelTouch: function() {
if(this.touchStartPosition == null) {
return false;
}
var scroll = $j(window).scrollTop() - this.touchStartPosition;
return Math.abs(scroll) > this.TOUCH_SCROLL_THRESHOLD;
}
},
/**
* Determines if small screen behavior should be used.
*
* @returns {boolean}
*/
useSmallScreenBehavior: function() {
return Modernizr.mq("screen and (max-width:" + bp.medium + "px)");
},
/**
* Toggles a given menu item's visibility.
* On large screens, also closes sibling and children of sibling menus.
*
* @param target
*/
toggleMenuVisibility: function(target) {
var link = $j(target);
var li = link.closest('li');
if(!this.useSmallScreenBehavior()) {
// remove menu-active from siblings and children of siblings
li.siblings()
.removeClass('menu-active')
.find('li')
.removeClass('menu-active');
//remove menu-active from children
li.find('li.menu-active').removeClass('menu-active');
}
//toggle current item's active state
li.toggleClass('menu-active');
},
// --------------------------------------------
// Initialization methods
//
/**
* Initialize MenuManager and wire all required events.
* Should only be called once.
*
*/
init: function() {
this.wirePointerEvents();
},
/**
* This method observes an absurd number of events
* depending on the capabilities of the current browser
* to implement expected header navigation functionality.
*
* The goal is to separate interactions into four buckets:
* - pointer enter using an actual mouse
* - pointer leave using an actual mouse
* - pointer down using an actual mouse
* - pointer down using touch
*
* Browsers supporting PointerEvent events will use these
* to differentiate pointer types.
*
* Browsers supporting Apple-style will use those events
* along with mouseenter / mouseleave to emulate pointer events.
*/
wirePointerEvents: function() {
var that = this;
var pointerTarget = $j('#nav a.has-children');
var hoverTarget = $j('#nav li');
if(PointerManager.getPointerEventsSupported()) {
// pointer events supported, so observe those type of events
var enterEvent = window.navigator.pointerEnabled ? 'pointerenter' : 'mouseenter';
var leaveEvent = window.navigator.pointerEnabled ? 'pointerleave' : 'mouseleave';
var fullPointerSupport = window.navigator.pointerEnabled;
hoverTarget.on(enterEvent, function(e) {
if(e.originalEvent.pointerType === undefined // Browsers with partial PointerEvent support don't provide pointer type
|| e.originalEvent.pointerType == PointerManager.getPointerEventsInputTypes().MOUSE) {
if(fullPointerSupport) {
that.mouseEnterAction(e, this);
} else {
that.PartialPointerEventsSupport.mouseEnterAction(e, this);
}
}
}).on(leaveEvent, function(e) {
if(e.originalEvent.pointerType === undefined // Browsers with partial PointerEvent support don't provide pointer type
|| e.originalEvent.pointerType == PointerManager.getPointerEventsInputTypes().MOUSE) {
if(fullPointerSupport) {
that.mouseLeaveAction(e, this);
} else {
that.PartialPointerEventsSupport.mouseLeaveAction(e, this);
}
}
});
if(!fullPointerSupport) {
//click event doesn't have pointer type on it.
//observe MSPointerDown to set pointer type for click to find later
pointerTarget.on('MSPointerDown', function(e) {
$j(this).data('pointer-type', e.originalEvent.pointerType);
});
}
pointerTarget.on('click', function(e) {
var pointerType = fullPointerSupport ? e.originalEvent.pointerType : $j(this).data('pointer-type');
if(pointerType === undefined || pointerType == PointerManager.getPointerEventsInputTypes().MOUSE) {
that.mouseClickAction(e, this);
} else {
if(fullPointerSupport) {
that.touchAction(e, this);
} else {
that.PartialPointerEventsSupport.touchAction(e, this);
}
}
$j(this).removeData('pointer-type'); // clear pointer type hint from target, if any
});
} else {
//pointer events not supported, use Apple-style events to simulate
hoverTarget.on('mouseenter', function(e) {
// Touch events should cancel this event if a touch pointer is used.
// Record that this method has fired so that erroneous following
// touch events (if any) can respond accordingly.
that.mouseEnterEventObserved = true;
that.cancelNextTouch = true;
that.mouseEnterAction(e, this);
}).on('mouseleave', function(e) {
that.mouseLeaveAction(e, this);
});
$j(window).on('touchstart', function(e) {
if(that.mouseEnterEventObserved) {
// If mouse enter observed before touch, then device touch
// event order is incorrect.
that.touchEventOrderIncorrect = true;
that.mouseEnterEventObserved = false; // Reset test
}
// Reset TouchScroll in order to detect scroll later.
that.TouchScroll.reset();
});
pointerTarget.on('touchend', function(e) {
$j(this).data('was-touch', true); // Note that element was invoked by touch pointer
e.preventDefault(); // Prevent mouse compatibility events from firing where possible
if(that.TouchScroll.shouldCancelTouch()) {
return; // Touch was a scroll -- don't do anything else
}
if(that.touchEventOrderIncorrect) {
that.PartialTouchEventsSupport.touchAction(e, this);
} else {
that.touchAction(e, this);
}
}).on('click', function(e) {
if($j(this).data('was-touch')) { // Event invoked after touch
e.preventDefault(); // Prevent following link
return; // Prevent other behavior
}
that.mouseClickAction(e, this);
});
}
},
// --------------------------------------------
// Behavior "buckets"
//
/**
* Browsers with incomplete PointerEvent support (such as IE 10)
* require special event management. This collection of methods
* accommodate such browsers.
*/
PartialPointerEventsSupport: {
/**
* Without proper pointerenter / pointerleave / click pointerType support,
* we have to use mouseenter events. These end up triggering
* lots of mouseleave events that can be misleading.
*
* Each touch mouseenter and click event that ends up triggering
* an undesired mouseleave increments this lock variable.
*
* Mouseleave events are cancelled if this variable is > 0,
* and then the variable is decremented regardless.
*/
mouseleaveLock: 0,
/**
* Handles mouse enter behavior, but if using touch,
* toggle menus in the absence of full PointerEvent support.
*
* @param event
* @param target
*/
mouseEnterAction: function(event, target) {
if(MenuManager.useSmallScreenBehavior()) {
// fall back to normal method behavior
MenuManager.mouseEnterAction(event, target);
return;
}
event.stopPropagation();
var jtarget = $j(target);
if(!jtarget.hasClass('level0')) {
this.mouseleaveLock = jtarget.parents('li').length + 1;
}
MenuManager.toggleMenuVisibility(target);
},
/**
* Handles mouse leave behaivor, but obeys the mouseleaveLock
* to allow undesired mouseleave events to be cancelled.
*
* @param event
* @param target
*/
mouseLeaveAction: function(event, target) {
if(MenuManager.useSmallScreenBehavior()) {
// fall back to normal method behavior
MenuManager.mouseLeaveAction(event, target);
return;
}
if(this.mouseleaveLock > 0) {
this.mouseleaveLock--;
return; // suppress duplicate mouseleave event after touch
}
$j(target).removeClass('menu-active'); //hide all menus
},
/**
* Does no work on its own, but increments mouseleaveLock
* to prevent following undesireable mouseleave events.
*
* @param event
* @param target
*/
touchAction: function(event, target) {
if(MenuManager.useSmallScreenBehavior()) {
// fall back to normal method behavior
MenuManager.touchAction(event, target);
return;
}
event.preventDefault(); // prevent following link
this.mouseleaveLock++;
}
},
/**
* Browsers with incomplete Apple-style touch event support
* (such as the legacy Android browser) sometimes fire
* touch events out of order. In particular, mouseenter may
* fire before the touch events. This collection of methods
* accommodate such browsers.
*/
PartialTouchEventsSupport: {
/**
* Toggles visibility of menu, unless suppressed by previous
* out of order mouseenter event.
*
* @param event
* @param target
*/
touchAction: function(event, target) {
if(MenuManager.cancelNextTouch) {
// Mouseenter has already manipulated the menu.
// Suppress this undesired touch event.
MenuManager.cancelNextTouch = false;
return;
}
MenuManager.toggleMenuVisibility(target);
}
},
/**
* On large screens, show menu.
* On small screens, do nothing.
*
* @param event
* @param target
*/
mouseEnterAction: function(event, target) {
if(this.useSmallScreenBehavior()) {
return; // don't do mouse enter functionality on smaller screens
}
$j(target).addClass('menu-active'); //show current menu
},
/**
* On large screens, hide menu.
* On small screens, do nothing.
*
* @param event
* @param target
*/
mouseLeaveAction: function(event, target) {
if(this.useSmallScreenBehavior()) {
return; // don't do mouse leave functionality on smaller screens
}
$j(target).removeClass('menu-active'); //hide all menus
},
/**
* On large screens, don't interfere so that browser will follow link.
* On small screens, toggle menu visibility.
*
* @param event
* @param target
*/
mouseClickAction: function(event, target) {
if(this.useSmallScreenBehavior()) {
event.preventDefault(); //don't follow link
this.toggleMenuVisibility(target); //instead, toggle visibility
}
},
/**
* Toggle menu visibility, and prevent event default to avoid
* undesired, duplicate, synthetic mouse events.
*
* @param event
* @param target
*/
touchAction: function(event, target) {
this.toggleMenuVisibility(target);
event.preventDefault();
}
};
// ==============================================
// jQuery Init
// ==============================================
// Use $j(document).ready() because Magento executes Prototype inline
$j(document).ready(function () {
// ==============================================
// Shared Vars
// ==============================================
// Document
var w = $j(window);
var d = $j(document);
var body = $j('body');
Modernizr.addTest('ios', function () {
return navigator.userAgent.match(/(iPad|iPhone|iPod)/g);
});
//initialize pointer abstraction manager
PointerManager.init();
/* Wishlist Toggle Class */
$j(".change").click(function (e) {
$j( this ).toggleClass('active');
e.stopPropagation();
});
$j(document).click(function (e) {
if (! $j(e.target).hasClass('.change')) $j(".change").removeClass('active');
});
// =============================================
// Skip Links
// =============================================
var skipContents = $j('.skip-content');
var skipLinks = $j('.skip-link');
$j('.skip-links').on('click', '.skip-link', function (e) {
e.preventDefault();
var self = $j(this);
// Use the data-target-element attribute, if it exists. Fall back to href.
var target = self.attr('data-target-element') ? self.attr('data-target-element') : self.attr('href');
// Get target element
var elem = $j(target);
// Check if stub is open
var isSkipContentOpen = elem.hasClass('skip-active') ? 1 : 0;
// Hide all stubs
skipLinks.removeClass('skip-active');
skipContents.removeClass('skip-active');
// Toggle stubs
if (isSkipContentOpen) {
self.removeClass('skip-active');
elem.removeClass('skip-active');
} else {
self.addClass('skip-active');
elem.addClass('skip-active');
}
if (target == '#header-search') {
let searchInput = document.getElementById('search');
if (searchInput) {
searchInput.focus();
}
}
});
$j('.skip-links').on('click', '#header-cart .skip-link-close', function(e) {
var parent = $j(this).parents('.skip-content');
var link = parent.siblings('.skip-link');
parent.removeClass('skip-active');
link.removeClass('skip-active');
e.preventDefault();
});
// ==============================================
// Header Menus
// ==============================================
// initialize menu
MenuManager.init();
// Prevent sub menus from spilling out of the window.
function preventMenuSpill() {
var windowWidth = $j(window).width();
$j('ul.level0').each(function(){
var ul = $j(this);
//Show it long enough to get info, then hide it.
ul.addClass('position-test');
ul.removeClass('spill');
var width = ul.outerWidth();
var offset = ul.offset().left;
ul.removeClass('position-test');
//Add the spill class if it will spill off the page.
if ((offset + width) > windowWidth) {
ul.addClass('spill');
}
});
}
preventMenuSpill();
$j(window).on('delayed-resize', preventMenuSpill);
// ==============================================
// Language Switcher
// ==============================================
// In order to display the language switcher next to the logo, we are moving the content at different viewports,
// rather than having duplicate markup or changing the design
enquire.register('(max-width: ' + bp.medium + 'px)', {
match: function () {
$j('.page-header-container .store-language-container').prepend($j('.form-language'));
},
unmatch: function () {
$j('.header-language-container .store-language-container').prepend($j('.form-language'));
}
});
// ==============================================
// Enquire JS
// ==============================================
enquire.register('screen and (min-width: ' + (bp.medium + 1) + 'px)', {
match: function () {
$j('.menu-active').removeClass('menu-active');
$j('.sub-menu-active').removeClass('sub-menu-active');
$j('.skip-active').removeClass('skip-active');
},
unmatch: function () {
$j('.menu-active').removeClass('menu-active');
$j('.sub-menu-active').removeClass('sub-menu-active');
$j('.skip-active').removeClass('skip-active');
}
});
// ==============================================
// UI Pattern - Media Switcher
// ==============================================
// Used to swap primary product photo from thumbnails.
var mediaListLinks = $j('.media-list').find('a');
var mediaPrimaryImage = $j('.primary-image').find('img');
if (mediaListLinks.length) {
mediaListLinks.on('click', function (e) {
e.preventDefault();
var self = $j(this);
mediaPrimaryImage.attr('src', self.attr('href'));
});
}
// ==============================================
// UI Pattern - ToggleSingle
// ==============================================
// Use this plugin to toggle the visibility of content based on a toggle link/element.
// This pattern differs from the accordion functionality in the Toggle pattern in that each toggle group acts
// independently of the others. It is named so as not to be confused with the Toggle pattern below
//
// This plugin requires a specific markup structure. The plugin expects a set of elements that it
// will use as the toggle link. It then hides all immediately following siblings and toggles the sibling's
// visibility when the toggle link is clicked.
//
// Example markup:
// <div class="block">
// <div class="block-title">Trigger</div>
// <div class="block-content">Content that should show when </div>
// </div>
//
// JS: jQuery('.block-title').toggleSingle();
//
// Options:
// destruct: defaults to false, but if true, the plugin will remove itself, display content, and remove event handlers
$j.fn.toggleSingle = function (options) {
// passing destruct: true allows
var settings = $j.extend({
destruct: false
}, options);
return this.each(function () {
if (!settings.destruct) {
$j(this).on('click', function () {
$j(this)
.toggleClass('active')
.next()
.toggleClass('no-display');
});
// Hide the content
$j(this).next().addClass('no-display');
} else {
// Remove event handler so that the toggle link can no longer be used
$j(this).off('click');
// Remove all classes that were added by this plugin
$j(this)
.removeClass('active')
.next()
.removeClass('no-display');
}
});
};
// ==============================================
// UI Pattern - Toggle Content (tabs and accordions in one setup)
// ==============================================
$j('.toggle-content').each(function () {
var wrapper = jQuery(this);
var hasTabs = wrapper.hasClass('tabs');
var hasAccordion = wrapper.hasClass('accordion');
var startOpen = wrapper.hasClass('open');
var dl = wrapper.children('dl:first');
var dts = dl.children('dt');
var panes = dl.children('dd');
var groups = new Array(dts, panes);
//Create a ul for tabs if necessary.
if (hasTabs) {
var ul = jQuery('<ul class="toggle-tabs"></ul>');
dts.each(function () {
var dt = jQuery(this);
var li = jQuery('<li></li>');
li.html(dt.html());
ul.append(li);
});
ul.insertBefore(dl);
var lis = ul.children();
groups.push(lis);
}
//Add "last" classes.
var i;
for (i = 0; i < groups.length; i++) {
groups[i].filter(':last').addClass('last');
}
function toggleClasses(clickedItem, group) {
var index = group.index(clickedItem);
var i;
for (i = 0; i < groups.length; i++) {
groups[i].removeClass('current');
groups[i].eq(index).addClass('current');
}
}
//Toggle on tab (dt) click.
dts.on('click', function (e) {
//They clicked the current dt to close it. Restore the wrapper to unclicked state.
if (jQuery(this).hasClass('current') && wrapper.hasClass('accordion-open')) {
wrapper.removeClass('accordion-open');
} else {
//They're clicking something new. Reflect the explicit user interaction.
wrapper.addClass('accordion-open');
}
toggleClasses(jQuery(this), dts);
});
//Toggle on tab (li) click.
if (hasTabs) {
lis.on('click', function (e) {
toggleClasses(jQuery(this), lis);
});
//Open the first tab.
lis.eq(0).trigger('click');
}
//Open the first accordion if desired.
if (startOpen) {
dts.eq(0).trigger('click');
}
});
// ==============================================
// Layered Navigation Block
// ==============================================
// On product list pages, we want to show the layered nav/category menu immediately above the product list.
// While it would make more sense to just move the .block-layered-nav block rather than .col-left-first
// (since other blocks can be inserted into left_first), it creates simpler code to move the entire
// .col-left-first block, so that is the approach we're taking
if ($j('.col-left-first > .block').length && $j('div.category-products').length) {
enquire.register('screen and (max-width: ' + bp.medium + 'px)', {
match: function () {
$j('.col-left-first').insertBefore($j('div.category-products'));
},
unmatch: function () {
// Move layered nav back to left column
$j('.col-left-first').insertBefore($j('.col-main'));
}
});
}
// ==============================================
// 3 column layout
// ==============================================
// On viewports smaller than 1000px, move the right column into the left column
if ($j('.main-container.col3-layout').length > 0) {
enquire.register('screen and (max-width: 1000px)', {
match: function () {
var rightColumn = $j('.col-right');
var colWrapper = $j('.col-wrapper');
rightColumn.appendTo(colWrapper);
},
unmatch: function () {
var rightColumn = $j('.col-right');
var main = $j('.main');
rightColumn.appendTo(main);
}
});
}
// ==============================================
// Block collapsing (on smaller viewports)
// ==============================================
enquire.register('(max-width: ' + bp.medium + 'px)', {
setup: function () {
this.toggleElements = $j(
// This selects the menu on the My Account and CMS pages
'.col-left-first .block:not(.block-layered-nav) .block-title, ' +
'.col-left-first .block-layered-nav .block-subtitle--filter, ' +
'.sidebar:not(.col-left-first) .block .block-title'
);
},
match: function () {
this.toggleElements.toggleSingle();