|
| 1 | +// Vex Tools for JavaScript |
| 2 | +// Mohit Muthanna <[email protected]> |
| 3 | +// |
| 4 | +// Tools, utility functions, and other shared JS cruft. |
| 5 | +// |
| 6 | +// Requires: jQuery |
| 7 | + |
| 8 | +function Vex() {} |
| 9 | + |
| 10 | +/* |
| 11 | + function Vex.IsIE() |
| 12 | +
|
| 13 | + Returns true if we're running in Internet Explorer. |
| 14 | +*/ |
| 15 | +Vex.IsIE = function() { |
| 16 | + return /msie/i.test( |
| 17 | + navigator.userAgent) && !/opera/i.test(navigator.userAgent); |
| 18 | +} |
| 19 | + |
| 20 | +/* |
| 21 | + function Vex.Merge(dest, source) |
| 22 | +
|
| 23 | + Merge the associative arrays (or objects) in dest and source. Modifies "dest" |
| 24 | + and returns its value. |
| 25 | +*/ |
| 26 | +Vex.Merge = function(destination, source) { |
| 27 | + for (var property in source) |
| 28 | + destination[property] = source[property]; |
| 29 | + return destination; |
| 30 | +}; |
| 31 | + |
| 32 | + |
| 33 | +/* |
| 34 | + function Vex.Ajax(url, data, handler); |
| 35 | +
|
| 36 | + Call AJAX method in "url" using POST and data provided in "data". Upon |
| 37 | + success or error, calls: |
| 38 | +
|
| 39 | + handler(success, data, textstatus, xmlhttprequest, error) |
| 40 | +
|
| 41 | + where: |
| 42 | +
|
| 43 | + success: bool (false if AJAX or API error) |
| 44 | + data: result |
| 45 | + textstatus: status code |
| 46 | + (One of: "timeout", "error", "notmodified", "parseerror", "apierror") |
| 47 | + xmlhttprequest: the request object |
| 48 | + error: an optional exception |
| 49 | +
|
| 50 | + The AJAX method should return one of the following: |
| 51 | +
|
| 52 | + 1. Upon success: { success: true, data: {...} } |
| 53 | + 2. Upon error: { success: false, message: "string" } |
| 54 | +
|
| 55 | + Vex.error_sel (if set) is populated with the error message upon error. |
| 56 | +*/ |
| 57 | +Vex.error_sel = "#error-message"; |
| 58 | + |
| 59 | +Vex.Ajax = function(url, data, handler) { |
| 60 | + $.ajax({ |
| 61 | + type: "POST", |
| 62 | + 'url': url, |
| 63 | + 'data': data, |
| 64 | + 'dataType': 'json', |
| 65 | + success: function(d, t, x) { |
| 66 | + if (!d) { |
| 67 | + log("Server Error: " + t + " in " + x); |
| 68 | + $(Vex.error_sel).text( |
| 69 | + "Unexpected server error. Please try again later."); |
| 70 | + $(Vex.error_sel).fadeIn(500).delay(3000).fadeOut(1000); |
| 71 | + handler(false, null, "servererror", x, null); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (!d.success) { |
| 76 | + log("API Error: " + d.message); |
| 77 | + $(Vex.error_sel).text("(API Error) " + d.message); |
| 78 | + $(Vex.error_sel).fadeIn(500).delay(3000).fadeOut(1000); |
| 79 | + handler(false, d, "apierror", x, null); |
| 80 | + } else { |
| 81 | + handler(true, d, t, x, null); |
| 82 | + } |
| 83 | + }, |
| 84 | + error: function(x, t, e) { |
| 85 | + log("Server Error: " + t); |
| 86 | + $(Vex.error_sel).text("(Server Error) " + t); |
| 87 | + $(Vex.error_sel).fadeIn(500).delay(3000).fadeOut(1000); |
| 88 | + handler(false, null, t, x, e); |
| 89 | + } |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +/* |
| 94 | + Vex.InstallTracker - Install an asynchronous Google Analytics Tracker |
| 95 | + with the property ID supplied in "property_id". |
| 96 | +
|
| 97 | + See http://google.com/analytics for details. |
| 98 | +*/ |
| 99 | +Vex.InstallTracker = function(property_id) { |
| 100 | + var _gaq = _gaq || []; |
| 101 | + _gaq.push(['_setAccount', property_id]); |
| 102 | + _gaq.push(['_trackPageview']); |
| 103 | + |
| 104 | + (function() { |
| 105 | + var ga = document.createElement('script'); |
| 106 | + ga.type = 'text/javascript'; ga.async = true; |
| 107 | + ga.src = ('https:' == document.location.protocol ? |
| 108 | + 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
| 109 | + var s = document.getElementsByTagName('script')[0]; |
| 110 | + s.parentNode.insertBefore(ga, s); |
| 111 | + })(); |
| 112 | +} |
| 113 | + |
| 114 | +function VexUI() {} |
| 115 | + |
| 116 | +VexUI.Pulsate = function(sel) { |
| 117 | + var old_background = $(sel).css('background'); |
| 118 | + var old_color = $(sel).css('color'); |
| 119 | + |
| 120 | + $(sel).css('background', 'red'); |
| 121 | + $(sel).css('color', 'yellow'); |
| 122 | + |
| 123 | + $(sel).effect("pulsate", {}, 50, function() { |
| 124 | + $(sel).css('background', old_background); |
| 125 | + $(sel).css('color', old_color); |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +VexUI.MessageBox = function(sel) { |
| 130 | + this.sel = sel |
| 131 | + this.sel_content = sel + " .content" |
| 132 | + this.sel_icon = sel + " .ui-icon" |
| 133 | +} |
| 134 | + |
| 135 | +VexUI.MessageBox.prototype.Initialize = function() { |
| 136 | + $(this.sel).dialog({ |
| 137 | + bgiframe: true, |
| 138 | + modal: true, |
| 139 | + autoOpen: false, |
| 140 | + buttons: { |
| 141 | + Ok: function() { $(this).dialog('close') } |
| 142 | + } |
| 143 | + }); |
| 144 | +} |
| 145 | + |
| 146 | +VexUI.MessageBox.prototype.Open = function(title, message, mode) { |
| 147 | + var boxclass = "" |
| 148 | + var icon = "alert" |
| 149 | + var state = "" |
| 150 | + |
| 151 | + if (mode == "alert") { |
| 152 | + boxclass = "alert" |
| 153 | + icon = "alert" |
| 154 | + state = "error" |
| 155 | + } |
| 156 | + |
| 157 | + $(this.sel).dialog("option", "title", title); |
| 158 | + $(this.sel).dialog("option", "dialogClass", boxclass); |
| 159 | + $(this.sel_content).empty(); |
| 160 | + $(this.sel_content).text(message); |
| 161 | + |
| 162 | + $(this.sel_icon).removeClass(["ui-icon-alert"]) |
| 163 | + $(this.sel_icon).addClass("ui-icon-" + icon); |
| 164 | + $(this.sel).dialog("open"); |
| 165 | +} |
| 166 | + |
| 167 | +VexUI.MessageBox.prototype.Close = function() { |
| 168 | + $(this.sel).dialog("close"); |
| 169 | +} |
| 170 | + |
| 171 | +VexUI.MessageBox.prototype.Destroy = function() { |
| 172 | + $(this.sel).dialog("destroy"); |
| 173 | +} |
| 174 | + |
| 175 | +// TODO(0xfe): Move these to Vex namespace. |
| 176 | + |
| 177 | +function log(message) { |
| 178 | + if (window.console) { |
| 179 | + console.log(message); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +function html_escape(message) { |
| 184 | + return $('<div/>').text(message).html(); |
| 185 | +} |
| 186 | + |
0 commit comments