1
|
|
|
var $ = jQuery, |
2
|
|
|
hooks, emptyFunction; |
3
|
|
|
|
4
|
|
|
window.wp = window.wp || {}; |
5
|
|
|
|
6
|
|
|
window.wp.wordpoints = window.wp.wordpoints || {}; |
7
|
|
|
|
8
|
|
|
hooks = wp.wordpoints.hooks = { |
|
|
|
|
9
|
|
|
model: {}, |
10
|
|
|
view: {}, |
11
|
|
|
controller: {}, |
12
|
|
|
reactor: {}, |
13
|
|
|
extension: {}, |
14
|
|
|
util: {} |
15
|
|
|
}; |
16
|
|
|
|
17
|
|
|
_.extend( hooks, Backbone.Events ); |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ======================================================================== |
21
|
|
|
* UTILITIES |
22
|
|
|
* ======================================================================== |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
// We override the basic `extend` function so that we can perform a |
26
|
|
|
// little magic. |
27
|
|
|
hooks.util.extend = function () { |
28
|
|
|
|
29
|
|
|
var obj = this; |
30
|
|
|
|
31
|
|
|
// Instead of duplicating Backbone's `extend()` logic here, we look up the tree |
32
|
|
|
// until we find the original extend function By doing it this way we are |
33
|
|
|
// compatible with other extend() methods being inserted somewhere up the tree |
34
|
|
|
// beside just Backbone's. |
35
|
|
|
while ( obj.extend === hooks.util.extend ) { |
36
|
|
|
obj = obj.__super__.constructor; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// We create the extension here, which we'll call `child`. From now on we can |
40
|
|
|
// think of ourselves as a parent. |
41
|
|
|
var child = obj.extend.apply( this, arguments ); |
42
|
|
|
|
43
|
|
|
var parent = this.prototype; |
44
|
|
|
|
45
|
|
|
var childMethods = child.prototype; |
46
|
|
|
|
47
|
|
|
// If there are already descendants defined on the prototype, we need to add any |
48
|
|
|
// overridden methods as the children of the last generation. |
49
|
|
|
while ( childMethods.__child__ ) { |
50
|
|
|
childMethods = childMethods.__child__; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
childMethods.__child__ = {}; |
54
|
|
|
|
55
|
|
|
// This is where the magic happens. If the child defines any of the |
56
|
|
|
// parent's prototype methods in its own prototype, we override them with |
57
|
|
|
// the parent method, but save the child method for later in `__child__`. |
58
|
|
|
// This allows the parent method to call the child method. |
59
|
|
|
var iterator = function ( method, name ) { |
60
|
|
|
|
61
|
|
|
if ( 'constructor' === name ) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ( ! _.isFunction( method ) ) { |
|
|
|
|
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
var childMethod = child.prototype[ name ]; |
70
|
|
|
|
71
|
|
|
// If the child doesn't define this method, we still include a stub |
72
|
|
|
// so that the parent can call it without checking whether it exists. |
73
|
|
|
if ( childMethod === method ) { |
74
|
|
|
childMethod = emptyFunction( name ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// We need to override the child method in case there will be grandchildren. |
78
|
|
|
childMethods.__child__[ name ] = function () { |
79
|
|
|
|
80
|
|
|
var __child__; |
81
|
|
|
|
82
|
|
|
// If there are grandchildren, we need to update __child__ to refer to |
83
|
|
|
// them, instead of to us and our siblings (otherwise we'd loop |
84
|
|
|
// infinitely). |
85
|
|
|
if ( this.__child__ ) { |
86
|
|
|
__child__ = this.__child__; |
87
|
|
|
this.__child__ = __child__.__child__; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
childMethod.apply( this, arguments ); |
91
|
|
|
|
92
|
|
|
// Put everything back when we are done. |
93
|
|
|
if ( __child__ ) { |
94
|
|
|
this.__child__ = __child__; |
95
|
|
|
} |
96
|
|
|
}; |
97
|
|
|
|
98
|
|
|
child.prototype[ name ] = method; |
99
|
|
|
}; |
100
|
|
|
|
101
|
|
|
_.each( parent, iterator, this ); |
|
|
|
|
102
|
|
|
|
103
|
|
|
return child; |
104
|
|
|
}; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* An empty function. |
108
|
|
|
* |
109
|
|
|
* To be used for "abstract" functions in objects that use hooks.util.extend(). |
110
|
|
|
* |
111
|
|
|
* @returns {*} |
112
|
|
|
*/ |
113
|
|
|
hooks.util.emptyFunction = emptyFunction = function ( methodName ) { |
114
|
|
|
|
115
|
|
|
return function self() { |
116
|
|
|
|
117
|
|
|
if ( this.__child__ ) { |
|
|
|
|
118
|
|
|
|
119
|
|
|
var descendant = this.__child__; |
120
|
|
|
|
121
|
|
|
// It is possible that the method name will not be set on the child, but |
122
|
|
|
// will be set on a lower descendant. This can happen when the method was |
123
|
|
|
// not on the original patriarch but was set by a later ancestor. |
124
|
|
|
while ( descendant && ! descendant[ methodName ] ) { |
125
|
|
|
descendant = descendant.__child__; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// If we found a descendant with the method, call it, and pass back any |
129
|
|
|
// returned value. |
130
|
|
|
if ( descendant ) { |
|
|
|
|
131
|
|
|
return descendant[ methodName ].apply( this, arguments ); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
}; |
135
|
|
|
}; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* |
139
|
|
|
* @param {object} object The primary parameter to compare. |
140
|
|
|
* @param {array} hierarchy The primary parameter to compare. |
141
|
|
|
* |
142
|
|
|
* @return {object} |
143
|
|
|
*/ |
144
|
|
|
hooks.util.getDeep = function( object, hierarchy ) { |
145
|
|
|
|
146
|
|
|
for ( var i = 0; i < hierarchy.length; i++ ) { |
147
|
|
|
|
148
|
|
|
if ( typeof object === 'undefined' ) { |
149
|
|
|
break; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
object = object[ hierarchy[ i ] ]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return object; |
156
|
|
|
}; |
157
|
|
|
|
158
|
|
|
hooks.util.setDeep = function ( object, hierarchy, value ) { |
159
|
|
|
|
160
|
|
|
var field = hierarchy.pop(); |
161
|
|
|
|
162
|
|
|
_.each( hierarchy, function ( field ) { |
|
|
|
|
163
|
|
|
if ( typeof object[ field ] === 'undefined' ) { |
164
|
|
|
if ( _.isNumber( field ) ) { |
|
|
|
|
165
|
|
|
object[ field ] = []; |
166
|
|
|
} else { |
167
|
|
|
object[ field ] = {}; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
object = object[ field ]; |
172
|
|
|
}); |
173
|
|
|
|
174
|
|
|
object[ field ] = value; |
175
|
|
|
}; |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
wp.wordpoints.$cache = function ( $ ) { |
|
|
|
|
179
|
|
|
|
180
|
|
|
var cache = {}; |
181
|
|
|
|
182
|
|
|
return function ( selector ) { |
183
|
|
|
|
184
|
|
|
if ( typeof cache[ selector ] === 'undefined' ) { |
185
|
|
|
cache[ selector ] = $.call( this, selector ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return cache[ selector ]; |
189
|
|
|
}; |
190
|
|
|
}; |
191
|
|
|
|
192
|
|
|
wp.wordpoints.$ = wp.wordpoints.$cache( $ ); |
|
|
|
|
193
|
|
|
|
194
|
|
|
_.extend( hooks, { |
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* hooks.template( id ) |
197
|
|
|
* |
198
|
|
|
* Fetch a JavaScript template for an id, and return a templating function for it. |
199
|
|
|
* |
200
|
|
|
* See wp.template() in `wp-includes/js/wp-util.js`. |
201
|
|
|
*/ |
202
|
|
|
template: function ( id ) { |
203
|
|
|
return wp.template( 'wordpoints-' + id ); |
|
|
|
|
204
|
|
|
}, |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* hooks.textTemplate( text ) |
208
|
|
|
* |
209
|
|
|
* Returns a WordPress-style templating function for a text string. |
210
|
|
|
* |
211
|
|
|
* See wp.template() in `wp-includes/js/wp-util.js`. |
212
|
|
|
*/ |
213
|
|
|
textTemplate: function ( text ) { |
214
|
|
|
var options = { |
215
|
|
|
evaluate: /<#([\s\S]+?)#>/g, |
216
|
|
|
interpolate: /\{\{\{([\s\S]+?)\}\}\}/g, |
217
|
|
|
escape: /\{\{([^\}]+?)\}\}(?!\})/g, |
218
|
|
|
variable: 'data' |
219
|
|
|
}; |
220
|
|
|
|
221
|
|
|
return _.template( text, options ); |
|
|
|
|
222
|
|
|
}, |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* hooks.post( [action], [data] ) |
226
|
|
|
* |
227
|
|
|
* Sends a POST request to WordPress. |
228
|
|
|
* See wp.ajax.post() in `wp-includes/js/wp-util.js`. |
229
|
|
|
* |
230
|
|
|
* @borrows wp.ajax.post as post |
231
|
|
|
*/ |
232
|
|
|
post: wp.ajax.post, |
|
|
|
|
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* hooks.ajax( [action], [options] ) |
236
|
|
|
* |
237
|
|
|
* Sends an XHR request to WordPress. |
238
|
|
|
* See wp.ajax.send() in `wp-includes/js/wp-util.js`. |
239
|
|
|
* |
240
|
|
|
* @borrows wp.ajax.send as ajax |
241
|
|
|
*/ |
242
|
|
|
ajax: wp.ajax.send, |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Truncates a string by injecting an ellipsis into the middle. |
246
|
|
|
* Useful for filenames. |
247
|
|
|
* |
248
|
|
|
* @param {String} string |
249
|
|
|
* @param {Number} [length=30] |
|
|
|
|
250
|
|
|
* @param {String} [replacement=…] |
|
|
|
|
251
|
|
|
* @returns {String} The string, unless length is greater than string.length. |
252
|
|
|
*/ |
253
|
|
|
truncate: function( string, length, replacement ) { |
254
|
|
|
length = length || 30; |
255
|
|
|
replacement = replacement || '…'; |
256
|
|
|
|
257
|
|
|
if ( string.length <= length ) { |
258
|
|
|
return string; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return string.substr( 0, length / 2 ) + replacement + string.substr( -1 * length / 2 ); |
262
|
|
|
} |
263
|
|
|
}); |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* ======================================================================== |
267
|
|
|
* MODELS |
268
|
|
|
* ======================================================================== |
269
|
|
|
*/ |
270
|
|
|
|
271
|
|
|
hooks.model.Base = require( './models/base.js' ); |
272
|
|
|
hooks.model.Arg = require( './models/arg.js' ); |
273
|
|
|
hooks.model.Args = require( './models/args.js' ); |
274
|
|
|
hooks.model.Reaction = require( './models/reaction.js' ); |
275
|
|
|
hooks.model.Reactions = require( './models/reactions.js' ); |
276
|
|
|
hooks.model.Event = require( './models/event.js' ); |
277
|
|
|
|
278
|
|
|
// Clean up. Prevents mobile browsers caching |
279
|
|
|
$(window).on('unload', function(){ |
280
|
|
|
window.wp = null; |
281
|
|
|
}); |
282
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.