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