PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/polyglot/2.2.0/js/jquery.polyglot.language.switcher.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 393 lines | 327 code | 28 blank | 38 comment | 70 complexity | 83c622d957c8fb2daae66d7bd29efe7a MD5 | raw file
  1. // JavaScript Document
  2. /* ---------------------------------------------------------------------- */
  3. /* "Polyglot" Language Switcher
  4. /* ----------------------------------------------------------------------
  5. Version: 2.2
  6. Author: Ixtendo
  7. Author URI: http://www.ixtendo.com
  8. License: MIT License
  9. License URI: http://www.opensource.org/licenses/mit-license.php
  10. ------------------------------------------------------------------------- */
  11. /**
  12. * jquery.timer.js
  13. *
  14. * Copyright (c) 2011 Jason Chavannes <jason.chavannes@gmail.com>
  15. *
  16. * http://jchavannes.com/jquery-timer
  17. *
  18. * Permission is hereby granted, free of charge, to any person
  19. * obtaining a copy of this software and associated documentation
  20. * files (the "Software"), to deal in the Software without
  21. * restriction, including without limitation the rights to use, copy,
  22. * modify, merge, publish, distribute, sublicense, and/or sell copies
  23. * of the Software, and to permit persons to whom the Software is
  24. * furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be
  27. * included in all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  32. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  33. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  34. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  35. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  36. * SOFTWARE.
  37. */
  38. (function ($) {
  39. //jquery.timer.js
  40. $.timer = function(func, time, autostart) {
  41. this.set = function(func, time, autostart) {
  42. this.init = true;
  43. if(typeof func == 'object') {
  44. var paramList = ['autostart', 'time'];
  45. for(var arg in paramList) {if(func[paramList[arg]] != undefined) {eval(paramList[arg] + " = func[paramList[arg]]");}};
  46. func = func.action;
  47. }
  48. if(typeof func == 'function') {this.action = func;}
  49. if(!isNaN(time)) {this.intervalTime = time;}
  50. if(autostart && !this.active) {
  51. this.active = true;
  52. this.setTimer();
  53. }
  54. return this;
  55. };
  56. this.once = function(time) {
  57. var timer = this;
  58. if(isNaN(time)) {time = 0;}
  59. window.setTimeout(function() {timer.action();}, time);
  60. return this;
  61. };
  62. this.play = function(reset) {
  63. if(!this.active) {
  64. if(reset) {this.setTimer();}
  65. else {this.setTimer(this.remaining);}
  66. this.active = true;
  67. }
  68. return this;
  69. };
  70. this.pause = function() {
  71. if(this.active) {
  72. this.active = false;
  73. this.remaining -= new Date() - this.last;
  74. this.clearTimer();
  75. }
  76. return this;
  77. };
  78. this.stop = function() {
  79. this.active = false;
  80. this.remaining = this.intervalTime;
  81. this.clearTimer();
  82. return this;
  83. };
  84. this.toggle = function(reset) {
  85. if(this.active) {this.pause();}
  86. else if(reset) {this.play(true);}
  87. else {this.play();}
  88. return this;
  89. };
  90. this.reset = function() {
  91. this.active = false;
  92. this.play(true);
  93. return this;
  94. };
  95. this.clearTimer = function() {
  96. window.clearTimeout(this.timeoutObject);
  97. };
  98. this.setTimer = function(time) {
  99. var timer = this;
  100. if(typeof this.action != 'function') {return;}
  101. if(isNaN(time)) {time = this.intervalTime;}
  102. this.remaining = time;
  103. this.last = new Date();
  104. this.clearTimer();
  105. this.timeoutObject = window.setTimeout(function() {timer.go();}, time);
  106. };
  107. this.go = function() {
  108. if(this.active) {
  109. this.action();
  110. this.setTimer();
  111. }
  112. };
  113. if(this.init) {
  114. return new $.timer(func, time, autostart);
  115. } else {
  116. this.set(func, time, autostart);
  117. return this;
  118. }
  119. };
  120. $.fn.polyglotLanguageSwitcher = function (op) {
  121. var ls = $.fn.polyglotLanguageSwitcher;
  122. var rootElement = $(this);
  123. var rootElementId = $(this).attr('id');
  124. var aElement;
  125. var ulElement = $("<ul class=\"dropdown\">");
  126. var length = 0;
  127. var isOpen = false;
  128. var liElements = [];
  129. var settings = $.extend({}, ls.defaults, op);
  130. var closePopupTimer;
  131. var isStaticWebSite = settings.websiteType == 'static';
  132. init();
  133. installListeners();
  134. function triggerEvent(evt) {
  135. if(settings[evt.name]){
  136. settings[evt.name].call($(this), evt);
  137. }
  138. }
  139. function open() {
  140. if(!isOpen){
  141. triggerEvent({name:'beforeOpen', element:rootElement, instance:ls});
  142. aElement.addClass("active");
  143. doAnimation(true);
  144. setTimeout(function () {
  145. isOpen = true;
  146. triggerEvent({name:'afterOpen', element:rootElement, instance:ls});
  147. }, 100);
  148. }
  149. }
  150. function close() {
  151. if(isOpen){
  152. triggerEvent({name:'beforeClose', element:rootElement, instance:ls});
  153. doAnimation(false);
  154. aElement.removeClass("active");
  155. isOpen = false;
  156. if (closePopupTimer && closePopupTimer.active) {
  157. closePopupTimer.clearTimer();
  158. }
  159. triggerEvent({name:'afterClose', element:rootElement, instance:ls});
  160. }
  161. }
  162. function suspendCloseAction() {
  163. if (closePopupTimer && closePopupTimer.active) {
  164. closePopupTimer.pause();
  165. }
  166. }
  167. function resumeCloseAction() {
  168. if (closePopupTimer) {
  169. closePopupTimer.play(false);
  170. }
  171. }
  172. function doAnimation(open) {
  173. if (settings.effect == 'fade') {
  174. if (open) {
  175. ulElement.fadeIn(settings.animSpeed);
  176. } else {
  177. ulElement.fadeOut(settings.animSpeed);
  178. }
  179. } else {
  180. if (open) {
  181. ulElement.slideDown(settings.animSpeed);
  182. } else {
  183. ulElement.slideUp(settings.animSpeed);
  184. }
  185. }
  186. }
  187. function doAction(item) {
  188. close();
  189. var selectedAElement = $(item).children(":first-child");
  190. var selectedId = $(selectedAElement).attr("id");
  191. var selectedText = $(selectedAElement).text();
  192. $(ulElement).children().each(function () {
  193. $(this).detach();
  194. });
  195. for (var i = 0; i < liElements.length; i++) {
  196. if ($(liElements[i]).children(":first-child").attr("id") != selectedId) {
  197. ulElement.append(liElements[i]);
  198. }
  199. }
  200. var innerSpanElement = aElement.children(":first-child");
  201. aElement.attr("id", selectedId);
  202. aElement.text(selectedText);
  203. aElement.append(innerSpanElement);
  204. }
  205. function installListeners() {
  206. $(document).click(function () {
  207. close();
  208. });
  209. $(document).keyup(function (e) {
  210. if (e.which == 27) {
  211. close();
  212. }
  213. });
  214. if (settings.openMode == 'hover') {
  215. closePopupTimer = $.timer(function () {
  216. close();
  217. });
  218. closePopupTimer.set({ time:settings.hoverTimeout, autostart:true });
  219. }
  220. }
  221. function init() {
  222. var selectedItem;
  223. var options = $("#" + rootElementId + " > form > select > option");
  224. if (isStaticWebSite) {
  225. var selectedId;
  226. var url = window.location.href;
  227. options.each(function(){
  228. var id = $(this).attr("id");
  229. if(url.indexOf('/'+id+'/')>=0){
  230. selectedId = id;
  231. }
  232. });
  233. }
  234. options.each(function () {
  235. var id = $(this).attr("id");
  236. var selected;
  237. if (isStaticWebSite) {
  238. selected = selectedId === id;
  239. }else{
  240. selected = $(this).attr("selected")
  241. }
  242. var liElement = toLiElement($(this));
  243. if (selected) {
  244. selectedItem = liElement;
  245. }
  246. liElements.push(liElement);
  247. if (length > 0) {
  248. ulElement.append(liElement);
  249. } else {
  250. aElement = $("<a id=\"" + $(this).attr("id") + "\" class=\"current\" href=\"#\">" + $(this).text() + " <span class=\"trigger\">&raquo;</span></a>");
  251. if (settings.openMode == 'hover') {
  252. aElement.hover(function () {
  253. open();
  254. suspendCloseAction();
  255. }, function () {
  256. resumeCloseAction();
  257. });
  258. } else {
  259. aElement.click(
  260. function () {
  261. open();
  262. }
  263. );
  264. }
  265. }
  266. length++;
  267. });
  268. $("#" + rootElementId + " form:first-child").remove();
  269. rootElement.append(aElement);
  270. rootElement.append(ulElement);
  271. if (selectedItem) {
  272. doAction(selectedItem);
  273. }
  274. }
  275. function toLiElement(option) {
  276. var id = $(option).attr("id");
  277. var value = $(option).attr("value");
  278. var text = $(option).text();
  279. var liElement;
  280. if (isStaticWebSite) {
  281. var url = window.location.href;
  282. var page = url.substring(url.lastIndexOf("/")+1);
  283. var urlPage = 'http://' + document.domain + '/' + settings.pagePrefix + id + '/' + page;
  284. liElement = $("<li><a id=\"" + id + "\" href=\"" + urlPage + "\">" + text + "</a></li>");
  285. } else {
  286. var href = document.URL.replace('#', '');
  287. var params = parseQueryString();
  288. params[settings.paramName] = value;
  289. if (href.indexOf('?') > 0) {
  290. href = href.substring(0, href.indexOf('?'));
  291. }
  292. href += toQueryString(params);
  293. liElement = $("<li><a id=\"" + id + "\" href=\"" + href + "\">" + text + "</a></li>");
  294. }
  295. liElement.bind('click', function () {
  296. triggerEvent({name:'onChange', selectedItem: $(this).children(":first").attr('id'), element:rootElement, instance:ls});
  297. doAction($(this));
  298. });
  299. if (settings.openMode == 'hover') {
  300. liElement.hover(function () {
  301. suspendCloseAction();
  302. }, function () {
  303. resumeCloseAction();
  304. });
  305. }
  306. return liElement;
  307. }
  308. function parseQueryString() {
  309. var params = {};
  310. var query = window.location.search.substr(1).split('&');
  311. if (query.length > 0) {
  312. for (var i = 0; i < query.length; ++i) {
  313. var p = query[i].split('=');
  314. if (p.length != 2) {
  315. continue;
  316. }
  317. params[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
  318. }
  319. }
  320. return params;
  321. }
  322. function toQueryString(params) {
  323. if (settings.testMode) {
  324. return '#';
  325. } else {
  326. var queryString = '?';
  327. var i = 0;
  328. for (var param in params) {
  329. var x = '';
  330. if (i > 0) {
  331. x = '&';
  332. }
  333. queryString += x + param + "=" + params[param];
  334. i++;
  335. }
  336. return queryString;
  337. }
  338. }
  339. ls.open = function () {
  340. open();
  341. };
  342. ls.close = function () {
  343. close();
  344. };
  345. triggerEvent({name:'afterLoad', element:rootElement, instance:ls});
  346. return ls;
  347. };
  348. var ls = $.fn.polyglotLanguageSwitcher;
  349. ls.defaults = {
  350. openMode:'click',
  351. hoverTimeout:1500,
  352. animSpeed:200,
  353. effect:'slide',
  354. paramName:'lang',
  355. pagePrefix:'',
  356. websiteType:'dynamic',
  357. testMode:false,
  358. onChange:NaN,
  359. afterLoad:NaN,
  360. beforeOpen:NaN,
  361. afterOpen:NaN,
  362. beforeClose:NaN,
  363. afterClose:NaN
  364. };
  365. })(jQuery);