forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimationHelpers.js
More file actions
103 lines (84 loc) · 2.49 KB
/
animationHelpers.js
File metadata and controls
103 lines (84 loc) · 2.49 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
/**
* MUI CSS/JS animation helper module
* @module lib/animationHelpers
*/
'use strict';
var jqLite = require('./jqLite'),
util = require('./util'),
animationEvents = 'animationstart mozAnimationStart webkitAnimationStart',
animationCallbacks = {};
/**
* Register callbacks
* @param {String} name - The animation name
* @param {Function} callbackFn = The callback function
*/
function onAnimationStartFn(name, callbackFn) {
// get/set callback function
var callbacks = animationCallbacks[name];
if (!callbacks) callbacks = animationCallbacks[name] = [];
callbacks.push(callbackFn);
// initialize listeners
if (!this.init) {
// add css classes
loadCss();
// add listener
jqLite.on(document, animationEvents, animationStartHandler, true);
// set flag
this.init = true;
}
}
/**
* Animation start handler
* @param {Event} ev - The DOM event
*/
function animationStartHandler(ev) {
var callbacks = animationCallbacks[ev.animationName] || [],
i = callbacks.length;
// exit if a callback hasn't been registered
if (!i) return;
// stop other callbacks from firing
ev.stopImmediatePropagation();
// iterate through callbacks
while (i--) callbacks[i](ev);
}
/**
* Load animation css
*/
function loadCss() {
// define rules
var rules = [
['.mui-btn', 'mui-btn-inserted'],
['[data-mui-toggle="dropdown"]', 'mui-dropdown-inserted'],
[
'.mui-btn[data-mui-toggle="dropdown"]',
'mui-btn-inserted,mui-dropdown-inserted'
],
['[data-mui-toggle="tab"]', 'mui-tab-inserted'],
['.mui-textfield > input', 'mui-textfield-inserted'],
['.mui-textfield > textarea', 'mui-textfield-inserted'],
['.mui-textfield > input:-webkit-autofill', 'mui-textfield-autofill'],
['.mui-textfield > textarea:-webkit-autofill', 'mui-textfield-autofill'],
['.mui-select > select', 'mui-select-inserted'],
['.mui-select > select ~ .mui-event-trigger', 'mui-node-inserted'],
['.mui-select > select:disabled ~ .mui-event-trigger', 'mui-node-disabled']
];
// build css
var css = '',
rule;
for (var i=0, m=rules.length; i < m; i++) {
rule = rules[i];
css += '@keyframes ' + rule[1];
css += '{from{transform:none;}to{transform:none;}}';
css += rule[0];
css += '{animation-duration:0.0001s;animation-name:' + rule[1] + ';}';
}
// add CSS to DOM
util.loadStyle(css);
}
/**
* Define module API
*/
module.exports = {
animationEvents: animationEvents,
onAnimationStart: onAnimationStartFn
}