forked from freeCodeCamp/devdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.coffee
More file actions
161 lines (124 loc) · 3.38 KB
/
Copy pathview.coffee
File metadata and controls
161 lines (124 loc) · 3.38 KB
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
class app.View
$.extend @prototype, Events
constructor: ->
@setupElement()
@originalClassName = @el.className if @el.className
@resetClass() if @constructor.className
@refreshElements()
@init?()
@refreshElements()
setupElement: ->
@el ?= if typeof @constructor.el is 'string'
$ @constructor.el
else if @constructor.el
@constructor.el
else
document.createElement @constructor.tagName or 'div'
return
refreshElements: ->
if @constructor.elements
@[name] = @find selector for name, selector of @constructor.elements
return
addClass: (name) ->
@el.classList.add(name)
return
removeClass: (name) ->
@el.classList.remove(name)
return
resetClass: ->
@el.className = @originalClassName or ''
if @constructor.className
@addClass name for name in @constructor.className.split ' '
return
find: (selector) ->
$ selector, @el
findAll: (selector) ->
$$ selector, @el
findByClass: (name) ->
@findAllByClass(name)[0]
findLastByClass: (name) ->
all = @findAllByClass(name)[0]
all[all.length - 1]
findAllByClass: (name) ->
@el.getElementsByClassName(name)
findByTag: (tag) ->
@findAllByTag(tag)[0]
findLastByTag: (tag) ->
all = @findAllByTag(tag)
all[all.length - 1]
findAllByTag: (tag) ->
@el.getElementsByTagName(tag)
append: (value) ->
$.append @el, value.el or value
return
appendTo: (value) ->
$.append value.el or value, @el
return
prepend: (value) ->
$.prepend @el, value.el or value
return
prependTo: (value) ->
$.prepend value.el or value, @el
return
before: (value) ->
$.before @el, value.el or value
return
after: (value) ->
$.after @el, value.el or value
return
remove: (value) ->
$.remove value.el or value
return
empty: ->
$.empty @el
@refreshElements()
return
html: (value) ->
@empty()
@append value
return
tmpl: (args...) ->
app.templates.render(args...)
delay: (fn, args...) ->
delay = if typeof args[args.length - 1] is 'number' then args.pop() else 0
setTimeout fn.bind(@, args...), delay
onDOM: (event, callback) ->
$.on @el, event, callback
return
offDOM: (event, callback) ->
$.off @el, event, callback
return
bindEvents: ->
if @constructor.events
@onDOM name, @[method] for name, method of @constructor.events
if @constructor.routes
app.router.on name, @[method] for name, method of @constructor.routes
if @constructor.shortcuts
app.shortcuts.on name, @[method] for name, method of @constructor.shortcuts
return
unbindEvents: ->
if @constructor.events
@offDOM name, @[method] for name, method of @constructor.events
if @constructor.routes
app.router.off name, @[method] for name, method of @constructor.routes
if @constructor.shortcuts
app.shortcuts.off name, @[method] for name, method of @constructor.shortcuts
return
addSubview: (view) ->
(@subviews or= []).push(view)
activate: ->
return if @activated
@bindEvents()
view.activate() for view in @subviews if @subviews
@activated = true
true
deactivate: ->
return unless @activated
@unbindEvents()
view.deactivate() for view in @subviews if @subviews
@activated = false
true
detach: ->
@deactivate()
$.remove @el
return