-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSlidingMenu.coffee
140 lines (114 loc) · 3.49 KB
/
SlidingMenu.coffee
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
###
SlidingMenu Constructor 1.0
Author: M.Ulyanov
Created: 05/12/2015
###
class window.SlidingMenu
@itemActive;
@activeState;
@sliding;
@defaultData;
@options;
@callbacks;
# #
constructor: (options, callbacks) ->
@options =
'menu' : options.menu,
'items': options.items,
'itemActiveClass': options.itemActiveClass ?= 'active',
'slidingClass': options.slidingClass ?= '',
'duration': options.duration ?= 400,
'direction': options.direction ?= 'x'
@callbacks =
'over' : null,
'leave': null
if callbacks? then @callbacks = callbacks;
do @createSliding;
do @setActiveState;
do @setDOMEvents;
# #
createSliding: () =>
@sliding = $('<div class="menu-sliding ' + @options.slidingClass + '"></div>');
@sliding.appendTo(@options.menu);
# #
setActiveState: () =>
@options.items.each (index, value) =>
if($(value).hasClass(@options.itemActiveClass)) then @itemActive = $(value);
if @itemActive?
@activeState = true;
do @setDefaultData;
else
@activeState = false;
# #
setDefaultData: () =>
@defaultData =
'left' : @itemActive.offset().left - @options.menu.offset().left,
'top' : @itemActive.offset().top - @options.menu.offset().top
'width' : @itemActive.outerWidth()
'height': @itemActive.outerHeight();
do @setSlidingData;
# #
setSlidingData: () =>
@sliding.css
'width' : @defaultData.width,
if @options.direction is 'x'
@sliding.css
'left': @defaultData.left
else if @options.direction is 'y'
@sliding.css
'height' : @defaultData.height,
'top' : @defaultData.top
else
errorReport("#{@options.direction} not support! Used x or y!");
# #
setDOMEvents: () ->
do @setMouseOver;
do @setMouseLeave;
# #
setMouseOver: () =>
@options.items.on 'mouseover', (event) =>
if event.currentTarget isnt event.target then return;
$self = $(event.target);
if not @itemActive
@itemActive = $self;
do @setDefaultData;
if @callbacks.over?
@callbacks.over($self, {@itemActive, @activeState, @sliding, @options});
@sliding.css('opacity', 1);
if @options.direction is 'x'
animateOptions =
'left' : $self.offset().left - @options.menu.offset().left,
'width' : $self.outerWidth()
else if @options.direction is 'y'
animateOptions =
'top' : $self.offset().top - @options.menu.offset().top
else
errorReport("#{@options.direction} not support! Used x or y!");
@sliding.stop().animate(animateOptions, duration: @options.duration);
# #
setMouseLeave: () ->
@options.menu.on 'mouseleave', (event) =>
$self = $(event.target);
if not @activeState
@itemActive = null;
@sliding.css({
'opacity' : 0,
'left' : 0,
'with' : 0
})
return;
if @callbacks.leave?
@callbacks.leave($self, {@itemActive, @activeState, @sliding, @options});
if @options.direction is 'x'
animateOptions =
'left' : @defaultData.left,
'width' : @defaultData.width
else if @options.direction is 'y'
animateOptions =
'top' : @defaultData.top,
else
errorReport("#{@options.direction} not support! Used x or y!");
@sliding.stop().animate(animateOptions, duration: @options.duration);
# #
errorReport = (message) ->
console.error(message);