forked from Tencent/vConsole
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Storage plugin as build-in plugin
- Loading branch information
Showing
8 changed files
with
4,646 additions
and
12 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div> | ||
<dl class="vc-table-row"> | ||
<dd class="vc-table-col">Name</dd> | ||
<dd class="vc-table-col vc-table-col-2">Value</dd> | ||
</dl> | ||
{{for (var i=0; i<list.length; i++)}} | ||
<dl class="vc-table-row"> | ||
<dd class="vc-table-col">{{list[i].name}}</dd> | ||
<dd class="vc-table-col vc-table-col-2">{{list[i].value}}</dd> | ||
</dl> | ||
{{/for}} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/* | ||
Tencent is pleased to support the open source community by making vConsole available. | ||
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
http://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
/** | ||
* vConsole Resouces Plugin | ||
*/ | ||
|
||
import './style.less'; | ||
import VConsolePlugin from '../lib/plugin.js'; | ||
import tplTabbox from './tabbox.html'; | ||
import tplList from './list.html'; | ||
|
||
import * as tool from '../lib/tool.js'; | ||
import $ from '../lib/query.js'; | ||
|
||
class VConsoleStorageTab extends VConsolePlugin { | ||
|
||
constructor(...args) { | ||
super(...args); | ||
|
||
this.$tabbox = $.render(tplTabbox, {}); | ||
this.currentType = ''; // cookies, localstorage, ... | ||
this.typeNameMap = { | ||
'cookies': 'Cookies', | ||
'localstorage': 'LocalStorage' | ||
} | ||
} | ||
|
||
onRenderTab(callback) { | ||
callback(this.$tabbox); | ||
} | ||
|
||
onAddTool(callback) { | ||
let that = this; | ||
let toolList = [{ | ||
name: 'Refresh', | ||
global: false, | ||
onClick: function(e) { | ||
that.renderStorage(); | ||
} | ||
}, { | ||
name: 'Clear', | ||
global: false, | ||
onClick: function(e) { | ||
that.clearLog(); | ||
} | ||
}]; | ||
callback(toolList); | ||
} | ||
|
||
onReady() { | ||
let that = this; | ||
|
||
$.delegate($.one('.vc-sub-tabbar', that.$tabbox), 'click', '.vc-subtab', function(e) { | ||
$.removeClass($.all('.vc-subtab', that.$tabbox), 'vc-actived'); | ||
$.addClass(this, 'vc-actived'); | ||
|
||
that.currentType = this.dataset.type; | ||
that.renderStorage(); | ||
}); | ||
|
||
// show default list | ||
this.currentType = 'cookies'; | ||
this.renderStorage(); | ||
} | ||
|
||
clearLog() { | ||
if (this.currentType && window.confirm) { | ||
let result = window.confirm('Remove all ' + this.typeNameMap[this.currentType] + '?'); | ||
if (!result) { | ||
return false; | ||
} | ||
} | ||
switch (this.currentType) { | ||
case 'cookies': | ||
this.clearCookieList(); | ||
break; | ||
case 'localstorage': | ||
this.clearLocalStorageList(); | ||
break; | ||
default: | ||
return false; | ||
} | ||
this.renderStorage(); | ||
} | ||
|
||
renderStorage() { | ||
let list = []; | ||
|
||
switch (this.currentType) { | ||
case 'cookies': | ||
list = this.getCookieList(); | ||
break; | ||
case 'localstorage': | ||
list = this.getLocalStorageList(); | ||
break; | ||
default: | ||
return false; | ||
} | ||
|
||
let $log = $.one('.vc-log', this.$tabbox); | ||
if (list.length == 0) { | ||
$log.innerHTML = ''; | ||
} else { | ||
// html encode for rendering | ||
for (let i=0; i<list.length; i++) { | ||
list[i].name = tool.htmlEncode(list[i].name); | ||
list[i].value = tool.htmlEncode(list[i].value); | ||
} | ||
$log.innerHTML = $.render(tplList, {list: list}, true); | ||
} | ||
} | ||
|
||
getCookieList() { | ||
if (!document.cookie || !navigator.cookieEnabled) { | ||
return []; | ||
} | ||
|
||
let list = []; | ||
let items = document.cookie.split(';'); | ||
for (let i=0; i<items.length; i++) { | ||
let item = items[i].split('='); | ||
let name = item[0].replace(/^ /, ''), | ||
value = item[1]; | ||
list.push({ | ||
name: decodeURIComponent(name), | ||
value: decodeURIComponent(value) | ||
}); | ||
} | ||
return list; | ||
} | ||
|
||
getLocalStorageList() { | ||
if (!window.localStorage) { | ||
return []; | ||
} | ||
|
||
try { | ||
let list = [] | ||
for (var i = 0; i < localStorage.length; i++) { | ||
let name = localStorage.key(i), | ||
value = localStorage.getItem(name); | ||
list.push({ | ||
name: name, | ||
value: value | ||
}); | ||
} | ||
return list; | ||
} catch (e) { | ||
return []; | ||
} | ||
} | ||
|
||
clearCookieList() { | ||
if (!document.cookie || !navigator.cookieEnabled) { | ||
return; | ||
} | ||
|
||
let list = this.getCookieList(); | ||
for (var i=0; i<list.length; i++) { | ||
document.cookie = list[i].name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT'; | ||
} | ||
this.renderStorage(); | ||
} | ||
|
||
clearLocalStorageList() { | ||
if (!!window.localStorage) { | ||
try { | ||
localStorage.clear(); | ||
this.renderStorage(); | ||
} catch (e) { | ||
alert('localStorage.clear() fail.'); | ||
} | ||
} | ||
} | ||
|
||
} // END Class | ||
|
||
export default VConsoleStorageTab; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.vc-sub-tabbar { | ||
background-color: #fbf9fe; | ||
display: flex; | ||
flex-direction: row; | ||
flex-wrap: wrap; | ||
|
||
.vc-subtab { | ||
flex: 1; | ||
line-height: 30px; | ||
padding: 0 15px; | ||
border-right: 1px solid #D9D9D9; | ||
border-bottom: 1px solid #D9D9D9; | ||
text-decoration: none; | ||
text-align: center; | ||
color: #000; | ||
-webkit-tap-highlight-color: transparent; | ||
-webkit-touch-callout: none; | ||
} | ||
.vc-subtab:active { | ||
background-color: rgba(0,0,0,0.15); | ||
} | ||
.vc-subtab.vc-actived { | ||
background-color: #FFF; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div class="vc-table"> | ||
<div class="vc-sub-tabbar"> | ||
<a href="javascript:;" class="vc-subtab vc-actived" data-type="cookies">Cookies</a> | ||
<a href="javascript:;" class="vc-subtab" data-type="localstorage">LocalStorage</a> | ||
</div> | ||
<div class="vc-log"></div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters