たぼさんの部屋

いちょぼとのんびり

localStorageAdapter

localStorageAdapter.js

"use strict";
var efolabObj = {};
//名前空間を定義
efolabObj.localStorageAdapter = function(key) {
   var oldValue;
   this.KEY = key;
   this.dataArray = [];

   //前回データをローカルストレージから取得してdataArrayに格納する
   oldValue = this.getLocalStorageAll();
   if (oldValue) {
      console.log("oldValueあり");
      this.dataArray = oldValue;
   }else{
      console.log("oldValueなし");
   }
   //ページ終了時にはローカルストレージに格納する
   window.addEventListener('unload', ( function(ns) {
         //closerでthisをパックして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);
      // Array.prototype.push.apply(this.dataArray,[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() {
         //localStorageAdapter実装
         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);
      }
   }());