comparison default/static/common.js @ 83:caab77328b1c

js (common): added a method to check if local or session storage is available
author Goffi <goffi@goffi.org>
date Wed, 03 Jan 2018 01:12:16 +0100
parents d1741c2f3e9d
children
comparison
equal deleted inserted replaced
82:6ba0129a9a4e 83:caab77328b1c
1 var __session_storage_available;
2 var __local_storage_available;
3
4 function storageAvailable(type) {
5 /* check if session or local storage is available
6 *
7 * @param type(string): "session" or "storage"
8 * @return (boolean): true if requested storage is available
9 */
10 console.assert(type == 'session' || type == 'storage', "bad storage type (%s)", type);
11 const var_name = '__' + type + '_storage_available';
12 var available = window[var_name];
13 if (available === undefined) {
14 // test method from https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
15 var storage = window[type + 'Storage'];
16 try {
17 x = '__storage_test__';
18 storage.setItem(x, x);
19 storage.removeItem(x);
20 available = true;
21 }
22 catch(e) {
23 available = e instanceof DOMException && (
24 // everything except Firefox
25 e.code === 22 ||
26 // Firefox
27 e.code === 1014 ||
28 // test name field too, because code might not be present
29 // everything except Firefox
30 e.name === 'QuotaExceededError' ||
31 // Firefox
32 e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
33 // acknowledge QuotaExceededError only if there's something already stored
34 storage.length !== 0;
35 }
36
37 if (!available) {
38 console.warn("%s storage not available", type);
39 }
40 window[var_name] = available;
41 }
42
43 return available;
44 }
45
1 function toggle_clicked_class_tag(tag_name, class_name='clicked') { 46 function toggle_clicked_class_tag(tag_name, class_name='clicked') {
2 for (let elt of document.getElementsByTagName(tag_name)) { 47 for (let elt of document.getElementsByTagName(tag_name)) {
3 elt.classList.toggle(class_name); 48 elt.classList.toggle(class_name);
4 } 49 }
5 } 50 }
44 89
45 function clicked_mh_fix(arg) { 90 function clicked_mh_fix(arg) {
46 /* toggle clicked, and fix max-height on transitionend 91 /* toggle clicked, and fix max-height on transitionend
47 * 92 *
48 * needed to workaround transition issue with max-height:none 93 * needed to workaround transition issue with max-height:none
94 * inspired from https://css-tricks.com/using-css-transitions-auto-dimensions,
95 * thanks to Brandon Smith
49 * 96 *
50 * inspired from https://css-tricks.com/using-css-transitions-auto-dimensions, 97 * @param arg: element to toggle (id as string, or element itself)
51 * thanks to Brandon Smith */ 98 * */
52 elt = get_elt(arg); 99 elt = get_elt(arg);
53 100
54 if (!elt.classList.contains("clicked")) { 101 if (!elt.classList.contains("clicked")) {
55 /* expand */ 102 /* expand */
56 var fix_expand = function(event) { 103 var fix_expand = function(event) {