-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdromos.utilities.js
More file actions
322 lines (299 loc) · 12.6 KB
/
dromos.utilities.js
File metadata and controls
322 lines (299 loc) · 12.6 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
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
/*=============================
dromos javascript library utilities.
This contains helper functions used by the dromos libraries
=============================*/
define(['jquery'],
function($jQ)
{
/*********************
* UPDATE THE STRING PROTOTYPE
*********************/
/**
* Pads the string on the left using the character provided, ensures the string is
* no longer than tnFinal length after padding.
*/
String.prototype.padLeft = function(tcPadPattern, tnFinalLength)
{
var loRE = new RegExp(".{" + tnFinalLength + "}$");
var lcPadding = "";
do
{
lcPadding += tcPadPattern;
} while(lcPadding.length < tnFinalLength);
return loRE.exec(lcPadding + this);
}
/**
* Pads the string on the right using the character provided, ensures the string is
* no longer than tnFinal length after padding.
*/
String.prototype.padRight = function(tcPadPattern, tnFinalLength)
{
var loRE = new RegExp("^.{" + tnFinalLength + "}");
var lcPadding = "";
do
{
lcPadding += tcPadPattern;
} while(lcPadding.length < tnFinalLength);
return loRE.exec(lcPadding + this);
}
/**
* Trims all white space from the front of the string
*/
String.prototype.lTrim = function()
{
return this.replace(/^\s+/, '');
}
/**
* Trims all white space from the back of the string
*/
String.prototype.rTrim = function()
{
return this.replace(/\s+$/, '');
}
/**
* Trims all white space from both sides of the string
*/
String.prototype.allTrim = function()
{
return this.replace(/^\s+|\s+$/g, '');
}
/**
* Functions for allowing animation of background opacity
*/
var loRgba = /^rgba\((\d+),\s*(\d+),\s*(\d+)\,\s*(\d+(\.\d+)?)\)$/;
var loRgb = /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/;
function getRgbaColorValue(toElement)
{
var loBackground = dromos.$jQ.css(toElement, 'background-color');
return (loBackground.indexOf('rgba') !== -1) ?
loRgba.exec(loBackground) :
loRgb.exec(loBackground);
};
dromos.$jQ.cssNumber.backgroundColorAlpha = true;
dromos.$jQ.cssHooks.backgroundColorAlpha =
{
get: function(toElement)
{
var laReturn = getRgbaColorValue(toElement);
return laReturn.length >= 4 ? laReturn[4] : 1;
},
set: function(toElement, tnValue)
{
var laColour = getRgbaColorValue(toElement);
toElement.style.backgroundColor = 'rgba(' + laColour[1] + ',' + laColour[2] + ',' + laColour[3] + ',' + tnValue + ')';
}
};
dromos.$jQ.fx.step.backgroundColorAlpha = function(toFX)
{
dromos.$jQ.cssHooks.backgroundColorAlpha.set(toFX.elem, toFX.now + toFX.unit);
};
// Dromos utilities object
dromos.utilities = {
/**
* Gets the attribute specified on the element specified
* @param Element toElement the element to get the attribute value from
* @param String tcAttribute the name of the attribte to get the value of
* @return the value of the attribute or null if the attribute did not exist on the element
*/
getAttribute : function(toElement, tcAttribute)
{
if (toElement[tcAttribute] !== undefined)
{
return toElement[tcAttribute];
}
else if (toElement.getAttribute)
{
return toElement.getAttribute(tcAttribute);
}
else if (toElement.attributes && toElement.attributes[tcAttribute])
{
return toElement.attributes[tcAttribute];
}
return null;
},
// Checks if the object is of the type specified.
isType : function(toObject, tcType)
{return Object.prototype.toString.call(toObject) === ("[object " + tcType + "]");},
// Extracts the script tag from the first script tag with a src containing tcScript
getScript : function(tcScript)
{
// TODO: Test with paramaterised scripts
// TODO: Test with namespaced scripts (e.g. dromos dromos.utilities)
var laScripts = document.getElementsByTagName("script");
var loRegEx = new RegExp(".+?"+ tcScript, "i");
for (var i=0, lnLength = laScripts.length; i<lnLength; i++)
{
if (loRegEx.test(laScripts[i].src))
{
return laScripts[i];
}
}
return null;
},
// Returns true if the element passed is attached to the document
isAttachedToDom : function(toElement)
{
var loAncestor = toElement;
while(loAncestor.parentNode)
{
loAncestor = loAncestor.parentNode;
}
return !!(loAncestor.body);
},
// Popup related functionallity
popup : {
popups : {},
lastSettings : {
'location': 'no',
'status': 'no',
'titlebar': 'no',
'toolbar': 'no',
'menubar': 'no',
'directories': 'no',
'resizable': 'no',
'scrollbars': 'no',
'width': '250',
'height': '250'
},
// Checks if the current window has been opened by javascript, if so returns true
isPopup : function(){return !!window.opener;},
// Gets the specified popup if it exists, or null
get : function(tcID){return this.popups[tcID] ? this.popups[tcID] : null;},
// Closes the specified popup, no op if no popup is found
close : function(tcID)
{
var loPopup = this.get(tcID);
if (loPopup != null)
{
loPopup.close();
this.popups[tcID] = null;
}
},
/**
* Pops up a window for the user and returns a reference to that window.
* if the popup is blocked then the user will be asked to disable their blocker and
* try again.
* tcURL - the url to open
* tcID - the identity of the popup, if none is given a default will be used
* toOptions - the options to use for the window
* tlUseBlank - if the user has a popup blocker and the window is blocked, use _blank instead
**/
open : function(tcURL, tcID, toOptions, tlUseBlank)
{
// Prepare any defaults
if (!tcURL){tcURL = "";}
if (!tcID){tcID = "default";}
// Merge the options with the default options
var loDefaults = this.lastSettings;
if (toOptions)
{
for (var loProp in toOptions)
{
loDefaults[loProp] = toOptions[loProp];
}
}
// Close previously opened popup
this.close(tcID);
// Create the popup string
var lcPopup = "";
for (var lcProp in loDefaults)
{
lcPopup += lcProp + '=' + loDefaults[lcProp] + ',';
}
var loPopup = window.open(tcURL, tcID, lcPopup.substring(0, lcPopup.length-1));
if (loPopup)
{
if (window.focus)
{
loPopup.focus();
this.popups[tcID] = loPopup;
return loPopup;
}
}
else if (tlUseBlank)
{
// Open the URL as a link with a blank target
// TODO: Implement this
alert('Please disable your pop-up blocker and try again.');
}
else
{
alert('Please disable your pop-up blocker and try again.');
}
return null;
}
},
// Adds an event listener to the element specified
addEventListener : (function(){
return window.addEventListener ?
function(toElement, toCallback, tcEventType)
{
toElement['_'+tcEventType+'Handler'] = toElement['_'+tcEventType+'Handler'] || {};
toElement['_'+tcEventType+'Handler'][toCallback.toString()] = toElement['_'+tcEventType+'Handler'][toCallback] || function(e){toCallback(e)};
toElement.addEventListener(tcEventType, toElement['_'+tcEventType+'Handler'][toCallback.toString()], false);
} :
function(toElement, toCallback, tcEventType)
{
toElement['_'+tcEventType+'Handler'] = toElement['_'+tcEventType+'Handler'] || {};
toElement['_'+tcEventType+'Handler'][toCallback.toString()] = toElement['_'+tcEventType+'Handler'][toCallback] || function(e){toCallback(window.event)};
toElement.attachEvent("on" + tcEventType, toElement['_'+tcEventType+'Handler'][toCallback.toString()]);
};
})(),
// Removes an event listener from the element specified
// TODO: ensure this is removing the event
removeEventListener : (function(){
return window.removeEventListener ?
function(toElement, toCallback, tcEventType)
{
toElement.removeEventListener(tcEventType, toElement['_'+tcEventType+'Handler'][toCallback.toString()], false);
delete toElement['_'+tcEventType+'Handler'][toCallback.toString()];
} :
function(toElement, toCallback, tcEventType)
{
toElement.detachEvent("on" + tcEventType, toElement['_'+tcEventType+'Handler'][toCallback.toString()]);
delete toElement['_'+tcEventType+'Handler'][toCallback.toString()];
};
})(),
/**
* Uses bit.ly to shorten the URL provided
**/
shortenURL : function(tcURL, tcUser, tcApiKey, tcCallback)
{
$jQ.getJSON("http://api.bitly.com/v3/shorten?callback=?",
{
"format": "json",
"apiKey": tcApiKey,
"login": tcUser,
"longUrl": encodeURI(tcUrl)
}, function(toResponse){toCallback(toResponse.data.url);});
},
// Takes a URL and "Cleans" it by adding to the url, the default is to add the version from cachebuster
cleanURL : function(tcURL)
{return tcURL + (tcURL.indexOf("?") < 0 ? "?" : "&") + "version=" + dromos.Bootstrap["version"];},
/**
* Initialises toModule by calling toInitFunction(init by default) passing the
* element and index of the element in the list of module elements. toModule should already be loaded
* for this call. toInitConfig can be configuration options, or a function
* that returns configuration options, if it exists, the options will be passed to the
* init function as well
*/
initialiseModule : function(toModule, toElement, tnIndex, toInitFunction, toInitConfig)
{
toInitFunction = toInitFunction || "init";
toInitConfig = this.isType(toInitConfig, "Function") ? toInitConfig.call(null, toElement) : toInitConfig || {};
if (toModule && toModule[toInitFunction])
{
toModule[toInitFunction].apply(toModule, [toElement, toInitConfig, tnIndex]);
};
},
// Creates an element using the namespace if possible
createElement : (function(){
return document.createElementNS ?
function(tcTagName){return (dromos.utilities.createElement[tcTagName] = dromos.utilities.createElement[tcTagName] || document.createElementNS( 'http://www.w3.org/1999/xhtml', tcTagName)).cloneNode(false);} :
function(tcTagName){return (dromos.utilities.createElement[tcTagName] = dromos.utilities.createElement[tcTagName] || document.createElement(tcTagName)).cloneNode(false);};
})(),
// Creates a callback function which will call toMethod on toTarget and return the result.
createCallback : function(toMethod, toTarget){return function(){return toMethod.apply(toTarget, arguments);}}
};
return dromos.utilities;
});