Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- saved from url=(0065)http://192.168.1.252:8080/websites/_test/memory/localstorage.html -->
- <html><head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=8, user-scalable=yes" />
- <script src="../../_common/common.js"></script>
- <style>
- /* main */
- * { box-sizing: border-box; }
- html { display:block; width:100%; height:100%; }
- /* layout */
- #main{}
- #main td{vertical-align:top;}
- #leftSide,#rightSide { width:45%; outline:1px solid #222; }
- #leftSide *,#rightSide * { margin-left:auto; margin-right:auto }
- /* left */
- #form1, #form2 { width:95%; }
- /* right */
- #output{width:95%; font-size:0.8em; font-family:courier; padding-left:12px; background:#EEE;}
- /* other */
- label{width:80px; display:inline-block;}
- input[type="textbox"]{ width:280px; }
- .del{color:#F00; font-size:1.3em; text-decoration:none;}
- #mess1,#mess2{color:#444;}
- .showElement { display:block!important }
- /* table selection button + menu */
- #tableselect { position:relative; display:inline-block }
- #tableselectbutton {
- background:#39d; color:#fff;
- font-size:16px; border:none; cursor:pointer }
- #tableselectbutton:hover,#tableselectbutton:focus { color:#fff;
- background:#28b; box-shadow:1px 8px 10px 2px rgba(0,0,0,0.7) }
- #tableselected { ; }
- #tableselectitems {
- display:none; position:absolute; background:#eee;
- min-width:50px; box-shadow:0px 0px 16px 3px rgba(0,0,0,0.7); z-index:10000 }
- #tableselectitems a {
- color:#000; padding:12px 16px; text-decoration:none;
- display:block; border:0 solid #444; border-width:1 1 1 1 }
- #tableselectitems a:hover { background:#ddd!important }
- /* tables */
- #list1{width:100%}
- #grid{width:95%; border:1px solid #CCC; border-collapse:collapse; font-size:0.8em;}
- #grid caption {font-weight:800; background:#ddd}
- #grid th{background:#EEE;}
- #grid td{padding:1px 6px; vertical-align:top;}
- </style>
- <script>
- /*---- STORAGE MANAGER ----*/
- var datamanager={
- get : function(table,id){
- if(!table || !id){return null;}
- return window.localStorage.getItem(table+"["+id.toString()+"]");
- },
- set : function(table,id,record){
- if(!table){return null;}
- if(id==null){ id=this.nextid(table); }
- window.localStorage.setItem(table+"["+id.toString()+"]",record);
- return id;
- },
- del : function(table,id){
- if(!table || !id){return null;}
- window.localStorage.removeItem(table+"["+id.toString()+"]");
- return true;
- },
- zap : function(table){
- if(!table){return null;}
- var i=0;
- var n=0;
- var v=null;
- var x=null;
- var j=table+"."; //delete properties
- var k=table+"["; //delete records
- var l=table.length+1;
- var s = [];
- for(i=0;i<window.localStorage.length;i++){
- v = window.localStorage.key(i);
- x = v.substr(0,l);
- if(x==j || x==k){ s.push(v); n++;}
- }
- for(i=0;i<s.length;i++){ window.localStorage.removeItem(s[i]);}
- try{
- tables=window.localStorage.getObj("tables")
- out(tables)
- tables = tables.filter( function(item){ return item !== table })
- out(tables)
- window.localStorage.setObj("tables",tables)
- } catch(e){ out(e) }
- return n;
- },
- nextid : function(table){
- if(!table){return null;}
- var n = 0;
- n = window.localStorage.getItem(table+".nextid");
- if(!n){n=0;}
- n++;
- window.localStorage.setItem(table+".nextid",n.toString());
- return n;
- },
- id : function(table){
- return window.localStorage.getItem(table+".nextid");
- }
- }
- /*---- VISUALS ----*/
- function welcome(){
- if(!window.localStorage.visitcount){window.localStorage.visitcount=0;}
- window.localStorage.visitcount++;
- $("visitcount").innerHTML = window.localStorage.visitcount+ ((window.localStorage.visitcount<2) ? " time" : " times")
- $("lastvisit").innerHTML = (window.localStorage.lastvisit===undefined) ? "never" : window.localStorage.lastvisit
- window.localStorage.lastvisit="on "+new Date();
- refreshUI()
- }
- function refreshUI(){
- showgrid();
- refreshSelectMenu()
- mess1()
- mess2()
- }
- function showgrid(){
- tabObj=window.localStorage.getObj("tables")
- if(!tabObj){ tabObj=[-1] }
- var html=""
- for(tab=0, len=tabObj.length; tab<len; tab++)
- {
- table=tabObj[tab]
- if(tabObj==-1){ html="<table>Nothing here</table>" }
- else
- {
- html+="<table id='grid'>\n";
- html+="<caption>table: "+table+" <a onmouseover='mess1(\""+text.zap+"\",\""+table+"\")' href='javascript:dropTable(\""+table+"\")' class='del'>✗</a></caption>"
- html+="<tr><th>ID</th><th>Name</th><th>Phone</th><th>Email</th><th> </th></tr>";
- var i=0;
- var n=0;
- var x=null;
- var k=table+"[";
- var l=table.length+1;
- var s=[];
- var o=null;
- var id=null;
- for(i=0;i<window.localStorage.length;i++){
- x = window.localStorage.key(i);
- if(x.substr(0,l)==k){ s.push(x); n++;}
- }
- s.sort();
- for(i=0;i<s.length;i++){
- o = JSON.parse(window.localStorage.getItem(s[i]));
- id=s[i].substring(s[i].indexOf("[")+1,s[i].indexOf("]"));
- html+="<tr><td>"+id+"</td><td><a href='javascript:modRecord(\""+table+"\","+id+")'>"+o[0]+"</a></td><td>"+o[1]+"</td><td>"+o[2]+"</td><td><a href='javascript:delRecord(\""+table+"\","+id+")' class='del'>✗</a></td></tr>";
- }
- html+="</table>";
- }
- }
- $("list1").innerHTML=html;
- showraws();
- }
- /*---- Test ----*/
- function test(){
- try{
- clearDatabase()
- } catch(e){ out(e) }
- /*
- for(tab=0, len=window.localStorage.getObj("tables").length; tab>-1; tab--){
- currentTable=window.localStorage.getObj("tables")[tab]
- out("table:" +currentTable)
- }
- tables=window.localStorage.getObj("tables")
- if(!tables){ tables=[] }
- tables.push(tablename)
- window.localStorage.setObj("tables",tables)
- */
- }
- /*---- ACTIONS ----*/
- function load(){
- var data=[
- ['1 Doe' ,'555-1231','johndoe@gmail.com'],
- ['2 Doe' ,'555-1232','janedoe@hotmail.com'],
- ['3 Doe' ,'555-1233','jimmydoe@yahoo.com'],
- ['4 Doe' ,'555-1235','jennifer@example.com'],
- ['5 Doe' ,'555-1237','jamie@example.com']
- ];
- for(i=0;i<data.length;i++){ datamanager.set("customers",null,data[i].toSource()); }
- var data2=[
- ['Joel Doe' ,'555-1231','johndoe@gmail.com'],
- ['Jenk Doe' ,'555-1233','jimmydoe@yahoo.com'],
- ['Johan Doe' ,'555-1234','joanne@example.com'],
- ['Hennifer Doe' ,'555-1235','jennifer@example.com'],
- ['Yamie Doe' ,'555-1237','jamie@example.com']
- ];
- for(i=0;i<data2.length;i++){ datamanager.set("customers2",null,data2[i].toSource()); }
- window.localStorage.setObj("tables",["customers","customers2"])
- out(data.length+data2.length+" records loaded...");
- refreshUI()
- }
- function saveTable(){
- // prevent dupes in loop
- // maybe replace with objects so JS native prevents dupes?
- var tablename= $('tablename').value
- if(!tablename){ return }
- tables=window.localStorage.getObj("tables")
- if(!tables){ tables=[] }
- tables.push(tablename)
- window.localStorage.setObj("tables",tables)
- refreshUI()
- // add tables to record 'tableselect'
- }
- function saveRecord(isNew){
- try{
- var tablename= $('tableselected').value
- var id=$("id").value; if(id=="" || isNew){id=null;}
- var rec=[];
- rec[0]=$("name" ).value==""?"[new]":$("name").value;
- rec[1]=$("phone").value;
- rec[2]=$("email").value;
- id = datamanager.set(tablename,id,rec.toSource());
- $("id").value = id;
- refreshUI()
- } catch(e){ out(e) }
- }
- function clearRecord(record){
- if(record===undefined){
- $("tableselected").value="";
- $("id" ).value = "";
- $("name" ).value = "";
- $("phone").value = "";
- $("email").value = "";
- }
- else{
- $(record).value = "";
- }
- }
- function modRecord(table,id){
- //if(!table){return;}
- var rec = JSON.parse(datamanager.get(table,id));
- $("tableselected").value=table
- $("id" ).value = id;
- $("name" ).value = rec[0];
- $("phone").value = rec[1];
- $("email").value = rec[2];
- }
- function delRecord(table,id){
- if(!table){return;}
- datamanager.del(table,id);
- refreshUI()
- }
- function dropTable(table){
- var n = datamanager.zap(table);
- refreshUI()
- tablesel=$('tableselected').value
- if(table==tablesel){
- clearRecord()
- }
- out(n+" records deleted");
- }
- function clearDatabase(){
- // clears only the tables, not other data
- tables=window.localStorage.getObj("tables")
- tab_o=tables.length;
- for(tab=tab_o; tab>-1; tab--){
- currentTable=tables[tab]
- //out("table" +tab+ ":" +currentTable)
- datamanager.zap(currentTable)
- }
- refreshUI()
- out(tab_o+" tables deleted!")
- }
- /*---- debug ----*/
- function clearDebug(){ out("<br>",true); }
- function showraws(){
- var i,k,v,z;
- k = [];
- for(i=0;i<window.localStorage.length;i++){ k.push(window.localStorage.key(i)); }
- k.sort();
- clearDebug();
- for(i=0;i<k.length;i++){
- v=window.localStorage.getItem(k[i]);
- out(k[i]+": "+v);
- }
- out(" ");
- }
- function space(){
- // shows length, bytes, max
- out("Space used: "+window.localStorage.length);
- }
- function emptyLocalStorage(){
- window.localStorage.clear();
- refreshUI()
- out("Storage is empty...");
- }
- /* ---- menu manager ---- */
- function makeMenuItem(item){
- html='<a href="#" onclick="menuSelect(\''+item+'\')">'+item+'</a>'
- return html
- }
- function refreshSelectMenu(){
- try{
- tables=window.localStorage.getObj("tables")
- if(!tables){
- $('tableselectitems').innerHTML='Nothing Here'
- return
- }
- html=''
- for(tab=0, len=tables.length; tab<len; tab++){
- currentTable=tables[tab]
- //out("table:" +currentTable)
- html+=makeMenuItem(currentTable)
- }
- $('tableselectitems').innerHTML=html
- } catch(e){ out(e) }
- }
- function openMenu(){
- $('tableselectitems').classList.toggle('showElement')
- }
- function menuSelect(item){
- $("tableselected").value=item
- $("id").value=datamanager.id(item)
- }
- /*---- TEXT ----*/
- var text={
- show :'Show the contents of the local storage',
- clear:'Clears the output window',
- empty:'Clears the local storage',
- zap :'Deletes all records from the table ',
- space:'Shows available space in the local storage',
- load :'Loads test data in the local storage',
- view :'Refresh all records in the datagrid',
- end :'The End',
- test :'Test some shit',
- addtable :'Adds a new table',
- addrecord :'Adds a new record',
- deldb : 'Clears all tables'
- };
- /*---- events ----*/
- window.onclick= function(event){
- // menu hide if clicked outside
- try{
- //out(event +' '+event.target+' '+event.target.id)
- if(event.target.id!='tableselectbutton'){
- t=$('tableselectitems').classList
- if(t.contains('showElement')){ t.remove('showElement') }
- }
- } catch(e){ out(e) }
- }
- /*---- UTILITIES ----*/
- function getAttr(element,attribute){
- return document.getElementById(element).getAttribute(attribute)
- }
- function setAttr(element,attribute,newvalue){
- return document.getElementById(element).setAttribute(attribute,newvalue)
- }
- function $(id){ return document.getElementById(id); }
- function debug(msg){ setTimeout("throw(new Error('"+msg+"'))",0); }
- function mess1(msg,table) {
- if(!table){ table="" }
- if(!msg){ msg="" }
- document.getElementById("mess1").innerHTML=">"+msg+" "+table;
- }
- function mess2(msg) {
- if(!msg){ msg="" }
- document.getElementById("mess2").innerHTML=">"+msg;
- }
- function out(msg,noappend){
- if(noappend){ document.getElementById("output").innerHTML=msg; }
- else{ document.getElementById("output").innerHTML+="<br>"+msg; }
- }
- Storage.prototype.setObj = function(key, obj) {
- return this.setItem(key, JSON.stringify(obj))
- }
- Storage.prototype.getObj = function(key) {
- return JSON.parse(this.getItem(key))
- }
- </script>
- </head>
- <body onload="welcome()" class="fuck">
- <div id="welcome">
- Welcome, you have visited us <span id="visitcount">0</span>.<br>
- You last visit was <span id="lastvisit">0</span>.<br>
- </div>
- <hr>
- <table id="main">
- <tbody><tr>
- <td id='leftSide'>
- <div>
- <button onclick="load()" onmouseover="mess1(text.load)">Load Test Data</button>
- <button onclick="clearDatabase()" onmouseover="mess1(text.deldb)">Clear Database</button>
- <button onclick="test()" onmouseover="mess1(text.test)">Test</button><br>
- <span id="mess1">></span>
- </div>
- <fieldset id="form1"><legend><b>Add Table</b></legend>
- <label>Name: </label><input type="textbox" name="name" id="tablename"> <a href='javascript:clearRecord("tablename")' class='del'>✗</a> <br>
- <button onclick="saveTable()" onmouseover="mess1(text.addtable)">Create Table</button>
- </fieldset>
- <br>
- <fieldset id="form2"><legend><b>Add Record</b></legend>
- <label><button id="tableselectbutton" onclick="openMenu()">Table </button><div id='tableselectitems'></div>:</label><input type="textbox" id="tableselected" disabled><br>
- <label>ID: </label><input type="textbox" name="id" id="id" disabled><br>
- <label>Name: </label><input type="textbox" name="name" id="name"> <a href='javascript:clearRecord("name")' class='del'>✗</a> <br>
- <label>Phone:</label><input type="textbox" name="phone" id="phone"> <a href='javascript:clearRecord("phone")' class='del'>✗</a> <br>
- <label>Email:</label><input type="textbox" name="email" id="email"> <a href='javascript:clearRecord("email")' class='del'>✗</a> <br>
- <button onclick="clearRecord()">Clear Record</button>
- <button onclick="saveRecord()">Save Record</button>
- <button onclick="saveRecord(true)">Save as New</button>
- </fieldset>
- <br>
- <div id="list1"><table id="grid"></table></div>
- </td>
- <td id='rightSide'>
- <div>
- <button onclick="showraws()" onmouseover="mess2(text.show)">Show Storage</button>
- <button onclick="emptyLocalStorage()" onmouseover="mess2(text.empty)">Empty Storage</button>
- <button onclick="space()" onmouseover="mess2(text.space)">Storage Space</button>
- <button onclick="clearDebug()" onmouseover="mess2(text.clear)">Clear Output</button>
- <br>
- <span id="mess2">></span>
- </div>
- <div id="output"></div>
- </td>
- </tr>
- </tbody></table>
- <hr>
- </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement