nav.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. var ynh_url = 'https://arn-fai.net/nav/';
  2. var ynh_not_in_frame = (top.location==self.document.location);
  3. if (ynh_not_in_frame) {
  4. ynh_loadScript(ynh_url+'lib/jquery/fquery.min.js', function(){
  5. var f$=fQuery;
  6. f$(document).ready(function(f$) {
  7. function choose_menu(tree) {
  8. if (tree)
  9. {
  10. menu=tree;
  11. if (menu.style=='default')
  12. {
  13. display_menu(ynh_url,f$,menu);
  14. }
  15. else
  16. {
  17. f$.getScript(ynh_url+'themes/'+menu.style+'/'+menu.style+'.js', function() {
  18. create_menu(ynh_url,f$,menu);
  19. });
  20. }
  21. }
  22. }
  23. f$.ajax({
  24. url: ynh_url+'nav.json',
  25. crossdomain: true,
  26. traditional: true,
  27. dataType: 'json',
  28. })
  29. .done(choose_menu);
  30. });
  31. });
  32. }
  33. function display_menu(ynh_url,f$,menu)
  34. {
  35. var create_html_menu=function () {
  36. function create_menu_from_tree(tree, level) {
  37. var html="";
  38. for (elt in tree)
  39. {
  40. elt=tree[elt];
  41. if (!elt.class) elt.class = '';
  42. if (!elt.tree || elt.tree.length==0)
  43. {
  44. if (elt.divider) {
  45. html+='<li class="divider"></li>';
  46. }
  47. else {
  48. html+='<li class="' + elt.class +'">';
  49. if (!elt.link)
  50. elt.link='#';
  51. html+='<a href="'+elt.link+'" ';
  52. if (elt.short_description && elt.description) html+='rel="popover" ';
  53. if (elt.short_description) html+='data-original-title="'+elt.short_description+'" ';
  54. if (elt.description) html+='data-content="'+elt.description+'" ';
  55. html+='>';
  56. if (elt.icon) html+='<i class="fa fa-fw fa-lg '+elt.icon+'" aria-hidden="true"></i>&nbsp';
  57. html+=elt.title;
  58. html+='</a>';
  59. html+='</li>';
  60. }
  61. }
  62. else if (level > 1) {
  63. html+='<li class="nav-subheader ' + elt.class + '">';
  64. html+='<a href="'+elt.link+'" ';
  65. if (elt.short_description && elt.description) html+='rel="popover" ';
  66. if (elt.short_description) html+='data-original-title="'+elt.short_description+'" ';
  67. if (elt.description) html+='data-content="'+elt.description+'" ';
  68. html+='>';
  69. if (elt.icon) html+='<i class="fa fa-fw fa-lg '+elt.icon+'" aria-hidden="true"></i>';
  70. if (elt.title && elt.icon) html+='&nbsp';
  71. if (elt.title) html+=elt.title;
  72. html+='</a>';
  73. html+='<ul>' + create_menu_from_tree(elt.tree, level + 1) + '</ul>';
  74. html+='</li>';
  75. }
  76. else
  77. {
  78. html+='<li class="dropdown ' + elt.class + '"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" ';
  79. if (elt.short_description) html+='data-original-title="'+elt.short_description+'" ';
  80. if (elt.description) html+='data-content="'+elt.description+'" ';
  81. html+='>';
  82. if (elt.icon) html+='<i class="fa fa-fw fa-lg '+elt.icon+'" aria-hidden="true"></i>';
  83. if (elt.title && elt.icon) html+='&nbsp';
  84. if (elt.title) html+=elt.title;
  85. html+='<span class="caret"></span></a>';
  86. html+='<ul class="dropdown-menu" role="menu">'+create_menu_from_tree(elt.tree, level + 1)+'</ul>';
  87. html+='</li>';
  88. }
  89. }
  90. return html
  91. }
  92. var html='<div style="height: 42px; position: fixed; width: 100%; top: 0px; z-index: 1000;" id="menu_ynh_container" class="bootstrap-scope hidden-print">'
  93. html+='<meta charset="utf-8">';
  94. html+='<nav class="navbar navbar-default navbar-fixed-top" id="menu_ynh" role="menubar"> ';
  95. html+='<button data-target=".navbar-ex1-collapse" data-toggle="collapse" class="navbar-toggle collapsed" type="button">';
  96. html+=' <span class="sr-only">Afficher/masquer le menu</span>';
  97. html+=' <span class="icon-bar"></span>';
  98. html+=' <span class="icon-bar"></span>';
  99. html+=' <span class="icon-bar"></span>';
  100. html+='</button>';
  101. html+=' <div class="nav-container">';
  102. if (menu.title)
  103. {
  104. html+=' <div class="navbar-header">';
  105. if (menu.link)
  106. html+=' <a class="navbar-brand" href="'+menu.link+'">';
  107. if (menu.image)
  108. {
  109. html+=' <img alt="'+menu.title+'" src="'+menu.image+'" style="height:36px; margin-top:3px;">';
  110. }
  111. else
  112. {
  113. html+=menu.title;
  114. }
  115. if (menu.link)
  116. html+=' </a>';
  117. html+=' <a id="nav-skip" href="#nav-end">Sauter le menu</a>';
  118. html+=' </div>';
  119. }
  120. html+=' <div class="collapse navbar-collapse navbar-ex1-collapse" id="menu_ynh">';
  121. html+=' <ul class="nav navbar-nav">';
  122. html+=create_menu_from_tree(menu.tree.filter(function (item)
  123. {
  124. return item.category!='right';
  125. }), 1);
  126. html+=' </ul>';
  127. html+=' <ul class="nav navbar-nav navbar-right">';
  128. html+=create_menu_from_tree(menu.tree.filter(function (item)
  129. {
  130. return item.category=='right';
  131. }), 1);
  132. html+=' </ul>';
  133. html+=' </div>';
  134. html+=' </div>';
  135. html+=' <a id="nav-end"></a>';
  136. html+='</nav>';
  137. html+='</div>';
  138. var body=f$('body').children().detach();
  139. html+='</div><div style="height: 42px;" /><div id="ynh_menu_content" style="position: absolute;width:100%;top:42px;bottom:0;" ></div>';
  140. f$('body').append(html);
  141. f$('#ynh_menu_content').append(body);
  142. if(typeof f$().popover == 'function') {
  143. f$('a[rel="popover"]').each(function() {
  144. f$(this).popover({
  145. html: true,
  146. trigger: 'hover',
  147. // utilisation de 'template' pour ajout du lien sur la popover en mode tactile
  148. template: '<div class="popover" role="tooltip"><div class="arrow"></div><a href="'+f$(this).attr('href')+'"><h3 class="popover-title"></h3><div class="popover-content"></div></a></div>'
  149. });
  150. });
  151. }
  152. var cssRuleCode = document.all ? 'rules' : 'cssRules';
  153. for (var i=0;i<document.styleSheets.length;i++)
  154. {
  155. var sheet =document.styleSheets[i];
  156. for (var j=0;j<sheet[cssRuleCode].length;j++)
  157. {
  158. var rule=sheet[cssRuleCode][j];
  159. if (rule.style['position']=='fixed')
  160. {
  161. var top=parseInt(rule.style['top']);
  162. if (!isNaN(top))
  163. {
  164. addCSSRule(sheet, rule.selectorText, 'top:'+(top+42)+'px',j+1);
  165. j++;
  166. }
  167. }
  168. }
  169. }
  170. };
  171. //ynh_loadCSS(ynh_url+'lib/font-awesome/css/font-awesome.min.css','end','all');
  172. ynh_loadCSS(ynh_url+"lib/bootstrap/css/bootstrap.min.css", 'end', "all");
  173. ynh_loadCSS(ynh_url+"lib/bootstrap/css/bootstrap-accessibility.css", 'end', "all");
  174. ynh_loadCSS(ynh_url+"lib/fork-awesome/css/fork-awesome.min.css", 'end', "all");
  175. ynh_loadCSS(ynh_url+'nav.css');
  176. var bootstrap_enabled = (window.jQuery && typeof window.jQuery().modal == 'function');
  177. if (bootstrap_enabled)
  178. {
  179. f$=window.jQuery;
  180. create_html_menu();
  181. }
  182. else
  183. {
  184. f$.getScript(ynh_url+'lib/bootstrap/js/fbootstrap.min.js',create_html_menu);
  185. }
  186. }
  187. function addCSSRule(sheet, selector, rules, index) {
  188. if("insertRule" in sheet) {
  189. sheet.insertRule(selector + "{" + rules + "}", index);
  190. }
  191. else if("addRule" in sheet) {
  192. sheet.addRule(selector, rules, index);
  193. }
  194. }
  195. // Fonction d'ajout de scripts
  196. function ynh_loadScript(url, callback, forceCallback) {
  197. if (!this.loadedScript) {
  198. this.loadedScript = new Array();
  199. }
  200. // indexOf n'existe pas pour IE8
  201. if (!Array.prototype.indexOf) {
  202. Array.prototype.indexOf = function(elt /*, from*/) {
  203. var len = this.length >>> 0;
  204. var from = Number(arguments[1]) || 0;
  205. from = (from < 0)
  206. ? Math.ceil(from)
  207. : Math.floor(from);
  208. if (from < 0)
  209. from += len;
  210. for (; from < len; from++) {
  211. if (from in this &&
  212. this[from] === elt)
  213. return from;
  214. }
  215. return -1;
  216. };
  217. }
  218. // fin teste indexOf
  219. if (this.loadedScript.indexOf(url) == -1) {
  220. this.loadedScript.push(url);
  221. var head = document.getElementsByTagName("head")[0];
  222. var e = document.createElement("script");
  223. e.src = url;
  224. e.type = "text/javascript";
  225. e.charset ="utf-8";
  226. var done = false;
  227. e.onload = e.onreadystatechange = function() {
  228. if ( !done && (!this.readyState ||
  229. this.readyState === "loaded" || this.readyState === "complete") ) {
  230. done = true;
  231. callback();
  232. // Handle memory leak in IE
  233. e.onload = e.onreadystatechange = null;
  234. }
  235. };
  236. head.appendChild(e);
  237. } else {
  238. if (forceCallback) { // pas utilisé
  239. callback();
  240. }
  241. }
  242. }
  243. // Ajout de CSS
  244. function ynh_loadCSS(url, position, media) {
  245. if (position==undefined) position='end';
  246. if (media==undefined) media='screen';
  247. var f$_link = document.createElement('link');
  248. f$_link.rel = "stylesheet";
  249. f$_link.media=media;
  250. f$_link.href= url;
  251. if (position == 'start') {
  252. document.getElementsByTagName('head')[0].insertBefore(f$_link, document.getElementsByTagName('head')[0].firstChild);
  253. } else {
  254. document.getElementsByTagName('head')[0].appendChild(f$_link);
  255. }
  256. }