123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- /**
- * @yux-storage.js
- * @author xboxyan
- * @email yanwenbin1991@live.com
- * Created: 20-11-10
- * Update: 21-03-13
- */
- function guid() {
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
- var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
- return v.toString(16);
- });
- }
- class yuxDB {
- constructor() {
- this.ready();
- }
- ready() {
- return new Promise((resolve, reject) => {
- if (this.db) {
- resolve(this);
- } else {
- this.objectStoreName = 'dozs-store';
- const request = window.indexedDB.open('dozs-project', 1);
- request.onsuccess = (event) => {
- this.db = event.target.result;
- resolve(this);
- };
- request.onupgradeneeded = (event) => {
- const db = event.target.result;
- if (!db.objectStoreNames.contains(this.objectStoreName)) {
- db.createObjectStore(this.objectStoreName);
- }
- };
- request.onerror = (event) => {
- reject(event);
- }
- }
- })
- }
- init(request, callback) {
- return new Promise((resolve, reject) => {
- const success = value => {
- if (callback && typeof callback === 'function') {
- callback(false, value);
- }
- resolve(value);
- }
- const error = event => {
- if (callback && typeof callback === 'function') {
- callback(event);
- }
- reject(event);
- }
- return this.ready().then(() => {
- request(success, error);
- }).catch(error)
- })
- }
- setItem(key, value, callback) {
- return this.init((success, error)=>{
- const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).put(value, key);
- request.onsuccess = () => success(value);
- request.onerror = error;
- }, callback)
- }
- getItem(key, callback) {
- return this.init((success, error)=>{
- const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).get(key);
- request.onsuccess = () => success(request.result);
- request.onerror = error;
- }, callback)
- }
- removeItem(key, callback) {
- return this.init((success, error)=>{
- const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).delete(key);
- request.onsuccess = () => success(key);
- request.onerror = error;
- }, callback)
- }
- key(index, callback) {
- return this.init((success, error)=>{
- const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys();
- request.onsuccess = () => success(request.result[index]);
- request.onerror = error;
- }, callback)
- }
- keys(callback) {
- return this.init((success, error)=>{
- const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys();
- request.onsuccess = () => success(request.result);
- request.onerror = error;
- }, callback)
- }
- clear(callback) {
- return this.init((success, error)=>{
- const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).clear();
- request.onsuccess = () => success(null);
- request.onerror = error;
- }, callback)
- }
- }
- if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
- module.exports = new yuxDB();
- } else {
- window.yuxStorage = new yuxDB();
- }
- function gets(key) {
- yuxStorage.getItem(key).then(res => {
- console.log(key, '=>', res)
- // new LightTip().success(JSON.stringify(key)+': '+Object.prototype.toString.call(res)+ ' '+JSON.stringify(res));
- }).catch(err => {
- console.log(err)
- //new LightTip().error(JSON.stringify(err));
- })
- }
- function sets(key, value,fn) {
- yuxStorage.setItem(key, value,fn).then(res => {
- console.log(key, '设置完成')
- // new LightTip().success('设置完成');
- }).catch(err => {
- console.log(err)
- // new LightTip().error(JSON.stringify(err));
- })
- }
- function removes(key) {
- yuxStorage.removeItem(key).then(res => {
- console.log(key, '已被移除')
- //new LightTip().success(JSON.stringify(key)+'已被移除');
- }).catch(err => {
- console.log(err)
- // new LightTip().error(JSON.stringify(err));
- })
- }
- function clearStorage() {
- yuxStorage.clear().then(res => {
- console.log('数据已清空')
- // new LightTip().success('数据已清空');
- }).catch(err => {
- console.log(err)
- // new LightTip().error(JSON.stringify(err));
- })
- }
- function key(index) {
- yuxStorage.key(index).then(res => {
- console.log(index, '=>', res)
- // new LightTip().success(`第${index}个键: `+JSON.stringify(res));
- })
- }
- function copyText(text) {
- var element = createElement(text);
- element.select();
- element.setSelectionRange(0, element.value.length);
- document.execCommand('copy');
- element.remove();
- alert("已复制到剪切板");
- }
- //创建临时的输入框元素
- function createElement(text) {
- var isRTL = document.documentElement.getAttribute('dir') === 'rtl';
- var element = document.createElement('textarea');
- // 防止在ios中产生缩放效果
- element.style.fontSize = '12pt';
- // 重置盒模型
- element.style.border = '0';
- element.style.padding = '0';
- element.style.margin = '0';
- // 将元素移到屏幕外
- element.style.position = 'absolute';
- element.style[isRTL ? 'right' : 'left'] = '-9999px';
- // 移动元素到页面底部
- let yPosition = window.pageYOffset || document.documentElement.scrollTop;
- element.style.top = `${yPosition}px`;
- //设置元素只读
- element.setAttribute('readonly', '');
- element.value = text;
- document.body.appendChild(element);
- return element;
- }
|