Browse Source

Merge branch 'master' of https://github.com/Omnimaga/omninet-control-panel

Julien Savard 10 years ago
parent
commit
37b5c1888d
2 changed files with 87 additions and 17 deletions
  1. 17 5
      site/index.php
  2. 70 12
      site/js/index.js

+ 17 - 5
site/index.php

@@ -49,16 +49,28 @@
 			<link href="//cdnjs.cloudflare.com/ajax/libs/authy-forms.css/2.0/form.authy.min.css" rel="stylesheet"/>
 		<?php } ?>
 		<link href="<?php echo HOSTNAME; ?>site/index.css" rel="stylesheet"/>
+		<script>
+			function runWhenExists(name){
+				var run = function(){
+					if(typeof window[name] != 'function'){
+						setTimeout(run,10);
+					}else{
+						window[name]();
+					}
+				};
+				run();
+			}
+		</script>
 		<?php
 			if($user){
-				echo "<script>$(function(){";
+				echo "<script>$(document).ready(function(){";
 				if(is_logged_in() && is_verified()){
-					echo "window.ServerPing();";
+					echo "runWhenExists('ServerPing');";
 				}
 				if(has_flag($user,'u')){
-					echo "window.FetchMemos();";
-					echo "window.FetchNews();";
-					echo "window.FetchChannels();";
+					echo "runWhenExists('FetchMemos');";
+					echo "runWhenExists('FetchNews');";
+					echo "runWhenExists('FetchChannels');";
 				}
 				echo "});</script>";
 				if(has_flag($user,'u')){ ?>

+ 70 - 12
site/js/index.js

@@ -39,12 +39,65 @@ $(function(){
 				dataType: 'json'
 			});
 		},
+		LANG,
 		lang = Pomo.load(
 			__HOSTNAME__+'site/api?action=lang',{
 				format: 'po',
 				mode: 'ajax'
 			}
-		);
+		),
+		lang_keys = (function(){
+			var keys = [];
+			$('body').find('*').contents().filter(function(){
+				return this.nodeType === 3;
+			}).each(function(){
+				keys.push({
+					node: this,
+					key: this.nodeValue
+				});
+			});
+			return keys;
+		})(),
+		has_key = function(node){
+			for(var i in lang_keys){
+				if(node === lang_keys[i].node){
+					return true;
+				}
+			}
+			return false;
+		},
+		get_key = function(node){
+			for(var i in lang_keys){
+				if(node === lang_keys[i].node){
+					return lang_keys[i].key;
+				}
+			}
+			return false;
+		},
+		translate = function(){
+			$('body').find('*').contents().filter(function(){
+				return this.nodeType === 3;
+			}).each(function(){
+				if(!has_key(this)){
+					lang_keys.push({
+						node: this,
+						key: this.nodeValue
+					});
+				}
+				this.nodeValue = _(get_key(this));
+			});
+			$('body').find('input[type=submit],input[type=button]').each(function(){
+				if(this.tagName == 'INPUT' && this.type == 'submit'){
+					if(!has_key(this)){
+						lang_keys.push({
+							node: this,
+							key: this.value
+						});
+					}
+					this.value = _(get_key(this));
+				}
+			});
+		};
 	lang.ready(function(){
 		$('script[id^=template-]').each(function(){
 			templates[this.id.substr(9)] = Handlebars.compile($(this).html());
@@ -264,17 +317,6 @@ $(function(){
 				History.pushState({},document.title,url.attr('path')+'?'+$.param(params)+url.attr('anchor'));
 			}
 		}).trigger('statechange').resize(function(){
-			$('body').find('*').contents().filter(function(){
-				return this.nodeType === 3;
-			}).each(function(){
-				console.log(_($(this).text()));
-				this.nodeValue = _($(this).text());
-			});
-			$('body').find('input[type=submit],input[type=button]').each(function(){
-				if(this.tagName == 'INPUT' && this.type == 'submit'){
-					this.value = _(this.value);
-				}
-			});
 			dialogs.each(function(){
 				var d = $(this);
 				if(d.dialog('isOpen')){
@@ -515,6 +557,22 @@ $(function(){
 				setTimeout(window.ServerPing,1000*60); // Every minute
 			}
 		};
+		setInterval(function(){
+			if(LANG != window.navigator.language){
+				console.log(_('Language change detected'));
+				LANG = window.navigator.language;
+				lang = Pomo.load(
+					__HOSTNAME__+'site/api?action=lang',{
+						format: 'po',
+						mode: 'ajax'
+					}
+				);
+				lang.ready(function(){
+					translate();
+				});
+			}
+		},1000);
+		translate();
 		$('body').resize();
 	});
 });