FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ionic/js/utils/utils.js at master · Java1024/ionic · GitHub
Java1024
ionic
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ionic
/
js
/
utils
/
utils.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
215 lines (194 loc) · 6.58 KB
Breadcrumbs
ionic
/
js
/
utils
/
utils.js
Copy path
File metadata and controls
215 lines (194 loc) · 6.58 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
(
function
(
ionic
)
{
/* for nextUid function below */
var
nextId
=
0
;
/**
* Various utilities used throughout Ionic
*
* Some of these are adopted from underscore.js and backbone.js, both also MIT licensed.
*/
ionic
.
Utils
=
{
arrayMove
:
function
(
arr
,
oldIndex
,
newIndex
)
{
if
(
newIndex
>=
arr
.
length
)
{
var
k
=
newIndex
-
arr
.
length
;
while
(
(
k
--
)
+
1
)
{
arr
.
push
(
undefined
)
;
}
}
arr
.
splice
(
newIndex
,
0
,
arr
.
splice
(
oldIndex
,
1
)
[
0
]
)
;
return
arr
;
}
,
/**
* Return a function that will be called with the given context
*/
proxy
:
function
(
func
,
context
)
{
var
args
=
Array
.
prototype
.
slice
.
call
(
arguments
,
2
)
;
return
function
(
)
{
return
func
.
apply
(
context
,
args
.
concat
(
Array
.
prototype
.
slice
.
call
(
arguments
)
)
)
;
}
;
}
,
/**
* Only call a function once in the given interval.
*
*
@param
func {Function} the function to call
*
@param
wait {int} how long to wait before/after to allow function calls
*
@param
immediate {boolean} whether to call immediately or after the wait interval
*/
debounce
:
function
(
func
,
wait
,
immediate
)
{
var
timeout
,
args
,
context
,
timestamp
,
result
;
return
function
(
)
{
context
=
this
;
args
=
arguments
;
timestamp
=
new
Date
(
)
;
var
later
=
function
(
)
{
var
last
=
(
new
Date
(
)
)
-
timestamp
;
if
(
last
<
wait
)
{
timeout
=
setTimeout
(
later
,
wait
-
last
)
;
}
else
{
timeout
=
null
;
if
(
!
immediate
)
result
=
func
.
apply
(
context
,
args
)
;
}
}
;
var
callNow
=
immediate
&&
!
timeout
;
if
(
!
timeout
)
{
timeout
=
setTimeout
(
later
,
wait
)
;
}
if
(
callNow
)
result
=
func
.
apply
(
context
,
args
)
;
return
result
;
}
;
}
,
/**
* Throttle the given fun, only allowing it to be
* called at most every `wait` ms.
*/
throttle
:
function
(
func
,
wait
,
options
)
{
var
context
,
args
,
result
;
var
timeout
=
null
;
var
previous
=
0
;
options
||
(
options
=
{
}
)
;
var
later
=
function
(
)
{
previous
=
options
.
leading
===
false
?
0
:
Date
.
now
(
)
;
timeout
=
null
;
result
=
func
.
apply
(
context
,
args
)
;
}
;
return
function
(
)
{
var
now
=
Date
.
now
(
)
;
if
(
!
previous
&&
options
.
leading
===
false
)
previous
=
now
;
var
remaining
=
wait
-
(
now
-
previous
)
;
context
=
this
;
args
=
arguments
;
if
(
remaining
<=
0
)
{
clearTimeout
(
timeout
)
;
timeout
=
null
;
previous
=
now
;
result
=
func
.
apply
(
context
,
args
)
;
}
else
if
(
!
timeout
&&
options
.
trailing
!==
false
)
{
timeout
=
setTimeout
(
later
,
remaining
)
;
}
return
result
;
}
;
}
,
// Borrowed from Backbone.js's extend
// Helper function to correctly set up the prototype chain, for subclasses.
// Similar to `goog.inherits`, but uses a hash of prototype properties and
// class properties to be extended.
inherit
:
function
(
protoProps
,
staticProps
)
{
var
parent
=
this
;
var
child
;
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent's constructor.
if
(
protoProps
&&
protoProps
.
hasOwnProperty
(
'constructor'
)
)
{
child
=
protoProps
.
constructor
;
}
else
{
child
=
function
(
)
{
return
parent
.
apply
(
this
,
arguments
)
;
}
;
}
// Add static properties to the constructor function, if supplied.
ionic
.
extend
(
child
,
parent
,
staticProps
)
;
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
var
Surrogate
=
function
(
)
{
this
.
constructor
=
child
;
}
;
Surrogate
.
prototype
=
parent
.
prototype
;
child
.
prototype
=
new
Surrogate
(
)
;
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if
(
protoProps
)
ionic
.
extend
(
child
.
prototype
,
protoProps
)
;
// Set a convenience property in case the parent's prototype is needed
// later.
child
.
__super__
=
parent
.
prototype
;
return
child
;
}
,
// Extend adapted from Underscore.js
extend
:
function
(
obj
)
{
var
args
=
Array
.
prototype
.
slice
.
call
(
arguments
,
1
)
;
for
(
var
i
=
0
;
i
<
args
.
length
;
i
++
)
{
var
source
=
args
[
i
]
;
if
(
source
)
{
for
(
var
prop
in
source
)
{
obj
[
prop
]
=
source
[
prop
]
;
}
}
}
return
obj
;
}
,
nextUid
:
function
(
)
{
return
'ion'
+
(
nextId
++
)
;
}
,
disconnectScope
:
function
disconnectScope
(
scope
)
{
if
(
!
scope
)
return
;
if
(
scope
.
$root
===
scope
)
{
return
;
// we can't disconnect the root node;
}
var
parent
=
scope
.
$parent
;
scope
.
$$disconnected
=
true
;
scope
.
$broadcast
(
'$ionic.disconnectScope'
,
scope
)
;
// See Scope.$destroy
if
(
parent
.
$$childHead
===
scope
)
{
parent
.
$$childHead
=
scope
.
$$nextSibling
;
}
if
(
parent
.
$$childTail
===
scope
)
{
parent
.
$$childTail
=
scope
.
$$prevSibling
;
}
if
(
scope
.
$$prevSibling
)
{
scope
.
$$prevSibling
.
$$nextSibling
=
scope
.
$$nextSibling
;
}
if
(
scope
.
$$nextSibling
)
{
scope
.
$$nextSibling
.
$$prevSibling
=
scope
.
$$prevSibling
;
}
scope
.
$$nextSibling
=
scope
.
$$prevSibling
=
null
;
}
,
reconnectScope
:
function
reconnectScope
(
scope
)
{
if
(
!
scope
)
return
;
if
(
scope
.
$root
===
scope
)
{
return
;
// we can't disconnect the root node;
}
if
(
!
scope
.
$$disconnected
)
{
return
;
}
var
parent
=
scope
.
$parent
;
scope
.
$$disconnected
=
false
;
scope
.
$broadcast
(
'$ionic.reconnectScope'
,
scope
)
;
// See Scope.$new for this logic...
scope
.
$$prevSibling
=
parent
.
$$childTail
;
if
(
parent
.
$$childHead
)
{
parent
.
$$childTail
.
$$nextSibling
=
scope
;
parent
.
$$childTail
=
scope
;
}
else
{
parent
.
$$childHead
=
parent
.
$$childTail
=
scope
;
}
}
,
isScopeDisconnected
:
function
(
scope
)
{
var
climbScope
=
scope
;
while
(
climbScope
)
{
if
(
climbScope
.
$$disconnected
)
return
true
;
climbScope
=
climbScope
.
$parent
;
}
return
false
;
}
}
;
// Bind a few of the most useful functions to the ionic scope
ionic
.
inherit
=
ionic
.
Utils
.
inherit
;
ionic
.
extend
=
ionic
.
Utils
.
extend
;
ionic
.
throttle
=
ionic
.
Utils
.
throttle
;
ionic
.
proxy
=
ionic
.
Utils
.
proxy
;
ionic
.
debounce
=
ionic
.
Utils
.
debounce
;
}
)
(
window
.
ionic
)
;
Back
|
FazBrowse Home
|
New Git URL