-
Notifications
You must be signed in to change notification settings - Fork 0
/
m.js
18 lines (18 loc) · 6.82 KB
/
m.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var d=document;function g(a){return d.getElementById(a)}function lg(a){return localStorage.getItem(a)}function lr(a){localStorage.removeItem(a)}function ls(a,b){localStorage.setItem(a,b)}var tagMap=null;
function Doc(a){this.tags=this.id=null;this.content="";this.rmt={};this.tc=0;if(a){a=JSON.parse(lg(a));this.id=a.id;this.tags=a.tags;this.content=a.content}else{var b=new Date;this.id="doc-"+b.getTime()+"-"+Math.floor(Math.random()*1E3);this.tags={};this.content="";a=b.getFullYear();b=b.getMonth()+1;this.add(a+"-"+(b<10?"0":"")+b)}}Doc.prototype.del=function(){for(tag in this.tags)this.rmt[tag]=null;this.hdlr();lr(this.id)};
Doc.prototype.add=function(a){if(a.length<1)return false;if(a in this.tags)return false;else{this.tags[a]=null;this.tc++;return true}};Doc.prototype.rm=function(a){if(this.tc==1)return false;delete this.tags[a];this.tc--;this.rmt[a]=null;return true};
Doc.prototype.save=function(){if(this.tc==0)return false;var a=this.tc;delete this.tc;this.hdlr();delete this.rmt;ls(this.id,JSON.stringify(this));for(var b in this.tags){var c=tagMap[b];if(c==null)c={};this.id in c||(c[this.id]=null);tagMap[b]=c}svM();this.rmt={};this.tc=a;return true};Doc.prototype.hdlr=function(){for(tag in this.rmt)try{delete tagMap[tag][this.id]}catch(a){}svM()};function ldM(){var a=lg("map");tagMap=a==null?{}:JSON.parse(a)}function svM(){ls("map",JSON.stringify(tagMap))}
var msgTimer=null;function msg(a,b){g("msg").innerHTML=a;g("msg").style.display="block";clearTimeout(msgTimer);b>0&&setTimeout("rmsg()",b*1E3)}function rmsg(){clearTimeout(msgTimer);g("msg").style.display="none"}function rme(a){for(;a.childNodes.length>0;)a.removeChild(a.firstChild)}
function tagl(a,b,c){var e=d.createElement("a");e.className="tag";e.innerHTML=b;b=d.createAttribute("href");b.nodeValue="#";e.setAttributeNode(b);b=d.createAttribute("onclick");b.nodeValue=c+"(event)";e.setAttributeNode(b);a.appendChild(e)}function noprp(a){a.cancelBubble=true;a.returnValue=false;if(a.stopPropagation){a.stopPropagation();a.preventDefault()}}
function init(){if(window.localStorage){d. to minipad !",5)}else msg("Sorry, your browser is not supported !",0)}
function help(){if(lg("help")==null){newDoc();doc.content="Here's a bit of help...\n\nType your notes in this big, blank, gorgeous space. When you're done, hit ctrl-s.\n\nThere are no file names; instead, minipad uses tags to categorize your notes. A tag for the current month is added to new docs.\nAdding tags is simple: type in the input box just above this (ctrl-shift-t to go there), and hit enter (or the 'tab' key to go back to editing). You can add several tags at a time (space separated).\nTags are auto-completed!\n\nClick on a tag to remove it (there must be at least one, so you can't remove the last). Don't forget to save :p\n\nTo open an existing document, hit ctrl-o (or click Find). Select tags to see matching docs.\nEnjoy !";doc.add("help");
doc.save();ls("help",doc.id);reloadUI()}else loadDoc(lg("help"))}function onk(a){a=a||window.event;if(!a.ctrlKey)return true;if(a.keyCode)code=a.keyCode;else if(a.which)code=a.which;switch(code){case 79:searchMode();break;case 83:onSave();break;case 84:if(a.shiftKey){g("tage").focus();break}default:return true}noprp(a);return false}var mode=null,modes={write:0,search:0,"import":0};
function mod(a){if(mode=="write"&&dirty){onSave();if(dirty)return false}if(a==mode)return true;mode=a;for(var b in modes)g(b+"M").className=b==mode?"":"iv";return true}var doc=null,dirty=0;function dirt(a){if(dirty!=a){dirty=a;a.title=dirty?"minipad (modified)":"minipad"}}function newDoc(){if(mod("write")){doc=new Doc;reloadUI();msg("New document ready !",5)}}function loadDoc(a){if(mod("write")){doc=new Doc(a);reloadUI();msg("Document loaded !",5)}}
function reloadUI(){var a=g("tags");rme(a);for(tag in doc.tags)tagl(a,tag,"rmtag");g("data").value=doc.content;dirt(0);g("data").focus()}function addt(a){for(var b=a.value.split(/\s+/),c=0;c<b.length;c++)doc.add(b[c])&&tagl(g("tags"),b[c],"rmtag");a.value=""}function onSave(){doc.content=g("data").value;if(doc.save()){msg("Saved !",3);dirt(0)}else msg("Sorry, could not save !",15)}function rmtag(a){a=a.srcElement||a.target;doc.rm(a.innerHTML)&&a.parentNode.removeChild(a)}var ctimer=null;
function cmplup(a,b){switch(b.keyCode){case 8:case 16:case 17:case 18:case 27:case 33:case 34:case 35:case 36:case 37:case 38:case 39:case 40:case 45:case 46:return true;case 13:g("data").focus();default:}ctimer!=null&&clearTimeout(ctimer);ctimer=setTimeout("cmpldo()",280)}
function cmpldo(){var a=g("tage"),b=a.value.split(/\s+/);b=b[b.length-1];if(!(b.length<1))for(tag in tagMap)if(tag.indexOf(b)==0){b=tag.substring(b.length);if(b.length==0)return;a.value+=b;if(a.createTextRange){a=a.createTextRange();a.findText(b);a.select()}else a.setSelectionRange(a.value.length-b.length,a.value.length);return}}var searchTags={};
function searchMode(){if(mod("search")){var a=g("tls");rme(a);var b=0;for(tag in tagMap){tagl(a,tag,"selt");b++}g("tagcnt").innerHTML=b;for(i=b=0;i<=localStorage.length-1;i++){key=localStorage.key(i);key.indexOf("doc-")==0&&b++}g("doccnt").innerHTML=b;rme(g("dls"))}}function selt(a){a=a.srcElement||a.target;if(a.tagSelected){a.className="tag";a.tagSelected=0;delete searchTags[a.innerHTML]}else{a.className="tag stag";a.tagSelected=1;searchTags[a.innerHTML]=null}search()}
function del(a,b){if(a==lg("help"))msg("Deleting help: don't want !");else if(confirm("Really ?")){(new Doc(a)).del();b=d.getElementById(a);b.parentNode.removeChild(b);msg("Deleted",5)}}
function search(){var a=null;ldM();for(wantedTag in searchTags){var b=tagMap[wantedTag];if(a!=null){var c={};for(doc in a)doc in b||(c[doc]=null);for(doc in c)delete a[doc]}else a=b}b=[];b.p=b.push;c=0;for(docid in a){c++;var e=new Doc(docid);b.p('<div id="');b.p(e.id);b.p('" class="mdoc">');var f=0;for(tag in e.tags){b.p('<b class="mtag">');b.p(tag);b.p("</b>");if(++f>4){b.p('<b class="mtag">(...)</b>');break}}b.p("<a >
64));e.content.length>64&&b.p("...");b.p('»</a><input type="button" value="Delete" importMode(){mod("import")}function expt(){var a={};for(i=0;i<=localStorage.length-1;i++){key=localStorage.key(i);a[key]=lg(key)}g("xpa").value=JSON.stringify(a);g("xpa").select();msg("Export ready !",5)}
function impt(){if(confirm("This will replace all your documents and tags. Sure ?"))try{var a=JSON.parse(g("ipa").value);localStorage.clear();for(k in a)ls(k,a[k]);ldM();msg("Import successful !",5)}catch(b){msg("Import failed :(",10)}}function clearAll(){if(confirm("This will delete all your documents and tags. Sure ?")){localStorage.clear();ldM();msg("All data removed !",0)}};