1 /** 2 @namespace Random utility methods 3 */ 4 rio.Utils = { 5 /** 6 Alternates returning the two input arguments. 7 */ 8 cycle: function(var1, var2) { 9 if (!this._CYCLE_COUNT) { this._CYCLE_COUNT = 0; } 10 this._CYCLE_COUNT++; 11 return this._CYCLE_COUNT % 2 == 1 ? var1 : var2; 12 }, 13 14 /** 15 Navigates to a given URL, allowing you to set the Rails-style HTTP method. 16 17 @param {String} location The url to goto 18 @param {String} action (optional) The HTTP verb to use. (currently supports get|delete) 19 @param {String} token (optional) A Rails-style authenticity token 20 */ 21 navigateTo: function(location, action, token) { 22 if (action == "delete") { 23 var f = document.createElement('form'); 24 f.style.display = 'none'; 25 Element.body().appendChild(f); 26 f.method = 'POST'; 27 f.action = location; 28 var m = document.createElement('input'); 29 m.setAttribute('type', 'hidden'); 30 m.setAttribute('name', '_method'); 31 m.setAttribute('value', 'delete'); 32 f.appendChild(m); 33 var s = document.createElement('input'); 34 s.setAttribute('type', 'hidden'); 35 s.setAttribute('name', 'authenticity_token'); 36 s.setAttribute('value', token); 37 f.appendChild(s); 38 f.submit(); 39 } else { 40 document.location.href = location; 41 } 42 }, 43 44 /** 45 Returns the singular argument if num == 1, otherwise returns the plural argument. 46 47 @param {Number} num The number to be evaluated for plurality 48 @param {Object} singular The return value if num == 1 49 @param {Object} plural The return value if num != 1 50 */ 51 pluralize: function(num, singular, plural) { 52 return (num == 1) ? singular : plural; 53 }, 54 55 browserFromUserAgent: function(agt) { 56 agt = agt.toLowerCase(); 57 if (agt.indexOf("opera") != -1) { return 'Opera'; } 58 if (agt.indexOf("firefox") != -1) { return 'Firefox'; } 59 if (agt.indexOf("safari") != -1) { return 'Safari'; } 60 if (agt.indexOf("msie") != -1) { return 'IE'; } 61 if (agt.indexOf("chrome") != -1) { return 'Chrome'; } 62 return "Unknown"; 63 }, 64 65 osFromUserAgent: function(agt) { 66 agt = agt.toLowerCase(); 67 if (agt.indexOf("win") != -1) { return 'Windows'; } 68 if (agt.indexOf("mac") != -1) { return 'Mac'; } 69 if (agt.indexOf("linux") != -1) { return 'Linux'; } 70 return "Unknown"; 71 }, 72 73 baseUrl: function() { 74 return document.location.protocol + "//" + document.location.host; 75 }, 76 77 urlWithoutHash: function() { 78 return this.baseUrl() + document.location.pathname; 79 } 80 };