unbuilt/admin/assets/js/hooks/models.manifest.js   A
last analyzed

Complexity

Total Complexity 31
Complexity/F 2.21

Size

Lines of Code 281
Function Count 14

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 32
dl 0
loc 281
rs 9.8
c 0
b 0
f 0
wmc 31
mnd 2
bc 31
fnc 14
bpm 2.2142
cpm 2.2142
noi 17

9 Functions

Rating   Name   Duplication   Size   Complexity  
A $(window).unload 0 3 1
B hooks.util.extend 0 78 3
A wp.wordpoints.$cache 0 13 1
A hooks.util.getDeep 0 13 3
A _.extend.template 0 3 1
A models.manifest.js ➔ emptyFunction 0 23 1
A _.extend.textTemplate 0 10 1
A hooks.util.setDeep 0 18 1
A _.extend.truncate 0 10 2
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 = {
0 ignored issues
show
Bug introduced by
The variable wp seems to be never declared. If this is a global, consider adding a /** global: wp */ comment.

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.

Loading history...
9
	model: {},
10
	view: {},
11
	controller: {},
12
	reactor: {},
13
	extension: {},
14
	util: {}
15
};
16
17
_.extend( hooks, Backbone.Events );
0 ignored issues
show
Bug introduced by
The variable Backbone seems to be never declared. If this is a global, consider adding a /** global: Backbone */ comment.

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.

Loading history...
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

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.

Loading history...
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 ) ) {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

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.

Loading history...
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 );
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

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.

Loading history...
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__ ) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if this.__child__ is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
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 ) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if descendant is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
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 ) {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

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.

Loading history...
163
		if ( typeof object[ field ] === 'undefined' ) {
164
			if ( _.isNumber( field ) ) {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

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.

Loading history...
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 ( $ ) {
0 ignored issues
show
Bug introduced by
The variable wp seems to be never declared. If this is a global, consider adding a /** global: wp */ comment.

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.

Loading history...
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( $ );
0 ignored issues
show
Bug introduced by
The variable wp seems to be never declared. If this is a global, consider adding a /** global: wp */ comment.

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.

Loading history...
193
194
_.extend( hooks, {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

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.

Loading history...
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 );
0 ignored issues
show
Bug introduced by
The variable wp seems to be never declared. If this is a global, consider adding a /** global: wp */ comment.

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.

Loading history...
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 );
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

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.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable wp seems to be never declared. If this is a global, consider adding a /** global: wp */ comment.

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.

Loading history...
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]
0 ignored issues
show
Documentation Bug introduced by
The parameter length=30 does not exist. Did you maybe mean length instead?
Loading history...
250
	 * @param {String} [replacement=&hellip;]
0 ignored issues
show
Documentation Bug introduced by
The parameter replacement=&hellip; does not exist. Did you maybe mean replacement instead?
Loading history...
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 || '&hellip;';
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