import each from 'licia/each' import isStr from 'licia/isStr' import startWith from 'licia/startWith' import truncate from 'licia/truncate' import LunaModal from 'luna-modal' import LunaDataGrid from 'luna-data-grid' import isNull from 'licia/isNull' import trim from 'licia/trim' import copy from 'licia/copy' import emitter from '../lib/emitter' import { safeStorage, classPrefix as c } from '../lib/util' export default class Storage { constructor($container, devtools, resources, type) { this._type = type this._$container = $container this._devtools = devtools this._resources = resources this._selectedItem = null this._storeData = [] this._initTpl() this._dataGrid = new LunaDataGrid(this._$dataGrid.get(0), { columns: [ { id: 'key', title: 'キー', weight: 30, }, { id: 'value', title: '値', weight: 90, }, ], minHeight: 60, maxHeight: 223, }) this._bindEvent() } destroy() { emitter.off(emitter.SCALE, this._updateGridHeight) } refresh() { const dataGrid = this._dataGrid this._refreshStorage() dataGrid.clear() each(this._storeData, ({ key, val }) => { dataGrid.append( { key, value: val, }, { selectable: true, } ) }) } _refreshStorage() { const resources = this._resources let store = safeStorage(this._type, false) if (!store) return const storeData = [] // Mobile safari is not able to loop through localStorage directly. store = JSON.parse(JSON.stringify(store)) each(store, (val, key) => { // According to issue 20, not all values are guaranteed to be string. if (!isStr(val)) return if (resources.config.get('hideErudaSetting')) { if (startWith(key, 'eruda') || key === 'active-eruda') return } storeData.push({ key: key, val: truncate(val, 200), }) }) this._storeData = storeData } _updateButtons() { const $container = this._$container const $showDetail = $container.find(c('.show-detail')) const $deleteStorage = $container.find(c('.delete-storage')) const $copyStorage = $container.find(c('.copy-storage')) const btnDisabled = c('btn-disabled') $showDetail.addClass(btnDisabled) $deleteStorage.addClass(btnDisabled) $copyStorage.addClass(btnDisabled) if (this._selectedItem) { $showDetail.rmClass(btnDisabled) $deleteStorage.rmClass(btnDisabled) $copyStorage.rmClass(btnDisabled) } } _initTpl() { const $container = this._$container const type = this._type $container.html( c(`