scripts.js
378 lines
| 1 | /*-------------------------------------------------------------------------------* |
| 2 | * Script for onClick trigger functionality used by flag images |
| 3 | * Script modified from original GTranslate plugin created by Edvard Ananyan at http://edo.webmaster.am |
| 4 | * GTranslate Free Version is licensed under GNU/GPL license |
| 5 | *-------------------------------------------------------------------------------*/ |
| 6 | |
| 7 | function GLTFireEvent(lang_pair, lang_dest) { |
| 8 | try { |
| 9 | if (document.createEvent) { |
| 10 | var event = document.createEvent("HTMLEvents"); |
| 11 | event.initEvent(lang_dest, true, true); |
| 12 | lang_pair.dispatchEvent(event) |
| 13 | } else { |
| 14 | var event = document.createEventObject(); |
| 15 | lang_pair.fireEvent('on' + lang_dest, event) |
| 16 | } |
| 17 | } catch (e) {} |
| 18 | } |
| 19 | |
| 20 | function GLTGetCurrentLang() { |
| 21 | var keyValue = document.cookie.match('(^|;) ?googtrans=([^;]*)(;|$)'); |
| 22 | return keyValue ? keyValue[2].split('/')[2] : null; |
| 23 | } |
| 24 | |
| 25 | function doGoogleLanguageTranslator(lang_pair) { |
| 26 | if(window.glt_request_uri) |
| 27 | return true; |
| 28 | |
| 29 | if(lang_pair.value) |
| 30 | lang_pair = lang_pair.value; |
| 31 | |
| 32 | if(lang_pair == '') |
| 33 | return; |
| 34 | |
| 35 | var lang = lang_pair.split('|')[1]; |
| 36 | |
| 37 | if(GLTGetCurrentLang() == null && lang == lang_pair.split('|')[0]) |
| 38 | return; |
| 39 | |
| 40 | var teCombo = document.querySelector('select.goog-te-combo'); |
| 41 | var teWrapper = document.getElementById('google_language_translator'); |
| 42 | |
| 43 | if(teWrapper == null || teWrapper.innerHTML.length == 0 || teCombo == null || teCombo.innerHTML.length == 0) { |
| 44 | setTimeout(function(){doGoogleLanguageTranslator(lang_pair)}, 500); |
| 45 | } else { |
| 46 | teCombo.value = lang; |
| 47 | GLTFireEvent(teCombo,'change');GLTFireEvent(teCombo,'change'); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | jQuery(document).ready(function($) { |
| 52 | $('#glt-translate-trigger,#glt-translate-trigger font').toolbar({ |
| 53 | content: '#flags', |
| 54 | position: 'top', |
| 55 | hideOnClick: true, |
| 56 | event: 'click', |
| 57 | style: 'primary' |
| 58 | }); |
| 59 | |
| 60 | $('#glt-translate-trigger').on('toolbarItemClick',function(event) { |
| 61 | $(this).removeClass('pressed'); |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | /** |
| 66 | * Toolbar.js |
| 67 | * |
| 68 | * @fileoverview jQuery plugin that creates tooltip style toolbars. |
| 69 | * @link http://paulkinzett.github.com/toolbar/ |
| 70 | * @author Paul Kinzett (http://kinzett.co.nz/) |
| 71 | * @version 1.1.0 |
| 72 | * @requires jQuery 1.7+ |
| 73 | * |
| 74 | * @license jQuery Toolbar Plugin v1.1.0 |
| 75 | * http://paulkinzett.github.com/toolbar/ |
| 76 | * Copyright 2013 - 2015 Paul Kinzett (http://kinzett.co.nz/) |
| 77 | * Released under the MIT license. |
| 78 | * <https://raw.github.com/paulkinzett/toolbar/master/LICENSE.txt> |
| 79 | */ |
| 80 | |
| 81 | if ( typeof Object.create !== 'function' ) { |
| 82 | Object.create = function( obj ) { |
| 83 | function F() {} |
| 84 | F.prototype = obj; |
| 85 | return new F(); |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | (function( $, window, document, undefined ) { |
| 90 | |
| 91 | var ToolBar = { |
| 92 | init: function( options, elem ) { |
| 93 | var self = this; |
| 94 | self.elem = elem; |
| 95 | self.$elem = $( elem ); |
| 96 | self.options = $.extend( {}, $.fn.toolbar.options, options ); |
| 97 | self.metadata = self.$elem.data(); |
| 98 | self.overrideOptions(); |
| 99 | self.toolbar = $('<div class="tool-container" />') |
| 100 | .addClass('tool-'+self.options.position) |
| 101 | .addClass('toolbar-'+self.options.style) |
| 102 | .append('<div class="tool-items" />') |
| 103 | .append('<div class="arrow" />') |
| 104 | .appendTo('body') |
| 105 | .css('opacity', 0) |
| 106 | .hide(); |
| 107 | self.toolbar_arrow = self.toolbar.find('.arrow'); |
| 108 | self.initializeToolbar(); |
| 109 | }, |
| 110 | |
| 111 | overrideOptions: function() { |
| 112 | var self = this; |
| 113 | |
| 114 | $.each( self.options, function( $option ) { |
| 115 | if (typeof(self.$elem.data('toolbar-'+$option)) != "undefined") { |
| 116 | self.options[$option] = self.$elem.data('toolbar-'+$option); |
| 117 | } |
| 118 | }); |
| 119 | }, |
| 120 | |
| 121 | initializeToolbar: function() { |
| 122 | var self = this; |
| 123 | self.populateContent(); |
| 124 | self.setTrigger(); |
| 125 | self.toolbarWidth = self.toolbar.width(); |
| 126 | }, |
| 127 | |
| 128 | setTrigger: function() { |
| 129 | var self = this; |
| 130 | |
| 131 | if (self.options.event == 'onload') { |
| 132 | $(window).load(function(event) { |
| 133 | event.preventDefault(); |
| 134 | self.show(); |
| 135 | }); |
| 136 | } |
| 137 | |
| 138 | if (self.options.event == 'click') { |
| 139 | self.$elem.on('click', function(event) { |
| 140 | event.preventDefault(); |
| 141 | if(self.$elem.hasClass('pressed')) { |
| 142 | self.hide(); |
| 143 | } else { |
| 144 | self.show(); |
| 145 | } |
| 146 | }); |
| 147 | |
| 148 | if (self.options.hideOnClick) { |
| 149 | $('html').on("click.toolbar", function ( event ) { |
| 150 | if (event.target != self.elem && |
| 151 | self.$elem.has(event.target).length === 0 && |
| 152 | self.toolbar.has(event.target).length === 0 && |
| 153 | self.toolbar.is(":visible")) { |
| 154 | self.hide(); |
| 155 | } |
| 156 | }); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (self.options.hover) { |
| 161 | var moveTime; |
| 162 | |
| 163 | function decideTimeout () { |
| 164 | if (self.$elem.hasClass('pressed')) { |
| 165 | moveTime = setTimeout(function() { |
| 166 | self.hide(); |
| 167 | }, 150); |
| 168 | } else { |
| 169 | clearTimeout(moveTime); |
| 170 | }; |
| 171 | }; |
| 172 | |
| 173 | self.$elem.on({ |
| 174 | mouseenter: function(event) { |
| 175 | if (self.$elem.hasClass('pressed')) { |
| 176 | clearTimeout(moveTime); |
| 177 | } else { |
| 178 | self.show(); |
| 179 | } |
| 180 | } |
| 181 | }); |
| 182 | |
| 183 | self.$elem.parent().on({ |
| 184 | mouseleave: function(event){ decideTimeout(); } |
| 185 | }); |
| 186 | |
| 187 | $('.tool-container').on({ |
| 188 | mouseenter: function(event){ clearTimeout(moveTime); }, |
| 189 | mouseleave: function(event){ decideTimeout(); } |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | $(window).resize(function( event ) { |
| 194 | event.stopPropagation(); |
| 195 | |
| 196 | if ( self.toolbar.is(":visible") ) { |
| 197 | self.toolbarCss = self.getCoordinates(self.options.position, 20); |
| 198 | self.collisionDetection(); |
| 199 | self.toolbar.css( self.toolbarCss ); |
| 200 | self.toolbar_arrow.css( self.arrowCss ); |
| 201 | } |
| 202 | }); |
| 203 | }, |
| 204 | |
| 205 | populateContent: function() { |
| 206 | var self = this; |
| 207 | var location = self.toolbar.find('.tool-items'); |
| 208 | var content = $(self.options.content).clone( true ).find('a').addClass('tool-item'); |
| 209 | |
| 210 | location.html(content); |
| 211 | location.find('.tool-item').on('click', function(event) { |
| 212 | if(typeof window.glt_request_uri == 'undefined') |
| 213 | event.preventDefault(); |
| 214 | self.$elem.trigger('toolbarItemClick', this); |
| 215 | }); |
| 216 | }, |
| 217 | |
| 218 | calculatePosition: function() { |
| 219 | var self = this; |
| 220 | self.arrowCss = {}; |
| 221 | self.toolbarCss = self.getCoordinates(self.options.position, self.options.adjustment); |
| 222 | self.toolbarCss.position = 'fixed'; |
| 223 | self.toolbarCss.zIndex = self.options.zIndex; |
| 224 | self.collisionDetection(); |
| 225 | self.toolbar.css(self.toolbarCss); |
| 226 | self.toolbar_arrow.css(self.arrowCss); |
| 227 | }, |
| 228 | |
| 229 | getCoordinates: function( position, adjustment ) { |
| 230 | var self = this; |
| 231 | |
| 232 | self.coordinates = self.$elem.offset(); |
| 233 | |
| 234 | if (self.options.adjustment && self.options.adjustment[self.options.position]) { |
| 235 | adjustment = self.options.adjustment[self.options.position] + adjustment; |
| 236 | } |
| 237 | |
| 238 | switch(self.options.position) { |
| 239 | case 'top': |
| 240 | return { |
| 241 | left: self.coordinates.left-(self.toolbar.width()/2)+(self.$elem.outerWidth()/2), |
| 242 | top: self.coordinates.top-self.$elem.outerHeight()-adjustment, |
| 243 | right: 'auto' |
| 244 | }; |
| 245 | |
| 246 | case 'left': |
| 247 | return { |
| 248 | left: self.coordinates.left-(self.toolbar.width()/2)-(self.$elem.outerWidth()/2)-adjustment, |
| 249 | top: self.coordinates.top-(self.toolbar.height()/2)+(self.$elem.outerHeight()/2), |
| 250 | right: 'auto' |
| 251 | }; |
| 252 | |
| 253 | case 'right': |
| 254 | return { |
| 255 | left: self.coordinates.left+(self.toolbar.width()/2)+(self.$elem.outerWidth()/2)+adjustment, |
| 256 | top: self.coordinates.top-(self.toolbar.height()/2)+(self.$elem.outerHeight()/2), |
| 257 | right: 'auto' |
| 258 | }; |
| 259 | |
| 260 | case 'bottom': |
| 261 | return { |
| 262 | left: self.coordinates.left-(self.toolbar.width()/2)+(self.$elem.outerWidth()/2), |
| 263 | top: self.coordinates.top+self.$elem.outerHeight()+adjustment, |
| 264 | right: 'auto' |
| 265 | }; |
| 266 | } |
| 267 | }, |
| 268 | |
| 269 | collisionDetection: function() { |
| 270 | var self = this; |
| 271 | var edgeOffset = 20; |
| 272 | |
| 273 | if(self.options.position == 'top' || self.options.position == 'bottom') { |
| 274 | self.arrowCss = {left: '50%', right: '50%'}; |
| 275 | if( self.toolbarCss.left < edgeOffset ) { |
| 276 | self.toolbarCss.left = edgeOffset; |
| 277 | self.arrowCss.left = self.$elem.offset().left + self.$elem.width()/2-(edgeOffset); |
| 278 | } |
| 279 | |
| 280 | else if(($(window).width() - (self.toolbarCss.left + self.toolbarWidth)) < edgeOffset) { |
| 281 | self.toolbarCss.right = edgeOffset; |
| 282 | self.toolbarCss.left = 'auto'; |
| 283 | self.arrowCss.left = 'auto'; |
| 284 | self.arrowCss.right = ($(window).width()-self.$elem.offset().left)-(self.$elem.width()/2)-(edgeOffset)-5; |
| 285 | } |
| 286 | } |
| 287 | }, |
| 288 | |
| 289 | show: function() { |
| 290 | var self = this; |
| 291 | self.$elem.addClass('pressed'); |
| 292 | self.calculatePosition(); |
| 293 | self.toolbar.show().css({'opacity': 1}).addClass('animate-'+self.options.animation); |
| 294 | self.$elem.trigger('toolbarShown'); |
| 295 | }, |
| 296 | |
| 297 | hide: function() { |
| 298 | var self = this; |
| 299 | var animation = {'opacity': 0}; |
| 300 | self.$elem.removeClass('pressed'); |
| 301 | switch(self.options.position) { |
| 302 | case 'top': |
| 303 | animation.top = '+=20'; |
| 304 | break; |
| 305 | case 'left': |
| 306 | animation.left = '+=20'; |
| 307 | break; |
| 308 | case 'right': |
| 309 | animation.left = '-=20'; |
| 310 | break; |
| 311 | case 'bottom': |
| 312 | animation.top = '-=20'; |
| 313 | break; |
| 314 | } |
| 315 | self.toolbar.animate(animation, 200, function() { |
| 316 | self.toolbar.hide(); |
| 317 | }); |
| 318 | self.$elem.trigger('toolbarHidden'); |
| 319 | }, |
| 320 | |
| 321 | getToolbarElement: function () { |
| 322 | return this.toolbar.find('.tool-items'); |
| 323 | } |
| 324 | }; |
| 325 | |
| 326 | $.fn.toolbar = function( options ) { |
| 327 | if ($.isPlainObject( options )) { |
| 328 | return this.each(function() { |
| 329 | var toolbarObj = Object.create( ToolBar ); |
| 330 | toolbarObj.init( options, this ); |
| 331 | $(this).data('toolbarObj', toolbarObj); |
| 332 | }); |
| 333 | } else if ( typeof options === 'string' && options.indexOf('_') !== 0 ) { |
| 334 | var toolbarObj = $(this).data('toolbarObj'); |
| 335 | var method = toolbarObj[options]; |
| 336 | return method.apply(toolbarObj, $.makeArray(arguments).slice(1)); |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | $.fn.toolbar.options = { |
| 341 | content: '#myContent', |
| 342 | position: 'top', |
| 343 | hideOnClick: false, |
| 344 | zIndex: 120, |
| 345 | hover: false, |
| 346 | style: 'default', |
| 347 | animation: 'standard', |
| 348 | adjustment: 10 |
| 349 | }; |
| 350 | }) ( jQuery, window, document ); |
| 351 | |
| 352 | jQuery(function($) { |
| 353 | $('#flags a, a.single-language, .tool-items a').each(function() { |
| 354 | $(this).attr('data-lang', $(this).attr('title')); |
| 355 | }); |
| 356 | |
| 357 | $(document.body).on("click", "a.flag", function() { |
| 358 | lang_text = $(this).attr('data-lang'); |
| 359 | default_lang = window.glt_default_lang || $('#google_language_translator').attr('class').split("-").pop(); |
| 360 | lang_prefix = $(this).attr("class").split(" ")[2]; |
| 361 | lang_prefix == default_lang ? l() : n(); |
| 362 | function l() { |
| 363 | doGoogleLanguageTranslator(default_lang + "|" + default_lang); |
| 364 | } |
| 365 | function n() { |
| 366 | doGoogleLanguageTranslator(default_lang + "|" + lang_prefix); |
| 367 | } |
| 368 | $(".tool-container").hide(); |
| 369 | }); |
| 370 | |
| 371 | if(window.glt_request_uri) { |
| 372 | $('#google_language_translator select').on('change', function() { |
| 373 | doGLTTranslate($(this).val()); |
| 374 | }) |
| 375 | } |
| 376 | }); |
| 377 | |
| 378 |