yux-storage.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * @yux-storage.js
  3. * @author xboxyan
  4. * @email yanwenbin1991@live.com
  5. * Created: 20-11-10
  6. * Update: 21-03-13
  7. */
  8. function guid() {
  9. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  10. var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
  11. return v.toString(16);
  12. });
  13. }
  14. class yuxDB {
  15. constructor() {
  16. this.ready();
  17. }
  18. ready() {
  19. return new Promise((resolve, reject) => {
  20. if (this.db) {
  21. resolve(this);
  22. } else {
  23. this.objectStoreName = 'dozs-store';
  24. const request = window.indexedDB.open('dozs-project', 1);
  25. request.onsuccess = (event) => {
  26. this.db = event.target.result;
  27. resolve(this);
  28. };
  29. request.onupgradeneeded = (event) => {
  30. const db = event.target.result;
  31. if (!db.objectStoreNames.contains(this.objectStoreName)) {
  32. db.createObjectStore(this.objectStoreName);
  33. }
  34. };
  35. request.onerror = (event) => {
  36. reject(event);
  37. }
  38. }
  39. })
  40. }
  41. init(request, callback) {
  42. return new Promise((resolve, reject) => {
  43. const success = value => {
  44. if (callback && typeof callback === 'function') {
  45. callback(false, value);
  46. }
  47. resolve(value);
  48. }
  49. const error = event => {
  50. if (callback && typeof callback === 'function') {
  51. callback(event);
  52. }
  53. reject(event);
  54. }
  55. return this.ready().then(() => {
  56. request(success, error);
  57. }).catch(error)
  58. })
  59. }
  60. setItem(key, value, callback) {
  61. return this.init((success, error)=>{
  62. const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).put(value, key);
  63. request.onsuccess = () => success(value);
  64. request.onerror = error;
  65. }, callback)
  66. }
  67. getItem(key, callback) {
  68. return this.init((success, error)=>{
  69. const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).get(key);
  70. request.onsuccess = () => success(request.result);
  71. request.onerror = error;
  72. }, callback)
  73. }
  74. removeItem(key, callback) {
  75. return this.init((success, error)=>{
  76. const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).delete(key);
  77. request.onsuccess = () => success(key);
  78. request.onerror = error;
  79. }, callback)
  80. }
  81. key(index, callback) {
  82. return this.init((success, error)=>{
  83. const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys();
  84. request.onsuccess = () => success(request.result[index]);
  85. request.onerror = error;
  86. }, callback)
  87. }
  88. keys(callback) {
  89. return this.init((success, error)=>{
  90. const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys();
  91. request.onsuccess = () => success(request.result);
  92. request.onerror = error;
  93. }, callback)
  94. }
  95. clear(callback) {
  96. return this.init((success, error)=>{
  97. const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).clear();
  98. request.onsuccess = () => success(null);
  99. request.onerror = error;
  100. }, callback)
  101. }
  102. }
  103. if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
  104. module.exports = new yuxDB();
  105. } else {
  106. window.yuxStorage = new yuxDB();
  107. }
  108. function gets(key) {
  109. yuxStorage.getItem(key).then(res => {
  110. console.log(key, '=>', res)
  111. // new LightTip().success(JSON.stringify(key)+': '+Object.prototype.toString.call(res)+ ' '+JSON.stringify(res));
  112. }).catch(err => {
  113. console.log(err)
  114. //new LightTip().error(JSON.stringify(err));
  115. })
  116. }
  117. function sets(key, value,fn) {
  118. yuxStorage.setItem(key, value,fn).then(res => {
  119. console.log(key, '设置完成')
  120. // new LightTip().success('设置完成');
  121. }).catch(err => {
  122. console.log(err)
  123. // new LightTip().error(JSON.stringify(err));
  124. })
  125. }
  126. function removes(key) {
  127. yuxStorage.removeItem(key).then(res => {
  128. console.log(key, '已被移除')
  129. //new LightTip().success(JSON.stringify(key)+'已被移除');
  130. }).catch(err => {
  131. console.log(err)
  132. // new LightTip().error(JSON.stringify(err));
  133. })
  134. }
  135. function clearStorage() {
  136. yuxStorage.clear().then(res => {
  137. console.log('数据已清空')
  138. // new LightTip().success('数据已清空');
  139. }).catch(err => {
  140. console.log(err)
  141. // new LightTip().error(JSON.stringify(err));
  142. })
  143. }
  144. function key(index) {
  145. yuxStorage.key(index).then(res => {
  146. console.log(index, '=>', res)
  147. // new LightTip().success(`第${index}个键: `+JSON.stringify(res));
  148. })
  149. }
  150. function copyText(text) {
  151. var element = createElement(text);
  152. element.select();
  153. element.setSelectionRange(0, element.value.length);
  154. document.execCommand('copy');
  155. element.remove();
  156. alert("已复制到剪切板");
  157. }
  158. //创建临时的输入框元素
  159. function createElement(text) {
  160. var isRTL = document.documentElement.getAttribute('dir') === 'rtl';
  161. var element = document.createElement('textarea');
  162. // 防止在ios中产生缩放效果
  163. element.style.fontSize = '12pt';
  164. // 重置盒模型
  165. element.style.border = '0';
  166. element.style.padding = '0';
  167. element.style.margin = '0';
  168. // 将元素移到屏幕外
  169. element.style.position = 'absolute';
  170. element.style[isRTL ? 'right' : 'left'] = '-9999px';
  171. // 移动元素到页面底部
  172. let yPosition = window.pageYOffset || document.documentElement.scrollTop;
  173. element.style.top = `${yPosition}px`;
  174. //设置元素只读
  175. element.setAttribute('readonly', '');
  176. element.value = text;
  177. document.body.appendChild(element);
  178. return element;
  179. }