forked from CodeSeven/KoLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknockout.activity.js
381 lines (328 loc) · 13.6 KB
/
knockout.activity.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
/*!
* NETEYE Activity Indicator jQuery Plugin
*
* Copyright (c) 2010 NETEYE GmbH
* Licensed under the MIT license
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
*
* Author: Felix Gnass [fgnass at neteye dot de]
* Version: @{VERSION}
*/
/**
* Plugin that renders a customisable activity indicator (spinner) using SVG or VML.
*/
(function ($) {
$.fn.activity = function (opts) {
this.each(function () {
var $this = $(this);
var el = $this.data('activity');
if (el) {
clearInterval(el.data('interval'));
el.remove();
$this.removeData('activity');
}
if (opts !== false) {
opts = $.extend({ color: $this.css('color') }, $.fn.activity.defaults, opts);
el = render($this, opts).css('position', 'absolute').prependTo(opts.outside ? 'body' : $this);
var h = $this.outerHeight() - el.height();
var w = $this.outerWidth() - el.width();
var margin = {
top: opts.valign == 'top' ? opts.padding : opts.valign == 'bottom' ? h - opts.padding : Math.floor(h / 2),
left: opts.align == 'left' ? opts.padding : opts.align == 'right' ? w - opts.padding : Math.floor(w / 2)
};
var offset = $this.offset();
if (opts.outside) {
el.css({ top: offset.top + 'px', left: offset.left + 'px' });
}
else {
margin.top -= el.offset().top - offset.top;
margin.left -= el.offset().left - offset.left;
}
el.css({ marginTop: margin.top + 'px', marginLeft: margin.left + 'px' });
animate(el, opts.segments, Math.round(10 / opts.speed) / 10);
$this.data('activity', el);
}
});
return this;
};
$.fn.activity.defaults = {
segments: 12,
space: 3,
length: 7,
width: 4,
speed: 1.2,
align: 'center',
valign: 'center',
padding: 4
};
$.fn.activity.getOpacity = function (opts, i) {
var steps = opts.steps || opts.segments - 1;
var end = opts.opacity !== undefined ? opts.opacity : 1 / steps;
return 1 - Math.min(i, steps) * (1 - end) / steps;
};
/**
* Default rendering strategy. If neither SVG nor VML is available, a div with class-name 'busy'
* is inserted, that can be styled with CSS to display an animated gif as fallback.
*/
var render = function () {
return $('<div>').addClass('busy');
};
/**
* The default animation strategy does nothing as we expect an animated gif as fallback.
*/
var animate = function () {
};
/**
* Utility function to create elements in the SVG namespace.
*/
function svg(tag, attr) {
var el = document.createElementNS("http://www.w3.org/2000/svg", tag || 'svg');
if (attr) {
$.each(attr, function (k, v) {
el.setAttributeNS(null, k, v);
});
}
return $(el);
}
if (document.createElementNS && document.createElementNS("http://www.w3.org/2000/svg", "svg").createSVGRect) {
// =======================================================================================
// SVG Rendering
// =======================================================================================
/**
* Rendering strategy that creates a SVG tree.
*/
render = function (target, d) {
var innerRadius = d.width * 2 + d.space;
var r = (innerRadius + d.length + Math.ceil(d.width / 2) + 1);
var el = svg().width(r * 2).height(r * 2);
var g = svg('g', {
'stroke-width': d.width,
'stroke-linecap': 'round',
stroke: d.color
}).appendTo(svg('g', { transform: 'translate(' + r + ',' + r + ')' }).appendTo(el));
for (var i = 0; i < d.segments; i++) {
g.append(svg('line', {
x1: 0,
y1: innerRadius,
x2: 0,
y2: innerRadius + d.length,
transform: 'rotate(' + (360 / d.segments * i) + ', 0, 0)',
opacity: $.fn.activity.getOpacity(d, i)
}));
}
return $('<div>').append(el).width(2 * r).height(2 * r);
};
// Check if Webkit CSS animations are available, as they work much better on the iPad
// than setTimeout() based animations.
if (document.createElement('div').style.WebkitAnimationName !== undefined) {
var animations = {};
/**
* Animation strategy that uses dynamically created CSS animation rules.
*/
animate = function (el, steps, duration) {
if (!animations[steps]) {
var name = 'spin' + steps;
var rule = '@-webkit-keyframes ' + name + ' {';
for (var i = 0; i < steps; i++) {
var p1 = Math.round(100000 / steps * i) / 1000;
var p2 = Math.round(100000 / steps * (i + 1) - 1) / 1000;
var value = '% { -webkit-transform:rotate(' + Math.round(360 / steps * i) + 'deg); }\n';
rule += p1 + value + p2 + value;
}
rule += '100% { -webkit-transform:rotate(100deg); }\n}';
document.styleSheets[0].insertRule(rule);
animations[steps] = name;
}
el.css('-webkit-animation', animations[steps] + ' ' + duration + 's linear infinite');
};
}
else {
/**
* Animation strategy that transforms a SVG element using setInterval().
*/
animate = function (el, steps, duration) {
var rotation = 0;
var g = el.find('g g').get(0);
el.data('interval', setInterval(function () {
g.setAttributeNS(null, 'transform', 'rotate(' + (++rotation % steps * (360 / steps)) + ')');
}, duration * 1000 / steps));
};
}
}
else {
// =======================================================================================
// VML Rendering
// =======================================================================================
var s = $('<shape>').css('behavior', 'url(#default#VML)');
$('body').append(s);
if (s.get(0).adj) {
// VML support detected. Insert CSS rules for group, shape and stroke.
var sheet = document.createStyleSheet();
$.each(['group', 'shape', 'stroke'], function () {
sheet.addRule(this, "behavior:url(#default#VML);");
});
/**
* Rendering strategy that creates a VML tree.
*/
render = function (target, d) {
var innerRadius = d.width * 2 + d.space;
var r = (innerRadius + d.length + Math.ceil(d.width / 2) + 1);
var s = r * 2;
var o = -Math.ceil(s / 2);
var el = $('<group>', { coordsize: s + ' ' + s, coordorigin: o + ' ' + o }).css({ top: o, left: o, width: s, height: s });
for (var i = 0; i < d.segments; i++) {
el.append($('<shape>', { path: 'm ' + innerRadius + ',0 l ' + (innerRadius + d.length) + ',0' }).css({
width: s,
height: s,
rotation: (360 / d.segments * i) + 'deg'
}).append($('<stroke>', { color: d.color, weight: d.width + 'px', endcap: 'round', opacity: $.fn.activity.getOpacity(d, i) })));
}
return $('<group>', { coordsize: s + ' ' + s }).css({ width: s, height: s, overflow: 'hidden' }).append(el);
};
/**
* Animation strategy that modifies the VML rotation property using setInterval().
*/
animate = function (el, steps, duration) {
var rotation = 0;
var g = el.get(0);
el.data('interval', setInterval(function () {
g.style.rotation = ++rotation % steps * (360 / steps);
}, duration * 1000 / steps));
};
}
$(s).remove();
}
})(jQuery);
// By: Hans Fjällemark and John Papa
// https://github.com/CodeSeven/KoLite
;(function($) {
"use strict";
/* ACTIVITY INDICATOR EXTENDED CLASS DEFINITION
* ========================= */
var Indicator = function($element) {
this.$element = $element;
this.onlyIcon = this.$element.contents().length === 1 && this.$element.children('i').length === 1;
this.activityText = this.$element.data('activity-text');
this.isIndicatorOnly = this.$element.is('i');
this.icons = this.$element.children('i');
};
Indicator.prototype = {
createTemporaryIcon: function() {
if (this.onlyIcon)
return;
//this.temporaryIcon = $('<i class="icon-" style="padding-left: 5px"></i>');
this.temporaryIcon = $('<i style="display: inline-block; padding-left: 5px; width: 14px;"></i>');
this.$element.append(this.temporaryIcon);
},
hideExistingIcons: function() {
if (this.onlyIcon)
this.icons.css('visibility', 'hidden');
},
moveSpinnerToFront: function() {
$('body > div, body > group').first().css('z-index', 9999);
},
removeTemporaryIcon: function() {
if (!this.temporaryIcon)
return;
this.temporaryIcon.remove();
this.temporaryIcon = null;
},
setText: function(state) {
if (!this.activityText)
return;
var data = this.$element.data(),
val = this.$element.is('input') ? 'val' : 'html';
if (state === 'activity')
this.$element.data('resetText', this.$element[val]());
this.$element[val](data[state + 'Text']);
},
showExistingIcons: function() {
this.icons.css('visibility', 'visible');
},
start: function() {
this.isBusy = true;
this.setText('activity');
this.createTemporaryIcon();
this.hideExistingIcons();
if (this.$element.is('button'))
this.$element.addClass('disabled').attr('disabled', 'disabled');
this.$element.activity({
align: this.onlyIcon || this.isIndicatorOnly ? 'center' : 'right',
length: this.isIndicatorOnly ? 5 : 2,
padding: 12,
outside: true,
segments: this.isIndicatorOnly ? 12 : 10,
space: this.isIndicatorOnly ? 2 : 1,
width: 1.5
});
this.moveSpinnerToFront();
},
stop: function() {
this.removeTemporaryIcon();
this.showExistingIcons();
this.isBusy = false;
this.setText('reset');
this.$element.activity(false);
this.$element.removeClass('disabled').removeAttr('disabled');
},
update: function(isLoading) {
if (isLoading && !this.isBusy) {
this.start();
}
if (!isLoading && this.isBusy) {
this.stop();
}
}
};
/* ACTIVITY INDICATOR EXTENDED PLUGIN DEFINITION
* ========================== */
$.fn.activityEx = function(isLoading) {
var activity = function($element) {
if (!isLoading) {
$element.activity(false);
return;
}
var length = Math.round($element.height() / 4);
var isInput = $element.is('input');
$element.activity({
align: $element.is('input') ? 'right' : 'center',
length: length,
padding: isInput ? length : 0,
outside: true,
segments: Math.max(10, 10 + (length - 5)),
space: 1,
width: 1.5
});
$('body > div').first().css('z-index', 9999);
},
buttonActivity = function($element) {
var data = $element.data('activityEx');
if (!data)
$element.data('activityEx', (data = new Indicator($element)));
data.update(isLoading);
};
return this.each(function() {
$(this).is('button, input, a') ? buttonActivity($(this)) : activity($(this));
});
};
})(jQuery);
;(function ($, ko) {
ko.bindingHandlers.activity = {
init: function (element) {
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$(element).activityEx(false);
});
},
update: function (element, valueAccessor) {
var activity = valueAccessor()();
typeof activity !== 'boolean' || $(element).activityEx(activity);
}
};
})(jQuery, ko);