comparison sat_templates/default/static/common.js @ 147:33c7ce833d3f

install: setup.py fix + moved "default" dir in a "sat_templates" dir: the merge request at https://bugs.goffi.org/mr/view/3 was a good basis, but not fully working ("default" dir was removed), this patch fixes it, and do some improvments: - moved "default" in "sat_templates" dir, which correspond to the python module, so it can be found easily from python - added VERSION, and mercurial hash detection, in the same way as for Cagou and backend - slight modification of classifiers - replaces tabs coming from MR by spaces
author Goffi <goffi@goffi.org>
date Sat, 02 Jun 2018 17:25:43 +0200
parents default/static/common.js@caab77328b1c
children
comparison
equal deleted inserted replaced
146:7dc00829c32f 147:33c7ce833d3f
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
46 function toggle_clicked_class_tag(tag_name, class_name='clicked') {
47 for (let elt of document.getElementsByTagName(tag_name)) {
48 elt.classList.toggle(class_name);
49 }
50 }
51
52 function toggle_clicked_class_sel(selectors, class_name='clicked') {
53 for (let elt of document.querySelectorAll(selectors)) {
54 elt.classList.toggle(class_name);
55 }
56 }
57
58 function set_clicked_class_id(trigger_elem_id, target_elem_id=null, class_name='clicked') {
59 if (target_elem_id === null) { target_elem_id = trigger_elem_id; }
60 document.getElementById(trigger_elem_id).addEventListener(
61 "click",
62 function() {
63 document.getElementById(target_elem_id).classList.toggle(class_name);
64 }
65 );
66 }
67
68 function get_elt(arg) {
69 if (typeof arg === 'string') {
70 // we should have an id
71 return document.getElementById(arg);
72 }
73 else {
74 // we should have an element
75 return arg;
76 }
77 }
78
79 function clicked_cls(elt) {
80 /* toggle "clicked" class on each click, and remove "init" class if present */
81 // init
82 if (elt.classList.contains("init")) {
83 elt.classList.remove("init");
84 }
85
86 // clicked
87 elt.classList.toggle("clicked");
88 }
89
90 function clicked_mh_fix(arg) {
91 /* toggle clicked, and fix max-height on transitionend
92 *
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
96 *
97 * @param arg: element to toggle (id as string, or element itself)
98 * */
99 elt = get_elt(arg);
100
101 if (!elt.classList.contains("clicked")) {
102 /* expand */
103 var fix_expand = function(event) {
104 elt.removeEventListener("transitionend", fix_expand, false);
105 if (elt.classList.contains("clicked")) {
106 /* if event is clicked quicker than transition time,
107 * this callback can be called on reduce */
108 elt.style.maxHeight = "none";
109 }
110 };
111
112 elt.setAttribute('max_height_init', elt.clientHeight);
113 elt.addEventListener("transitionend", fix_expand, false);
114 clicked_cls(elt);
115 elt.style.maxHeight = elt.scrollHeight + 'px';
116 }
117 else {
118 /* reduce */
119 var transition_save = elt.style.transition;
120 elt.style.transition = '';
121 requestAnimationFrame(function() {
122 elt.style.maxHeight = elt.scrollHeight + 'px';
123 elt.style.transition = transition_save;
124
125 requestAnimationFrame(function() {
126 elt.style.maxHeight = elt.getAttribute('max_height_init') + 'px';
127 elt.removeAttribute('max_height_init');
128 elt.style.maxHeight = null;
129 });
130 });
131
132 clicked_cls(elt);
133 }
134 }