Skip to content

Instantly share code, notes, and snippets.

@ecounysis
Last active December 18, 2015 22:09
Show Gist options
  • Save ecounysis/5852336 to your computer and use it in GitHub Desktop.
Save ecounysis/5852336 to your computer and use it in GitHub Desktop.
// depends on jQuery and handlebars
// http://jquery.com/
// http://handlebarsjs.com/
// less is always nice too
// http://lesscss.org/
// as is jqueryui
// http://jqueryui.com/
String.prototype.is_number = function (x) {
if (x === undefined)
x = this;
return !isNaN(x); // if not NaN, then it's a number!
};
String.prototype.number = function (obj) {
if (obj === undefined) obj = this;
return obj * 1 + 0;
};
Array.prototype.sum = function (obj) {
if (obj === undefined) {
obj = this;
}
var s = 0;
for (var i = 0; i < obj.length; i++) {
s += obj[i];
}
return s;
};
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function (from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
Array.prototype.where = Array.prototype.filter;
Array.prototype.any = function (fn, obj) {
if (obj === undefined) obj = this;
// returns true if fn evaluates to true for any element of the array
var i = 0;
for (; i < obj.length; i++) {
if (fn(obj[i])) {
return true;
}
}
return false;
};
Array.prototype.filter_if = function (fn, obj) {
if (obj === undefined) obj = this;
// removes values from array where fn evaluates to non-false
var val = [],
i = 0;
for (; i < obj.length; i++) {
if (!fn(obj[i]))
val[val.length] = obj[i];
}
return val;
};
Array.prototype.filter_if_not = function (fn, obj) {
if (obj === undefined) obj = this;
// removes values from array where fn evaluates to false
var val = [],t
i = 0;
for (; i < obj.length; i++) {
if (fn(obj[i]))
val[val.length] = obj[i];
}
return val;
};
toolbox = {};
toolbox.app_id = function () {
return "app_" + Math.floor(Math.random() * 99999999) + "";
};
toolbox.add_popover_box = function (context, template_id, classname, appendto) {
if(appendto===undefined) appendto="body";
var html = StdApp.template_html(context, template_id);
var newid = StdApp.app_id();
var sel = "#" + newid;
$(appendto).append("<div id=\"" + newid + "\" class=\"" + classname + "\"></div>");
$(sel)
.dblclick(function () { $(sel).remove(); })
.html(html);
$(sel).prepend("<span class=\"link-button\" onclick=\"$('" + sel + "').remove();\">Close</span>");
return $(sel);
};
toolbox.template_html = function (context, template_div) {
var source = $("#" + template_div).html();
var temp = Handlebars.compile(source);
return temp(context);
};
Handlebars.registerHelper('data_grid', function () {
var st = "<table><thead><tr>";
for (var col = 0; col < this.columns.length; col++) {
st += "<td>" + this.columns[col] + "</td>";
}
st += "</tr></thead><tbody>";
for (var row = 0; row < this.data.length; row++) {
st += "<tr>";
for (var col = 0; col < this.columns.length; col++) {
st += "<td>" + this.data[row][this.columns[col]] + "</td>";
}
st += "</tr>";
}
st += "</tbody></table>";
return new Handlebars.SafeString(st);
});
Handlebars.registerHelper('data_table', function () {
var st = "<table>";
for (var att_num = 0; att_num < this.attributes.length; att_num++) {
st += "<tr><td>";
st += this.attributes[att_num];
st += "</td><td>";
st += this.item[this.attributes[att_num]];
st += "</td></tr>";
}
st += "</table>";
return new Handlebars.SafeString(st);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment