forked from breck7/note
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote.min.js
20 lines (20 loc) · 7.71 KB
/
note.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function Note(a){if("string"===typeof a)return this.load_from_string(a);for(var b in a)a.hasOwnProperty(b)&&(this[b]="object"===typeof a[b]?new Note(a[b]):a[b])}Note.copyright="2012 Nudge Inc.";Note.authors="Breck Yunits <[email protected]>, Ben Zulauf <[email protected]>";Note.homepage="github.com/breck7/note";Note.license="license www.opensource.org/licenses/MIT";Note.escape_html=function(a){return String(a).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")};
Note.prototype.bytes=function(){return this.to_string().length};Note.prototype.clear=function(){for(var a in this.names())delete this[a];this.trigger("change");return this};Note.prototype.clone=function(){return new Note(this)};
Note.prototype.diff=function(a){var b=new Note;a instanceof Note||(a=new Note(a));for(var c in this.names())if(c in a)if(typeof this[c]!=typeof a[c])"object"===typeof a[c]?b[c]=new Note(a[c]):this[c]!=a[c]&&(b[c]=a[c]);else if("object"!=typeof this[c])this[c]!=a[c]&&(b[c]=a[c]);else{var d=this[c].diff(a[c]);d.length()&&(b[c]=d)}else b[c]="";for(c in a.names())if(!this.is_public_property(c)&&(!(a instanceof Note)||a.is_public_property(c)))b[c]="object"!=typeof a[c]?a[c]:a[c]instanceof Note?new Note(a[c].to_string()):
new Note(a);return b};Note.prototype.each=function(a){for(var b in this.names())this[b]instanceof Note&&a(this[b],b);return this};Note.prototype.equals=function(a){return this.to_string()===a.to_string()};Note.prototype.empty=function(){for(var a in this)if(this.is_public_property(a))return!1;return!0};Note.prototype.filter=function(a){var b=this.clone(),c;for(c in b.names())a(b[c])||delete b[c];return b};
Note.prototype.first=function(){var a=new Note,b=this.clone(),c;for(c in b.names())return a[c]=b[c],a};Note.prototype.first_name=function(){for(var a in this.names())return a};Note.prototype.get=function(a,b){if(!a)return!1;b||(b=this);var c=a.split(/ /g),d=c.shift();return!(d in b)?!1:!c.length?b[d]:this.get(c.join(" "),b[d])};Note.prototype.get_float=function(a){a=parseFloat(this.get(a));return isNaN(a)?0:a};
Note.prototype.is_private_property=function(a){for(var b in this.privates)if(this.privates[b]===a)return!0;return!1};Note.prototype.is_public_property=function(a){return!this.hasOwnProperty(a)?!1:!this.is_private_property(a)};Note.prototype.last=function(){var a=new Note,b=this.clone(),c;for(c in b.names());a[c]=b[c];return a};Note.prototype.last_name=function(){for(var a in this.names());return a};
Note.prototype.leaves=function(){var a=0,b;for(b in this)this.is_public_property(b)&&(this[b]instanceof Note?a+=this[b].leafs():a++);return a};Note.prototype.length=function(){var a=0,b;for(b in this)this.is_public_property(b)&&a++;return a};
Note.prototype.load_from_string=function(a){var a=a.replace(/^[\n ]*/,""),a=a.replace(/\n\r/g,"\n").replace(/\r\n/g,"\n"),a=a.replace(/[\n ]*$/,""),a=a.replace(/\n\n+/,"\n"),a=a.split(/\n(?! )/g),b;for(b in a){var c=a[b];if(matches=c.match(/^([^ ]+)(\n|$)/))this[matches[1]]=new Note(c.substr(matches[1].length).replace(/\n /g,"\n"));else if(matches=c.match(/^([^ ]+) /))this[matches[1]]=c.substr(matches[1].length+1).replace(/^\n /,"").replace(/\n /g,"\n")}return this};
Note.prototype.map=function(a){var b=this.clone();b.each(a);return b};Note.prototype.names=function(){var a={},b;for(b in this)this.is_public_property(b)&&(a[b]=b);return a};Note.prototype.next=function(a){var b=this.sorted_array(),a=b.indexOf(a)+1;return b[a]?b[a]:b[0]};
Note.prototype.note_to_html=function(a,b){b||(b=0);var c="",d;for(d in a)if(a.hasOwnProperty(d)&&!this.is_private_property(d)){var e=a[d];"undefined"===typeof e&&(e="");c+=this.str_repeat(" ",b)+d;c="object"===typeof e?c+("\n"+this.note_to_html(e,b+1)):c+(' <span style="color: #444444;">'+Note.escape_html(e.toString().replace(/\n/g,"\n"+this.str_repeat(" ",b+d.length+1)))+"</span>\n")}return c};
Note.prototype.notes=function(){var a=0,b;for(b in this)this.is_public_property(b)&&this[b]instanceof Note&&(a++,a+=this[b].notes());return a};Note.prototype.off=function(a,b){this._events&&(this._events[a]&&this._events[a].indexOf(b))&&this._events[a].splice(1,this._events[a].indexOf(b));return this};Note.prototype.on=function(a,b){this._events||(this._events={});this._events[a]=[];this._events[a].push(b);return this};
Note.prototype.patch=function(a){"string"===typeof a&&(a=new Note(a));for(var b in a)a.hasOwnProperty(b)&&!this.is_private_property(b)&&("string"===typeof a[b]?""===a[b]?delete this[b]:this[b]=a[b]:"number"===typeof a[b]?this[b]=a[b]:a[b]instanceof Note&&a[b].empty()?delete this[b]:this[b]=this[b]instanceof Note?this[b].patch(a[b]):new Note(a[b]));this.trigger("change",a);return this};
Note.prototype.pluck=function(a){var b;if(!this[a])return"";b=this[a]instanceof Note?this[a].clone():this[a];delete this[a];return b};Note.prototype.pop=function(){var a,b;for(b in this.names());a=this[b]instanceof Note?this[b].clone():this[b];delete this[b];return a};Note.prototype.prev=function(a){var b=this.sorted_array(),a=b.indexOf(a)-1;return 0<=a?b[a]:b[b.length-1]};Note.prototype.privates=["_events"];
Note.prototype.rest=function(){var a=this.clone(),b;for(b in this.names())return delete a[b],a};Note.prototype.retrieve=function(a){var b=new Note,c;for(c in a.names())this[c]&&(!(a[c]instanceof Note)||!a[c].length()?b[c]=this[c]:this[c]instanceof Note&&(b[c]=this[c].retrieve(a[c])));return b};Note.prototype.set=function(a,b){for(var c=a.split(/ /g),d=new Note,e=0;e<c.length-1;e++)d[c[e]]=new Note;d[c[e]]=b;this.patch(d);this.trigger("change");return this};
Note.prototype.shift=function(){var a,b;for(b in this.names())return a=this[b]instanceof Note?this[b].clone():this[b],delete this[b],a};Note.prototype.sort_by_name=function(a){var b=[],c;for(c in this.names())b.push(c);b.sort();a&&b.reverse();var a=new Note,d;for(d in b)a[b[d]]=this[b[d]];return a};
Note.prototype.sort_by_property=function(a,b){var c=[],d;for(d in this.names())c.push([d,this[d][a]]);c=c.sort(function(a,c){return b?c[1]-a[1]:a[1]-c[1]});d=new Note;for(var e in c)d[c[e][0]]=this[c[e][0]];return d};Note.prototype.sorted_array=function(){var a=[],b;for(b in this.names())this.is_public_property(b)&&a.push(b);return a=a.sort()};Note.prototype.str_repeat=function(a,b){for(var c="",d=0;d<b;d++)c+=" ";return c};Note.prototype.stringify=function(){return this.to_javascript()};
Note.prototype.to_html=function(){return'<pre style="color: #888888;">'+this.note_to_html(this)+"</pre>"};Note.prototype.to_javascript=function(){return"new Note('"+this.to_string().replace(/\n/g,"\\n").replace(/\'/g,"\\'")+"')"};Note.prototype.to_json=function(){return JSON.stringify(this)};
Note.prototype.to_string=function(a){var a=a||0,b="",c;for(c in this)this.is_public_property(c)&&("undefined"===typeof this[c]?b+="\n":(b+=this.str_repeat(" ",a)+c,b=this[c]instanceof Note?b+("\n"+this[c].to_string(a+1)):"object"===typeof this[c]?b+("\n"+(new Note(this[c])).to_string(a+1)):""===this[c].toString()?b+"\n":this[c].toString().match(/\n/)?b+(" \n"+this.str_repeat(" ",a+1)+this[c].toString().replace(/\n/g,"\n"+this.str_repeat(" ",a+1))+"\n"):b+(" "+this[c].toString()+"\n")));return b};
Note.prototype.toObject=function(){var a={},b;for(b in this)this.is_public_property(b)&&(a[b]=this[b]instanceof Note?this[b].toObject():this[b]);return a};Note.prototype.toString=function(){return this.to_string()};Note.prototype.trigger=function(a,b){if(!this._events||!this._events[a])return"";for(var c in this._events[a])this._events[a][c].call(this,b)};
Note.prototype.union=function(a){var b=this.union_single(arguments[0]),c;for(c in arguments)if(0!==c&&(b=b.union_single(arguments[c]),!b.length()))break;return b};Note.prototype.union_single=function(a){var b=new Note,c;for(c in this.names())this[c]instanceof Note&&(b[c]=this[c].union(a[c])),this[c]===a[c]&&(b[c]=this[c]);return b};"undefined"!=typeof exports&&(module.exports=Note);