localStorageAdapter.js
"use strict";
var efolabObj = {};
efolabObj.localStorageAdapter = function(key) {
var oldValue;
this.KEY = key;
this.dataArray = [];
oldValue = this.getLocalStorageAll();
if (oldValue) {
console.log("oldValueあり");
this.dataArray = oldValue;
}else{
console.log("oldValueなし");
}
window.addEventListener('unload', ( function(ns) {
return function() {
ns.saveLocalStorage();
};
}(this)), false);
};
efolabObj.localStorageAdapter.prototype = {
getKey : function() {
return this.KEY;
},
getSize : function() {
var res = 0;
if(this.dataArray.length){
res = this.dataArray.length;
}
return res;
},
addData : function(jsonData) {
jsonData.seq = this.getSize();
this.dataArray.push(jsonData);
console.log(this.dataArray);
},
getDataAll : function() {
return this.dataArray;
},
getData : function(index) {
return this.dataArray[index];
},
clearData : function(){
this.dataArray.length = 0;
},
getLocalStorageAll : function() {
var items;
items = JSON.parse(localStorage.getItem(this.KEY));
console.log(items);
return items;
},
saveLocalStorage : function() {
if(this.dataArray.length !== 0){
var jsonString = JSON.stringify(this.dataArray);
console.log(this.dataArray);
console.log(jsonString);
localStorage.setItem(this.KEY, jsonString);
console.log("データをセーブしました");
}
}
};
( function() {
var ns = efolabObj, adapter = null, FINAL_KEY = "adapter_test", add_button, clear_button, show_button, save_button;
function getTime() {
var currentTime = new Date(), h, m, s, timeStr;
h = "0" + currentTime.getHours();
m = "0" + currentTime.getMinutes();
s = "0" + currentTime.getSeconds();
h = h.substr(h.length - 2, 2);
m = m.substr(m.length - 2, 2);
s = s.substr(s.length - 2, 2);
timeStr = h + ":" + m + ":" + s;
return timeStr;
}
function init() {
adapter = new ns.localStorageAdapter(FINAL_KEY);
add_button = document.getElementById('add_button');
clear_button = document.getElementById('clear_button');
show_button = document.getElementById('show_button');
save_button = document.getElementById('save_button');
add_button.addEventListener('click', function() {
var json = {
"data" : getTime().toString()
};
adapter.addData(json);
}, false);
clear_button.addEventListener('click', function() {
adapter.clearData();
localStorage.removeItem(FINAL_KEY);
}, false);
show_button.addEventListener('click', function() {
var table,items, i,j, row, cell_1,cell_2;
table = document.createElement('table');
items = adapter.getDataAll();
for ( i = 0; i < items.length; i += 1) {
row = document.createElement('tr');
cell_1 = document.createElement('td');
cell_1.innerHTML = items[i].seq;
row.appendChild(cell_1);
cell_2 = document.createElement('td');
cell_2.innerHTML = items[i].data;
row.appendChild(cell_2);
table.appendChild(row);
console.log("seq:" + items[i].seq + ",data:" + items[i].data);
}
document.body.appendChild(table);
}, false);
save_button.addEventListener('click', function() {
adapter.saveLocalStorage();
}, false);
}
if (document.body) {
init();
} else {
document.addEventListener('DOMContentLoaded', init, false);
}
}());