finished initial client side & server side language parsing methods; integrated preloading into ajaxify and server app.js

v1.18.x
psychobunny 12 years ago
parent b5274a0d91
commit 451ffafb9e

@ -2,6 +2,7 @@ var ajaxify = {};
(function ($) {
/*global app, templates, utils*/
var location = document.location || window.location,
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''),
@ -66,6 +67,8 @@ var ajaxify = {};
}, url, RELATIVE_PATH + "/" + url);
}
translator.load(tpl_url);
jQuery('#footer, #content').fadeOut(100);
templates.flush();
@ -93,7 +96,7 @@ var ajaxify = {};
}
return false;
}
};
$('document').ready(function () {
if (!window.history || !window.history.pushState) return; // no ajaxification for old browsers
@ -127,7 +130,7 @@ var ajaxify = {};
function nodeName(elem, name) {
return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
};
}
function evalScript(elem) {
var data = (elem.text || elem.textContent || elem.innerHTML || ""),
@ -149,7 +152,7 @@ var ajaxify = {};
head.insertBefore(script, head.firstChild);
//TODO: remove from head before inserting?, doing this breaks scripts in safari so commented out for now
//head.removeChild(script);
};
}
var scripts = [],
script,
@ -172,6 +175,6 @@ var ajaxify = {};
}
evalScript(scripts[i]);
}
};
}
}(jQuery));

@ -427,6 +427,8 @@ var socket,
}
jQuery('document').ready(function () {
translator.load('global');
$('#search-form').on('submit', function () {
var input = $(this).find('input');
ajaxify.go("search/" + input.val(), null, "search");

@ -2,33 +2,96 @@
"use strict";
/*global RELATIVE_PATH*/
/*
* TODO: language en is hardcoded while system is developed.
*/
var translator = {},
loaded = {};
files = {
loaded: {},
loading: {},
callbacks: {}
};
module.exports = translator;
translator.load = function (file, callback) {
if (loaded[file]) {
callback(loaded[file]);
translator.load = function (filename, callback) {
if (files.loaded[filename] && !files.loading[filename]) {
if (callback) {
callback(files.loaded[filename]);
}
} else if (files.loading[filename]) {
if (callback) {
files.callbacks[filename] = files.callbacks[filename] || [];
files.callbacks[filename].push(callback);
}
} else {
var timestamp = new Date().getTime(); //debug
jQuery.getJSON(RELATIVE_PATH + '/language/en/' + file + '.json?v=' + timestamp, function (language) {
loaded[file] = language;
files.loading[filename] = true;
jQuery.getJSON(RELATIVE_PATH + '/language/en/' + filename + '.json?v=' + timestamp, function (language) {
files.loaded[filename] = language;
if (callback) {
callback(language);
}
while (files.callbacks[filename] && files.callbacks[filename].length) {
files.callbacks[filename].pop()(language);
}
files.loading[filename] = false;
});
}
};
translator.loadAll = function (callback) {
var utils = require('./utils.js'),
path = require('path'),
fs = require('fs');
utils.walk(path.join(__dirname, '../../', 'public/language/en'), function (err, data) {
var loaded = data.length;
for (var d in data) {
if (data.hasOwnProperty(d)) {
(function (file) {
fs.readFile(file, function (err, json) {
files.loaded[path.basename(file).replace('json', '')] = json;
loaded--;
if (loaded === 0) {
callback();
}
});
}(data[d]));
}
}
});
};
/*
* TODO: DRY, see translator.translate. The hard part is to make sure both work node.js / js side
*/
translator.get = function (key) {
var parsedKey = key.split(':'),
languageFile = parsedKey[0];
parsedKey = parsedKey[1];
return files.loaded[languageFile][parsedKey];
};
/*
* TODO: Not fully converted to server side yet, ideally server should be able to parse whole templates on demand if necessary
* fix: translator.load should determine if server side and immediately return appropriate language file.
*/
translator.translate = function (data, callback) {
var keys = data.match(/\[\[.*?\]\]/g),
loading = 0;
for (var key in keys) {
if (keys.hasOwnProperty(key)) {
var parsedKey = keys[key].replace('[[', '').replace(']]', '').split(':'),
@ -36,8 +99,8 @@
parsedKey = parsedKey[1];
if (loaded[languageFile]) {
data = data.replace(keys[key], loaded[file][parsedKey]);
if (files.loaded[languageFile]) {
data = data.replace(keys[key], files.loaded[languageFile][parsedKey]);
} else {
loading++;
@ -51,12 +114,11 @@
}
}
}
checkComplete();
}
function checkComplete() {
if (loading === 0) {
callback(data);
}

Loading…
Cancel
Save