You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
8.1 KiB

  1. /**
  2. * Movim Utils
  3. *
  4. * This file include some useful functions used quite everywhere in Movim
  5. */
  6. function movim_check_string(str) {
  7. if (typeof str == 'object') {
  8. return str instanceof String;
  9. } else {
  10. return typeof str == 'string';
  11. }
  12. }
  13. function movim_get_node(str) {
  14. if(movim_check_string(str))
  15. return document.querySelector(str);
  16. else
  17. return str;
  18. }
  19. /**
  20. * @brief Force Movim to go back to the login page
  21. */
  22. function movim_disconnect()
  23. {
  24. window.location.replace(ERROR_URI);
  25. }
  26. /**
  27. * @brief Force Movim to reload the page
  28. * @param string uri
  29. */
  30. function movim_reload(uri) {
  31. window.location.replace(uri);
  32. }
  33. /**
  34. * @brief Force Movim to reload the current page
  35. * @param string error
  36. */
  37. function movim_reload_this() {
  38. window.location.reload();
  39. }
  40. /**
  41. * @brief Force Movim to go to an url
  42. * @param string url
  43. */
  44. function movim_redirect(url) {
  45. window.location.href = url;
  46. }
  47. /**
  48. * @brief Return a hash (key->value) version of a form
  49. * @param string the name of the form
  50. * @return hash
  51. */
  52. function movim_parse_form(formname) {
  53. var form = document.forms[formname];
  54. if(!form)
  55. return false;
  56. var data = H();
  57. for(var i = 0; i < form.elements.length; i++) {
  58. if(form.elements[i].type == 'checkbox') {
  59. data.set(form.elements[i].name,
  60. form.elements[i].checked);
  61. } else if(form.elements[i].type == 'radio'
  62. && form.elements[i].checked ) {
  63. data.set(form.elements[i].name,
  64. form.elements[i].value);
  65. } else if(form.elements[i].type != 'radio'){
  66. data.set(form.elements[i].name,
  67. form.elements[i].value);
  68. }
  69. }
  70. return data;
  71. }
  72. /**
  73. * @brief Return a JSON version of a form
  74. * @param string the name of the form
  75. * @return JSON
  76. */
  77. function movim_form_to_json(formname) {
  78. var form = document.forms[formname];
  79. if(!form)
  80. return false;
  81. var json = {};
  82. for(var i = 0; i < form.elements.length; i++) {
  83. json_att = {};
  84. for(var j = 0; j < form.elements[i].attributes.length; j++) {
  85. json_att[form.elements[i].attributes[j].name] = form.elements[i].attributes[j].value;
  86. }
  87. if(form.elements[i].name.length != 0) {
  88. if(form.elements[i].type == 'checkbox')
  89. json[form.elements[i].name] = {'value' : form.elements[i].checked, 'attributes' : json_att};
  90. else if(form.elements[i].type == 'radio'
  91. && form.elements[i].checked )
  92. json[form.elements[i].name] = {'value' : form.elements[i].value, 'attributes' : json_att};
  93. else if(form.elements[i].type != 'radio')
  94. json[form.elements[i].name] = {'value' : form.elements[i].value, 'attributes' : json_att};
  95. }
  96. }
  97. return json;
  98. }
  99. /**
  100. * @brief A magical function to autoresize textarea when typing
  101. * @param DOMElement textbox
  102. */
  103. function movim_textarea_autoheight(textbox) {
  104. if(textbox != null ) {
  105. textbox.style.height = 0;
  106. textbox.style.height = textbox.scrollHeight +"px";
  107. }
  108. }
  109. /**
  110. * Class manipulation
  111. */
  112. /**
  113. * @brief Check if the element own the class
  114. * @param string the selector of the element (e.g '#myid', '.theclass')
  115. * @param string the class to check
  116. */
  117. function movim_has_class(element,classname) {
  118. var node = movim_get_node(element);
  119. if(!node) console.log('Node ' + element + ' not found');
  120. return node.className.match(new RegExp('(\\s|^)'+classname+'(\\s|$)'));
  121. }
  122. /**
  123. * @brief Add a class of an element
  124. * @param string the selector of the element
  125. * @param string the class to add
  126. */
  127. function movim_add_class(element,classname) {
  128. if(!movim_has_class(element,classname)) {
  129. var element = movim_get_node(element);
  130. element.className += " "+classname;
  131. }
  132. }
  133. /**
  134. * @brief Remove a class of an element
  135. * @param string the selector of the element
  136. * @param string the class to remove
  137. */
  138. function movim_remove_class(element,classname) {
  139. if (movim_has_class(element,classname)) {
  140. var reg = new RegExp('(\\s|^)'+classname+'(\\s|$)');
  141. var element = movim_get_node(element);
  142. element.className=element.className.replace(reg,' ');
  143. }
  144. }
  145. /**
  146. * @brief Toggle the class of an element
  147. * @param string the selector of the element
  148. * @param string the class to toggle
  149. */
  150. function movim_toggle_class(element, classname) {
  151. if(movim_has_class(element, classname))
  152. movim_remove_class(element,classname);
  153. else
  154. movim_add_class(element, classname);
  155. }
  156. /**
  157. * @brief Save the current button class
  158. * @param string the selector of the element
  159. */
  160. function movim_button_save(element) {
  161. var elt = document.querySelector(element);
  162. elt.dataset.oldclassname = elt.className;
  163. }
  164. /**
  165. * @brief Reset the button
  166. * @param string the selector of the element
  167. */
  168. function movim_button_reset(element) {
  169. var elt = document.querySelector(element);
  170. elt.className = elt.dataset.oldclassname;
  171. }
  172. /**
  173. * @brief Toggle the visibility of an element
  174. * @param string the selector of the element
  175. */
  176. function movim_toggle_display(element) {
  177. var node = movim_get_node(element);
  178. if(node != null) {
  179. if(node.style.display == 'block')
  180. node.style.display = 'none';
  181. else
  182. node.style.display = 'block';
  183. }
  184. }
  185. /**
  186. * @brief Set object in localStorage
  187. * @param key string
  188. * @param value the object
  189. */
  190. Storage.prototype.setObject = function(key, value) {
  191. this.setItem(key, JSON.stringify(value));
  192. }
  193. /**
  194. * @brief Get object in localStorage
  195. * @param key
  196. */
  197. Storage.prototype.getObject = function(key) {
  198. return JSON.parse(this.getItem(key));
  199. }
  200. function base64_decode(data) {
  201. // discuss at: http://phpjs.org/functions/base64_decode/
  202. // original by: Tyler Akins (http://rumkin.com)
  203. // improved by: Thunder.m
  204. // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  205. // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  206. // input by: Aman Gupta
  207. // input by: Brett Zamir (http://brett-zamir.me)
  208. // bugfixed by: Onno Marsman
  209. // bugfixed by: Pellentesque Malesuada
  210. // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  211. // example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
  212. // returns 1: 'Kevin van Zonneveld'
  213. // example 2: base64_decode('YQ===');
  214. // returns 2: 'a'
  215. var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  216. var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
  217. ac = 0,
  218. dec = '',
  219. tmp_arr = [];
  220. if (!data) {
  221. return data;
  222. }
  223. data += '';
  224. do { // unpack four hexets into three octets using index points in b64
  225. h1 = b64.indexOf(data.charAt(i++));
  226. h2 = b64.indexOf(data.charAt(i++));
  227. h3 = b64.indexOf(data.charAt(i++));
  228. h4 = b64.indexOf(data.charAt(i++));
  229. bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
  230. o1 = bits >> 16 & 0xff;
  231. o2 = bits >> 8 & 0xff;
  232. o3 = bits & 0xff;
  233. if (h3 == 64) {
  234. tmp_arr[ac++] = String.fromCharCode(o1);
  235. } else if (h4 == 64) {
  236. tmp_arr[ac++] = String.fromCharCode(o1, o2);
  237. } else {
  238. tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
  239. }
  240. } while (i < data.length);
  241. dec = tmp_arr.join('');
  242. return dec.replace(/\0+$/, '');
  243. }
  244. /**
  245. * @brief Sanitize string for easy search
  246. * @param string
  247. */
  248. function accentsTidy(s){
  249. //Ian Elliott in http://stackoverflow.com/questions/990904/javascript-remove-accents-diacritics-in-strings
  250. var r = s.toLowerCase();
  251. r = r.replace(new RegExp("\\s", 'g'),"");
  252. r = r.replace(new RegExp("[àáâãäå]", 'g'),"a");
  253. r = r.replace(new RegExp("æ", 'g'),"ae");
  254. r = r.replace(new RegExp("ç", 'g'),"c");
  255. r = r.replace(new RegExp("[èéêë]", 'g'),"e");
  256. r = r.replace(new RegExp("[ìíîï]", 'g'),"i");
  257. r = r.replace(new RegExp("ñ", 'g'),"n");
  258. r = r.replace(new RegExp("[òóôõö]", 'g'),"o");
  259. r = r.replace(new RegExp("œ", 'g'),"oe");
  260. r = r.replace(new RegExp("[ùúûü]", 'g'),"u");
  261. r = r.replace(new RegExp("[ýÿ]", 'g'),"y");
  262. r = r.replace(new RegExp("\\W", 'g'),"");
  263. return r;
  264. };