forked from meowsus/jquery-state
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-state.js
158 lines (128 loc) · 4.63 KB
/
jquery-state.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
;(function ($, window, document, undefined) {
var pluginName = "state",
stateHistory = [],
// utilities
sanitizeArgument = function (arg) {
return (typeof arg === 'undefined') ? false : arg;
},
pluginInactive = function (element) {
return $.data(element, 'plugin_' + pluginName) !== true;
},
seedGUID = function () {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
},
getGUID = function () {
return seedGUID() + seedGUID() + seedGUID() + seedGUID();
},
// history helpers
getElementsInNamespace = function (namespace) {
var guids = [],
elements = [];
$.each(stateHistory, function (index, entry) {
if (entry.namespace === namespace) {
if (guids.indexOf(entry.guid) === -1) {
guids.push(entry.guid);
elements.push(entry.element);
}
}
});
return $(elements);
};
function Plugin (element, newState, namespace) {
this.element = element;
this.guid = getGUID();
this.init(newState, namespace);
}
Plugin.prototype = {
announce: function (newState, namespace) {
$(this.element).trigger('state:' + namespace, [
this.state,
newState
]);
},
addToStateHistory: function (newState, namespace) {
stateHistory.push({
guid: this.guid,
element: this.element,
state: newState,
namespace: namespace
});
},
addState: function (newState, namespace) {
if (typeof this.state[namespace] === 'undefined') {
this.state[namespace] = [];
}
this.state[namespace].push(newState);
},
createState: function (newState, namespace) {
this.state = {};
this.state[namespace] = [];
this.state[namespace].push(newState);
},
activate: function () {
$.data(this.element, 'plugin_' + pluginName, true);
},
init: function (newState, namespace) {
this.createState(newState, namespace);
this.activate();
}
};
$[pluginName] = function (action, namespace) {
action = sanitizeArgument(action);
namespace = sanitizeArgument(namespace);
if (action === 'destroy') {
stateHistory = [];
return stateHistory;
} else if (action === 'elementsIn') {
if ( ! namespace) {
throw new Error(
'`$.state("elementsIn")` requires a `namespace` String ' +
'as its second argument.'
);
}
return getElementsInNamespace(namespace);
} else {
return stateHistory;
}
};
$.fn[pluginName] = function (newState, namespace) {
newState = sanitizeArgument(newState);
namespace = sanitizeArgument(namespace);
if (newState && ! $.isPlainObject(newState)) {
throw new Error(
'jQuery State requires the first argument be a plain Object.'
);
}
if (newState && ! namespace) {
throw new Error(
'jQuery State requires the second argument to be a String.'
);
}
if (newState && namespace) {
return this.each(function () {
var instance;
if (pluginInactive(this)) {
instance = new Plugin(this, newState, namespace);
$.data(this, pluginName + '_instance', instance);
} else {
instance = $.data(this, pluginName + '_instance');
instance.addState(newState, namespace);
}
instance.addToStateHistory(newState, namespace);
instance.announce(newState, namespace);
return this;
});
} else {
var firstElement = $(this).first()[0];
if (pluginInactive(firstElement)) {
throw new Error(
'jQuery State has yet to be initialized on this element. ' +
'Initialize with `$(element).state(stateObj, namespace)`.'
);
}
return $.data(firstElement, pluginName + '_instance').state;
}
};
})(jQuery, window, document);