Created
March 8, 2011 19:00
-
-
Save mythz/860783 to your computer and use it in GitHub Desktop.
javascript utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fromDtoDate = function(dateStr) { | |
if (!dateStr) return null; | |
return new Date(parseFloat(/Date\(([^)]+)\)/.exec(dateStr)[1])); | |
} | |
var formatDtoDate = function(dateObj) { | |
if (!dateObj) return ""; | |
if (dateObj != typeof "date") dateObj = fromDtoDate(dateObj); | |
var day = dateObj.getDate(), month = dateObj.getMonth() + 1; | |
if (day < 10) day = "0" + day; | |
if (month < 10) month = "0" + month; | |
return day + "/" + month + "/" + dateObj.getFullYear(); | |
} | |
var evalToString = function(value) { | |
if (!value) return ''; | |
if (typeof value == 'function') | |
{ | |
return value.apply(this, Array.prototype.slice.call(arguments, 1)); | |
} | |
return value; | |
} | |
/* goog.string.StringBuffer */ | |
var hasScriptEngine = 'ScriptEngine' in window; | |
var HAS_JSCRIPT = hasScriptEngine && window['ScriptEngine']() == 'JScript'; | |
var IS_IE = HAS_JSCRIPT; | |
var StringBuffer = function(opt_a1, var_args) { | |
this.buffer_ = HAS_JSCRIPT ? [] : ''; | |
if (opt_a1 != null) { | |
this.append.apply(this, arguments); | |
} | |
}; | |
var p = StringBuffer.prototype; | |
p.set = function(s) { | |
this.clear(); | |
this.append(s); | |
}; | |
if (HAS_JSCRIPT) { | |
p.bufferLength_ = 0; | |
p.append = function(a1, opt_a2, var_args) { | |
if (opt_a2 == null) { | |
this.buffer_[this.bufferLength_++] = a1; | |
} else { | |
this.buffer_.push.apply((this.buffer_), arguments); | |
this.bufferLength_ = this.buffer_.length; | |
} | |
return this; | |
}; | |
p.isEmpty = function() { | |
return this.bufferLength_ == 0; | |
} | |
} else { | |
p.append = function(a1, opt_a2, var_args) { | |
this.buffer_ += a1; | |
if (opt_a2 != null) { | |
for (var i = 1; i < arguments.length; i++) { | |
this.buffer_ += arguments[i]; | |
} | |
} | |
return this; | |
}; | |
p.isEmpty = function() { | |
return !this.buffer_; | |
} | |
} | |
p.clear = function() { | |
if (HAS_JSCRIPT) { | |
this.buffer_.length = 0; | |
this.bufferLength_ = 0; | |
} else { | |
this.buffer_ = ''; | |
} | |
}; | |
p.getLength = function() { | |
return this.toString().length; | |
}; | |
p.toString = function() { | |
if (HAS_JSCRIPT) { | |
var str = this.buffer_.join(''); | |
this.clear(); | |
if (str) { | |
this.append(str); | |
} | |
return str; | |
} else { | |
return (this.buffer_); | |
} | |
}; | |
var createTableWithCaption = function(id, caption, tableStyle) { | |
var sb = new StringBuffer("<div class='sd-info'><table id='", id, "' class='itfa_sectionbody' style='", tableStyle, "'>"); | |
if (caption) sb.append("<caption>", caption, "</caption>"); | |
return sb.toString(); | |
} | |
var createTableFn = function(id, fieldNames, caption, tableStyle, tfoot) { | |
var sb = new StringBuffer(createTableWithCaption(id, caption, tableStyle)); | |
sb.append("<thead><tr>"); | |
for (var i = 0, len = fieldNames.length; i < len; i++) { | |
sb.append('<th class="c', (i + 1), '">', evalToString(fieldNames[i]), '</th>'); | |
} | |
sb.append("</tr></thead><tfoot>"); | |
if (tfoot) sb.append(evalToString(tfoot)); | |
return sb.append("</tfoot><tbody>"); | |
} | |
var createRowsFn = function(rows, fieldNames, filters) { | |
var sb = new StringBuffer(); | |
for (var i = 0; i < rows.length; i++) { | |
var row = rows[i]; | |
var className = i % 2 == 0 ? "r1" : "r2"; | |
sb.append("<tr class='", className, "' data-id='", row["Id"], "'>"); | |
for (var j = 0; j < fieldNames.length; j++) { | |
var fieldName = fieldNames[j]; | |
var fieldValue = row[fieldName]; | |
if (filters && typeof (filters[fieldName]) == 'function') { | |
fieldValue = filters[fieldName](fieldValue, row); | |
} | |
if (typeof fieldValue == "undefined") { | |
fieldValue = ""; | |
} | |
sb.append('<td class="c', (j + 1), '">', fieldValue, '</td>'); | |
} | |
sb.append("</tr>"); | |
} | |
return sb; | |
} | |
var createResultsFn = function(options) { | |
var fieldNames = [], fieldLabels = [], fields = options.fieldLabels, id = options.id || "wsResults"; | |
for (var fieldName in fields) { | |
fieldNames.push(fieldName); | |
fieldLabels.push(fields[fieldName]); | |
} | |
var tableStyle = options.show ? '' : 'display:none'; | |
var footer = options.footer || ""; | |
if ((!options.data || options.data.length == 0)) { | |
var tableHtml = options.showHeadersIfEmpty | |
? new StringBuilder(createTableFn(id, fieldLabels, options.caption, tableStyle, options.tfoot), | |
'<tr class="empty"><td colspan="', fieldNames.length, '">', options.emptyCaption, '</td></tr>') | |
: new StringBuilder(createTableWithCaption(id, options.emptyCaption, tableStyle)); | |
tableHtml.append("</table>", footer, "</div>"); | |
tableHtml.toString(); | |
} | |
var sb = new StringBuffer( | |
createTableFn(id, fieldLabels, options.caption, tableStyle, options.tfoot), | |
createRowsFn(options.data, fieldNames, options.filters), | |
"</tbody></table>", footer, "</div>"); | |
return sb.toString(); | |
} | |
var setError = function(errorMsg) { | |
//$("#status").html('<span class="error">' + errorMsg + '</span>'); | |
alert("Error calling web service:\n" + errorMsg); | |
} | |
var handleError = function(xhr, statusText, errorThrown) { | |
hideLoading(); | |
var dto = JSON.parse(xhr.responseText); | |
var status = dto.ResponseStatus; | |
setError(status.ErrorCode + ": " + status.Message); | |
} | |
function hideLoading() | |
{ | |
$("#loading").hide(); | |
$("#underlay").hide(); | |
} | |
var getJson = function(url, successFn, errorFn) { | |
$("#status").html(""); | |
var ieDontCache = { ts: new Date().getTime() }; | |
$.ajax({ | |
url: url, | |
contentType: 'application/json', | |
dataType: 'json', | |
data: ieDontCache, | |
success: successFn, | |
error: errorFn || handleError | |
}); | |
} | |
var urlParams = function(name) { | |
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href); | |
if (!results) { return 0; } | |
return results[1] || 0; | |
} | |
var keys = function(o){ var a=[]; for (var k in o) a.push(k); return a; } | |
function clearSel() { | |
if (document.selection && document.selection.empty) document.selection.empty(); | |
else if(window.getSelection) { | |
var sel=window.getSelection(); | |
if (sel && sel.removeAllRanges) sel.removeAllRanges(); | |
} | |
} | |
function setTableBody(tbody, html) { | |
if (!$.browser.msie) { tbody.innerHTML = html; return; } | |
var temp = tbody.ownerDocument.createElement('div'); | |
temp.innerHTML = '<table>' + html + '</table>'; | |
tbody.parentNode.replaceChild(temp.firstChild.firstChild, tbody); | |
} | |
function cmp(v1, v2){ | |
var f1, f2, f1=parseFloat(v1), f2=parseFloat(v2); | |
if (!isNaN(f1) && !isNaN(f2)) v1=f1, v2=f2; | |
if (typeof v1 == 'string' && v1.substr(0,6) == '/Date(') v1=date(v1), v2=date(v2); | |
if (v1 == v2) return 0; | |
return v1 > v2 ? 1 : -1; | |
} | |
if ("undefined" === typeof window.console) window.console = { "log": function(){} }; | |
function cancelEvent(e) | |
{ | |
e = e ? e : window.event; | |
if(e.stopPropagation) | |
e.stopPropagation(); | |
if(e.preventDefault) | |
e.preventDefault(); | |
e.cancelBubble = true; | |
e.cancel = true; | |
e.returnValue = false; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment