SWAPLANG_JSON.js 4.38 KB
Newer Older
Thitichaipun Wutthisak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

	setTarget = function() {
		var path = window.location.pathname;
		var lang = getLang().toUpperCase()||'ENG';
		var module ;
		file_name = 'JSON/CENTER_SWAPLANG.json';
		if(path.substring(0, path.lastIndexOf( "/" ))!=='/hr'){
			module = path.substring( path.indexOf( "/" ,1)+1, path.lastIndexOf( "/" ));
		//	console.log("module : "+module);
			if( module =='WORKFLOW' || module =='WORKFLOW_ADMIN' ){
				module = 'CENTER';
			}
			file_name = '../JSON/'+module+'_SWAPLANG.json';
		//	console.log("file_name : "+file_name);
		}
		return file_name;
	};

	setTargetBySite = function() {
		var swlang = getSwapLang().toUpperCase();
		if(swlang!=''){
			swlang = swlang+'/';
		}
		var path = window.location.pathname;
		var lang = getLang().toUpperCase()||'ENG';
		var module ;
		file_name_site = 'JSON/'+swlang+'CENTER_SWAPLANG.json';
		if(path.substring(0, path.lastIndexOf( "/" ))!=='/hr'){
			module = path.substring( path.indexOf( "/" ,1)+1, path.lastIndexOf( "/" ));
			if( module =='WORKFLOW' || module =='WORKFLOW_ADMIN' ){
				module = 'CENTER';
			}
			file_name_site = '../JSON/'+swlang+module+'_SWAPLANG.json';
		//	console.log("file_name_site : "+file_name_site);
		}
		return file_name_site;
	};

	setJSON = function() {
		jsonObj = null;
		if(window.ActiveXObject) {
		/*	objDOM = new ActiveXObject("Msxml.DOMDocument");
			objDOM.async = false;
			objDOM.load(file_name);	*/
		} else if(window.XMLHttpRequest) {
			var jsonDoc = new XMLHttpRequest();
			jsonDoc.open('GET', file_name ,false);
			try { jsonDoc.responseType = 'json'; } catch(e){}
			jsonDoc.send(null);
			jsonData = jsonDoc.response;
			jsonObj = JSON.parse(jsonData);
		//	console.log("jsonObj1 : "+jsonObj["SW100002"]["eng"]);
		}
		return jsonObj;
	};

	setJSONBySite = function() {
		jsonObj_site = null;
		if(file_name != file_name_site){
			if(window.ActiveXObject) {
			/*	objDOM = new ActiveXObject("Msxml.DOMDocument");
				objDOM.async = false;
				objDOM.load(file_name_site);	*/
			} else if(window.XMLHttpRequest) {
				var jsonDoc = new XMLHttpRequest();
				jsonDoc.open('GET', file_name_site ,false);
				try { jsonDoc.responseType = 'json'; } catch(e){}
				jsonDoc.send(null);
				jsonData = jsonDoc.response;
				jsonObj_site = JSON.parse(jsonData);
			//	console.log("jsonObj2 : "+jsonObj_site["SW100002"]["eng"]);
			}
		}
		return jsonObj_site;
	};

	var swaplang = function() {
		var file_name = setTarget();
		var file_name_site = setTargetBySite();
		var jsonObj = setJSON();
		var jsonObj_site = setJSONBySite();
	//	console.log("jsonObj : "+jsonObj);
	//	console.log("jsonObj_site : "+jsonObj_site);
	};

	swaplang.prototype.swap2String = function(code){
		var lang = getLang().toLowerCase()
		var result = "";
		try{
			if(jsonObj_site!=null){
				result = jsonObj_site[code][lang];
			}else{
				result = jsonObj[code][lang];
			}
		}catch(e){
			try{
				result = jsonObj[code][lang];
			}catch(e){
				console.log("can't find code : "+code+" : "+lang);
			}
		}
		return result;
	};

	swaplang.prototype.write = function(code) {
		return this.swap2String(code);
	};

	var mySwapLang = null;
	if(getLang()!=""){
		mySwapLang = new swaplang();
	}

	var myhr = angular.module('myHR', [])

	myhr.controller('Swaplang', swaplang);

  	myhr.directive('swlang', function() {
		var directive = {};
		directive.restrict = 'A';
		directive.template = "";
		directive.compile = function(element, attributes) {
		var linkFunction = function($scope, element, attributes) {
			if(mySwapLang==null){
				mySwapLang = new swaplang();
			}
			element.html(mySwapLang.swap2String(element.attr("code")));
		}
		return linkFunction;
		}
		return directive;
	});

	myhr.directive('swaplang', function() {
		var directive = {};
		directive.restrict = 'E';
		directive.template = "";
		directive.compile = function(element, attributes) {
		var linkFunction = function($scope, element, attributes) {
			if(mySwapLang==null){
				mySwapLang = new swaplang();
			}
			element.html(mySwapLang.swap2String(element.attr("code")));
		}
		return linkFunction;
		}
		return directive;
	});

	myhr.directive('swaplang2', function(){
		var directive = {};
		directive.restrict = 'E';
		directive.template = '';
		directive.compile = function(element, attributes){
			var linkfunc = function($scope, element, attributes){
				if (mySwapLang == null){
					mySwapLang = new swaplang();
				}
				element.html(mySwapLang.swap2String(attributes.code));
			}
			return linkfunc;
		}
		return directive;
	});