|
| 1 | +//@license |
| 2 | +// ========================================================================== |
| 3 | +// SproutCore -- JavaScript Application Framework |
| 4 | +// copyright 2006-2007, Sprout Systems, Inc. and contributors. |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | +// copy of this software and associated documentation files (the "Software"), |
| 8 | +// to deal in the Software without restriction, including without limitation |
| 9 | +// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | +// and/or sell copies of the Software, and to permit persons to whom the |
| 11 | +// Software is furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | +// DEALINGS IN THE SOFTWARE. |
| 23 | +// |
| 24 | +// For more information about SproutCore, visit http://www.sproutcore.com |
| 25 | +// |
| 26 | +// |
| 27 | +// ========================================================================== |
| 28 | +//@license |
| 29 | + |
| 30 | +// ========================================================================== |
| 31 | +// Utility Classes |
| 32 | +// Author: Charles Jolley |
| 33 | +// copyright 2006, Sprout Systems, Inc. |
| 34 | +// |
| 35 | +// This file contains a number of utility methods and classes used throughout |
| 36 | +// SproutCore. This should be loaded after your load Prototype but before you |
| 37 | +// load any other SproutCore objects. In general, this is the only |
| 38 | +// dependency most SproutCore objects will have. |
| 39 | +// |
| 40 | +// ========================================================================== |
| 41 | + |
| 42 | +// All objects live in the SproutCore namespace, which is also availabe in the |
| 43 | +// abreviation SC. |
| 44 | +SproutCore = {} ; SC = SproutCore ; |
| 45 | + |
| 46 | +// this makes for some nicer to read code |
| 47 | +var YES = true ; var NO = false ; |
| 48 | + |
| 49 | +// this is used by the JavascriptCompile class on the server side. You can |
| 50 | +// use this to automatically determine the order javascript files need to be |
| 51 | +// included in. On the client side, this is a NOP. |
| 52 | +function require(file) { return null ; } |
| 53 | + |
| 54 | +// implement window.console.log() for IE. |
| 55 | +if (!window.console) { |
| 56 | + window.console = { |
| 57 | + _output: [], |
| 58 | + log: function(str) { this._output.push(str) ; }, |
| 59 | + tail: function(lines) { |
| 60 | + if (!lines) lines = 1 ; |
| 61 | + var loc = this._output.length - lines ; |
| 62 | + if (loc < 0) loc = 0 ; |
| 63 | + var ret = [] ; |
| 64 | + while(loc < this._output.length) { |
| 65 | + ret.push(this._output[loc]) ; loc++ ; |
| 66 | + } |
| 67 | + return ret.join("\n"); |
| 68 | + } |
| 69 | + } ; |
| 70 | +} |
| 71 | +window.logCount = 0 ; |
| 72 | + |
| 73 | +// ........................................ |
| 74 | +// GENERAL UTILITIES |
| 75 | +// |
| 76 | + |
| 77 | +Object.extend(SC,{ |
| 78 | + |
| 79 | + _downloadFrames: 0, // count of download frames inserted into document |
| 80 | + |
| 81 | + download: function(path) { |
| 82 | + var tempDLIFrame=document.createElement('iframe'); |
| 83 | + var frameId = 'DownloadFrame_' + this._downloadFrames; |
| 84 | + tempDLIFrame.setAttribute('id',frameId); |
| 85 | + tempDLIFrame.style.border='10px'; |
| 86 | + tempDLIFrame.style.width='0px'; |
| 87 | + tempDLIFrame.style.height='0px'; |
| 88 | + tempDLIFrame.style.position='absolute'; |
| 89 | + tempDLIFrame.style.top='-10000px'; |
| 90 | + tempDLIFrame.style.left='-10000px'; |
| 91 | + // Don't set the iFrame content yet if this is Safari |
| 92 | + if (!(SC.isSafari())) { |
| 93 | + tempDLIFrame.setAttribute('src',path); |
| 94 | + } |
| 95 | + document.getElementsByTagName('body')[0].appendChild(tempDLIFrame); |
| 96 | + if (SC.isSafari()) { |
| 97 | + tempDLIFrame.setAttribute('src',path); |
| 98 | + } |
| 99 | + this._downloadFrames = this._downloadFrames + 1; |
| 100 | + if (!(SC.isSafari())) { |
| 101 | + var r = function() { |
| 102 | + document.body.removeChild(document.getElementById(frameId)); |
| 103 | + } ; |
| 104 | + var t = setTimeout(r,2000); |
| 105 | + } |
| 106 | + }, |
| 107 | + |
| 108 | + // Call this method during setup of your app to queue up methods to be |
| 109 | + // called once the entire document has finished loading. If you call this |
| 110 | + // method once the document has already loaded, then the function will be |
| 111 | + // called immediately. |
| 112 | + callOnLoad: function(func) { |
| 113 | + if (SC._onloadQueueFlushed) func.call(document); |
| 114 | + var queue = SC._onloadQueue || [] ; |
| 115 | + queue.push(func) ; SC._onloadQueue = queue ; |
| 116 | + }, |
| 117 | + |
| 118 | + // To flush the callOnLoad queue, you need to set window.onload=SC.didLoad |
| 119 | + didLoad: function() { |
| 120 | + |
| 121 | + SC.app = SC.Application.create(); |
| 122 | + SC.app.run(); |
| 123 | + |
| 124 | + // set the current language |
| 125 | + var b = $tag('body'); |
| 126 | + Element.addClassName(b, String.currentLanguage().toLowerCase()) ; |
| 127 | + |
| 128 | + // call the onloadQueue. |
| 129 | + var queue ; |
| 130 | + if (window.callOnLoad) { |
| 131 | + if (window.callOnLoad instanceof Array) { |
| 132 | + queue = window.callOnLoad ; |
| 133 | + } else if (window.callOnLoad instanceof Function) { |
| 134 | + queue = [window.callOnLoad] ; |
| 135 | + } |
| 136 | + } else queue = [] ; |
| 137 | + queue = queue.concat(SC._onloadQueue) ; |
| 138 | + var func = null ; |
| 139 | + while(func = queue.shift()) func.call(document) ; |
| 140 | + SC._onloadQueueFlushed = true ; |
| 141 | + |
| 142 | + // start the app; call main. |
| 143 | + if (window.main && (main instanceof Function)) main() ; // start app. |
| 144 | + |
| 145 | + // finally handle any routes if any. |
| 146 | + if (typeof Routes != 'undefined') { |
| 147 | + Routes.doRoutes() ; // old style. |
| 148 | + } else if (typeof SC.Routes != 'undefined') { |
| 149 | + SC.Routes.ping() ; // handle routes, if modules is installed. |
| 150 | + } |
| 151 | + |
| 152 | + }, |
| 153 | + |
| 154 | + // this will take a URL of any type and convert it to a fully qualified URL. |
| 155 | + normalizeURL: function(url) { |
| 156 | + if (url.slice(0,1) == '/') { |
| 157 | + url = window.location.protocol + '//' + window.location.host + url ; |
| 158 | + } else if ((url.slice(0,5) == 'http:') || (url.slice(0,6) == 'https:')) { |
| 159 | + // no change |
| 160 | + } else { |
| 161 | + url = window.location + '/' + url ; |
| 162 | + } |
| 163 | + return url ; |
| 164 | + }, |
| 165 | + |
| 166 | + // use this instead of typeOf() to get the type of item. The return values |
| 167 | + // are: 'string', 'number', 'function', 'class', 'object', 'hash', 'null', |
| 168 | + // 'undefined', 'boolean'. |
| 169 | + // 'object' will be returned for any items inheriting from SC.Object. 'hash' |
| 170 | + // is any other type of object. |
| 171 | + typeOf: function(item) { |
| 172 | + if (item === undefined) return T_UNDEFINED ; |
| 173 | + if (item === null) return T_NULL ; |
| 174 | + var ret = typeof(item) ; |
| 175 | + if (ret == "object") { |
| 176 | + if (item instanceof Array) { |
| 177 | + ret = T_ARRAY ; |
| 178 | + } else if (item instanceof Function) { |
| 179 | + ret = (item.isClass) ? T_CLASS : T_FUNCTION ; |
| 180 | + } else if (item instanceof SC.Error) { |
| 181 | + ret = T_ERROR ; |
| 182 | + } else if (item.isObject == true) { |
| 183 | + ret = T_OBJECT ; |
| 184 | + } else ret = T_HASH ; |
| 185 | + } else if (ret == T_FUNCTION) ret = (item.isClass) ? T_CLASS : T_FUNCTION; |
| 186 | + return ret ; |
| 187 | + }, |
| 188 | + |
| 189 | + // this will compare two values to see if they are equal. If you have two |
| 190 | + // values of unknown type, this is faster across all browsers than ===. |
| 191 | + isEqual: function(a,b) { |
| 192 | + if (a === null) { |
| 193 | + return b === null ; |
| 194 | + } else if (a === undefined) { |
| 195 | + return b === undefined ; |
| 196 | + } else if (typeof(a) == typeof(b)) { |
| 197 | + return a == b ; |
| 198 | + } |
| 199 | + }, |
| 200 | + |
| 201 | + isArray: function( obj ) |
| 202 | + { |
| 203 | + return ($type(obj) == T_ARRAY) || (obj && obj.objectAt); |
| 204 | + }, |
| 205 | + |
| 206 | + _nextGUID: 0, |
| 207 | + getGUID: function( obj ) |
| 208 | + { |
| 209 | + if (obj == null) return 0 ; |
| 210 | + return obj._guid ? obj._guid : (obj._guid = SC._nextGUID++); |
| 211 | + }, |
| 212 | + |
| 213 | + /** Browser and Platform info. */ |
| 214 | + Platform: { |
| 215 | + |
| 216 | + /** The current IE version number or 0 if not IE. */ |
| 217 | + IE: function() { |
| 218 | + if (Prototype.Browser.IE) { |
| 219 | + return (navigator.appVersion.match(/\bMSIE.*7\.\b/)) ? 7 : 6 ; |
| 220 | + } else return 0 ; |
| 221 | + }(), |
| 222 | + |
| 223 | + /** The current Safari major version number of 0 if not Safari */ |
| 224 | + Safari: function() { |
| 225 | + if (Prototype.Browser.WebKit) { |
| 226 | + var vers = parseInt(navigator.appVersion.replace(/^.*?AppleWebKit\/(\d+).*?$/,'$1'),0) ; |
| 227 | + return (vers > 420) ? 3 : 2 ; |
| 228 | + } return 0 ; |
| 229 | + }(), |
| 230 | + |
| 231 | + /** The current Firefox major version number or 0 if not Firefox */ |
| 232 | + Firefox: function() { |
| 233 | + if (Prototype.Browser.Gecko) { |
| 234 | + var ret = parseFloat((navigator.userAgent.match(/Firefox\/(.)/)[1]) || 0); |
| 235 | + if (ret < 1) ret = 1; |
| 236 | + return ret ; |
| 237 | + } else return 0 ; |
| 238 | + }(), |
| 239 | + |
| 240 | + isWindows: function() { |
| 241 | + return !!(navigator.appVersion.match(/(Windows)/)) ; |
| 242 | + }(), |
| 243 | + |
| 244 | + isMac: function() { |
| 245 | + if(Prototype.Browser.Gecko) { |
| 246 | + return !!(navigator.appVersion.match(/(Macintosh)/)); |
| 247 | + } else { |
| 248 | + return !!(navigator.appVersion.match(/(Mac OS X)/)) ; |
| 249 | + } |
| 250 | + }() |
| 251 | + |
| 252 | + }, |
| 253 | + |
| 254 | + // DEPRECATED. here for compatibility only. |
| 255 | + /** @private */ |
| 256 | + isIE: function() { |
| 257 | + return SC.Platform.IE > 0 ; |
| 258 | + }, |
| 259 | + |
| 260 | + /** @private */ |
| 261 | + isSafari: function() { |
| 262 | + return SC.Platform.Safari > 0 ; |
| 263 | + }, |
| 264 | + |
| 265 | + /** @private */ |
| 266 | + isSafari3: function() { |
| 267 | + return SC.Platform.Safari >= 3 ; |
| 268 | + }, |
| 269 | + |
| 270 | + /** @private */ |
| 271 | + isIE7: function() { |
| 272 | + return SC.Platform.IE >= 7 ; |
| 273 | + }, |
| 274 | + |
| 275 | + /** @private */ |
| 276 | + isIE6: function() { |
| 277 | + return (SC.Platform.IE >= 6) && (SC.Platform.IE < 7) ; |
| 278 | + }, |
| 279 | + |
| 280 | + /** @private */ |
| 281 | + isWindows: function() { |
| 282 | + return SC.Platform.isWindows; |
| 283 | + }, |
| 284 | + |
| 285 | + /** @private */ |
| 286 | + isMacOSX: function() { |
| 287 | + return SC.Platform.isMac ; |
| 288 | + }, |
| 289 | + |
| 290 | + /** @private */ |
| 291 | + isFireFox: function() { |
| 292 | + return SC.Platform.Firefox > 0 ; |
| 293 | + }, |
| 294 | + |
| 295 | + /** @private */ |
| 296 | + isFireFox2: function() { |
| 297 | + return SC.Platform.Firefox >= 2 ; |
| 298 | + } |
| 299 | + |
| 300 | +}); |
| 301 | + |
| 302 | +// Save the Platform.Browser name. |
| 303 | +SC.Platform.Browser = function() { |
| 304 | + if (SC.Platform.IE >0) { |
| 305 | + return 'IE'; |
| 306 | + } else if (SC.Platform.Safari > 0) { |
| 307 | + return 'Safari'; |
| 308 | + } else if (SC.Platform.Firefox >0) { |
| 309 | + return 'Firefox'; |
| 310 | + } |
| 311 | +}() ; |
| 312 | + |
| 313 | +T_ERROR = 'error' ; |
| 314 | +T_OBJECT = 'object' ; |
| 315 | +T_NULL = 'null'; |
| 316 | +T_CLASS = 'class' ; |
| 317 | +T_HASH = 'hash' ; |
| 318 | +T_FUNCTION = 'function' ; |
| 319 | +T_UNDEFINED = 'undefined' ; |
| 320 | +T_NUMBER = 'number' ; |
| 321 | +T_BOOL = 'boolean' ; |
| 322 | +T_ARRAY = 'array' ; |
| 323 | +T_STRING = 'string' ; |
| 324 | + |
| 325 | +$type = function(item) { return SC.typeOf(item); }; |
| 326 | + |
| 327 | +Object.extend(Object,{ |
| 328 | + |
| 329 | + // this will serialize a general JSON object into a URI. |
| 330 | + serialize: function(obj) { |
| 331 | + var ret = [] ; |
| 332 | + for(var key in obj) { |
| 333 | + var value = obj[key] ; |
| 334 | + if (typeof value == 'number') { value = '' + value ; } |
| 335 | + if (!(typeof value == 'string')) { value = value.join(','); } |
| 336 | + ret.push(encodeURIComponent(key) + "=" + encodeURIComponent(value)) ; |
| 337 | + } |
| 338 | + return ret.join('&') ; |
| 339 | + } |
| 340 | + |
| 341 | +}) ; |
| 342 | + |
| 343 | + |
| 344 | +// This will add or remove the class name based on the flag, allowing you to |
| 345 | +// treat it like a bool setting. Simplifies the common case where you need |
| 346 | +// to make a class name match a bool. |
| 347 | +Element.setClassName = function(element,className,flag) { |
| 348 | + if (flag) { |
| 349 | + element.addClassName(className); |
| 350 | + } else { |
| 351 | + element.removeClassName(className) ; |
| 352 | + } |
| 353 | +} ; |
| 354 | + |
| 355 | +// ........................................ |
| 356 | +// EVENT EXTENSIONS |
| 357 | +// |
| 358 | +Object.extend(Event,{ |
| 359 | + // get the character code for key pressed events. |
| 360 | + getCharCode: function(e) { |
| 361 | + return (e.keyCode) ? e.keyCode : ((e.which)?e.which:0) ; |
| 362 | + }, |
| 363 | + |
| 364 | + // get the pressed char as a string. |
| 365 | + getCharString: function(e) { |
| 366 | + return String.fromCharCode(Event.getCharCode(e)) ; |
| 367 | + }, |
| 368 | + |
| 369 | + pointerLocation: function(e) { |
| 370 | + return { x: Event.pointerX(e), y: Event.pointerY(e) }; |
| 371 | + }, |
| 372 | + |
| 373 | + ALT_KEY: '_ALT', |
| 374 | + CTRL_KEY: '_CTRL', |
| 375 | + SHIFT_KEY: '_SHIFT' |
| 376 | + |
| 377 | +}); |
| 378 | + |
0 commit comments