Commit d5455501 authored by Zéfling's avatar Zéfling 🎨
Browse files

Default language & little changes

- the first language is the default language
- add DOM actions
- more commentaries, more translated commentaries
- variables renaming
parent ec7882c3
baku.lg.add({en : {
baku.lg.add('en', {
monthNames : [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
dayNames : [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ],
math : {dot : '.', space : ","}
}});
number : {dot : '.', space : ","}
});
baku.lg.add({fr : {
baku.lg.add('fr', {
monthNames : ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
dayNames : ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'],
math : {dot : ',', space : "\u00A0"}
}});
number : {dot : ',', space : "\u00A0"}
});
baku.lg.add({ja : {
baku.lg.add('ja', {
monthNames : [ '1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月' ],
dayNames : [ '日曜日', '月曜日', '火曜日', '水曜日', '木曜日', '金曜日', '土曜日' ],
math : {dot : '.', space : ","}
}});
number : {dot : '.', space : ","}
});
baku.dom = {
id : function (id) { return document.elementById(id) },
first : function (selector) { return document.querySelector(selector) },
list : function (selector) { return document.querySelectorAll(selector) },
new : function (name) { return document.createElement(name) },
frag : function (name) { return document.createDocumentFragment(name) }
/**
* get a element by id ( id="xxx" )
* @param id the id element
* @return HTMLElement
*/
'id' : function (id) { return document.elementById(id) },
/**
* get a element by name ( name="xxx" )
* @param name the name element
* @return HTMLElement
*/
'name' : function (name) { return document.elementByName(name) },
/**
* get all elements that containts all classes list in params ( class="xxx yyy" )
* @param classes class list (separete with space)
* @return NodeList
*/
'class' : function (classes) { return document.elementByClassName(classes) },
/**
* get all elements that respect a selector
* @param selector CSS selector
* @return HTMLElement
*/
'first' : function (selector) { return document.querySelector(selector) },
/**
* get all elements that respect a selector
* @param selector CSS selector
* @return NodeList
*/
'list' : function (selector) { return document.querySelectorAll(selector) },
/**
* create a element
* @param name name element
* @return HTMLElement
*/
'new' : function (name) { return document.createElement(name) },
/**
* create de dument fragment
* @param name name element
* @return DOM fragment
*/
'frag' : function (name) { return document.createDocumentFragment(name) }
};
// copy Array object in DomArray
var DomArray = function() {};
for(var key in Array.prototype) {
DomArray.prototype[key] = Array.prototype[key];
}
/**
* ajout d'un style sur un élement.
* @get si value === undefined
* @set si value !== undefined || name est un object
* @param name nom du style ou liste de style {'nom' : 'valeur'}
* @param value valeur du style (facultatif)
* @return valeur pour @get, this pour @set
* add a style on the element.
* @get if value === undefined
* @set if value !== undefined || name is a object
* @param name style name or a style list {'name' : 'value'}
* @param value value of style (optional)
* @return a value if @get, 'this' if @set
*/
HTMLElement.prototype._css = function (name, value) {
if (typeof(name) !== 'object' && value === undefined) {
......@@ -41,9 +79,9 @@ HTMLElement.prototype._css = function (name, value) {
};
/**
* ajout d'une classe sur un élement
* @param name nom de classe ou une liste de classes
* @return l'élement
* add one or more classes on the element
* @param name name of class or a liste of classe. Ex. "class1 class2"
* @return the element
*/
HTMLElement.prototype._addClass = function (classes) {
if (baku.isString(classes)) {
......@@ -56,9 +94,9 @@ HTMLElement.prototype._addClass = function (classes) {
};
/**
* supprime d'une classe sur un élement
* @param name nom de classe ou une liste de classes
* @return l'élement
* remove one or more classes on element
* @param name name of class or a liste of classe. Ex. "class1 class2"
* @return the element
*/
HTMLElement.prototype._rmClass = function (classes) {
if (baku.isString(classes)) {
......@@ -71,10 +109,12 @@ HTMLElement.prototype._rmClass = function (classes) {
};
/**
* set les styles sur une liste d'élements
* @param name nom du style
* @param value valeur du style
* @return valeur ou undefined si non trouvée
* add a style on elements list.
* @get if value === undefined
* @set if value !== undefined || name is a object
* @param name style name or a style list {'name' : 'value'}
* @param value value of style (optional)
* @return the list
*/
NodeList.prototype._css =
DomArray.prototype._css = function (name, value) {
......@@ -87,9 +127,9 @@ DomArray.prototype._css = function (name, value) {
};
/**
* ajout d'une classe sur une liste d'élements
* @param name nom de classe
* @return la liste
* add one or more classes on the element list
* @param name name of class or a liste of classe. Ex. "class1 class2"
* @return the list
*/
NodeList.prototype._addClass =
DomArray.prototype._addClass = function (name) {
......@@ -102,9 +142,9 @@ DomArray.prototype._addClass = function (name) {
};
/**
* supprime d'une classe sur une liste d'élements
* @param name nom de classe
* @return la liste
* remove one or more classes on the element list
* @param name name of class or a liste of classe. Ex. "class1 class2"
* @return the list
*/
NodeList.prototype._rmClass =
DomArray.prototype._rmClass = function (name) {
......@@ -117,9 +157,9 @@ DomArray.prototype._rmClass = function (name) {
};
/**
* utilise le selector sur tout les élements de la liste et retour le premier
* @param selector nom de classe
* @return element
* use a selector on a list element and return the first found
* @param selector CSS selector
* @return the list
*/
NodeList.prototype._first =
DomArray.prototype._first = function (selector) {
......@@ -133,9 +173,9 @@ DomArray.prototype._first = function (selector) {
};
/**
* utilise le selector sur tout les élements de la liste et retourne une liste
* @param selector nom de classe
* @return la liste
* use a selector on a list element and return the list found
* @param selector CSS selector
* @return the list
*/
NodeList.prototype._list =
DomArray.prototype._list = function (selector) {
......
var baku = {};
baku.version = '0.1b';
/**
* get message by language
* @param lg language id
......@@ -10,16 +12,28 @@ baku.lg = function(lg, path) {
var text;
if (this.lg.lg[lg]) {
text = baku.get(this.lg.lg[lg], path.split('.'));
}
// if not found use the default language
if (text === undefined) {
text = baku.get(this.lg.lg[this.lg.default], path.split('.'));
}
return text;
};
/** language base */
baku.lg.lg = {};
/**
* add a language object
* @param lg object
*/
baku.lg.add = function (lg) {
this.lg = baku.extend(this.lg, lg);
* add or extend a language object
* @param id language id
* @param lg object this language informations
*/
baku.lg.add = function (id, lg) {
// first if the defaut language
if (!this.default) {
this.default = id;
}
this.lg[id] = this.lg[id] ? baku.extend(this.lg[id], lg) : lg;
};
/**
......
......@@ -21,15 +21,15 @@ Number.prototype._formatByPattern = function(pattern, params) {
lg = params.lg || navigator.language;
if(!pattern) {
// partern par défaut
// partern by default
pattern = '#,###';
}
var match = pattern.match(/\s*(?:((?:[#,]*)(?:[0,]*))(?:(?:\.([0 ]*#*))|))((?:\s*\%)|)\s*$/);
if(match && match[0] === pattern) {
var number = match[1];
var decimal = match[2] ? match[2].replace(/\s/, '').match(/^(0*)#*/) : null;
format.unit = match[3];
var number = match[1],
decimal = match[2] ? match[2].replace(/\s/, '').match(/^(0*)#*/) : null;
format.unit = match[3];
// digit grouping
format.groupingSize = number.match(/,?([#]*[0]*)$/)[1].length;
......@@ -44,10 +44,10 @@ Number.prototype._formatByPattern = function(pattern, params) {
throw 'patten error : '+pattern;
}
if (format.dot === undefined) {
format.dot = baku.lg(lg ,'math.dot');
format.dot = baku.lg(lg ,'number.dot');
}
if (format.space === undefined) {
format.space = baku.lg(lg, 'math.space');
format.space = baku.lg(lg, 'number.space');
}
return this._format(format);
};
......
......@@ -9,50 +9,50 @@ Formatter._parsePattern = /^\s*([^,]+)\s*(?:,\s*([^,]+)\s*(?:,\s*(.*)\s*)?)?$/;
* @return la chaine parsée
*/
Formatter._parse = function (str, func) {
var rtn = "", i = 0, op = 0, cl = 0, frag = {};
var text = "", i = 0, openTagCount = 0, closeTagCount = 0, fragment = {};
while ((i = str.indexOf('{' , i)) > -1) {
if (i === 0 || str[i-1] !== '\\') {
frag[i] = 1;
op++;
fragment[i] = 1;
openTagCount++;
}
i++;
}
i = 0;
while ((i = str.indexOf('}' , i)) > -1) {
if (i === 0 || str[i-1] !== '\\') {
frag[i] = -1;
cl++;
fragment[i] = -1;
closeTagCount++;
}
i++;
}
if (op === cl) {
if (openTagCount === closeTagCount) {
var ct = 0, j = 0, mrq = 0, tmp;
for(var pos in frag) {
pos = parseInt(pos);
if (frag[pos] === 1 && frag[pos] + ct === 1) {
rtn += str.substring(mrq, pos);
mrq = pos;
for(var position in fragment) {
position = parseInt(position);
if (fragment[position] === 1 && fragment[position] + ct === 1) {
text += str.substring(mrq, position);
mrq = position;
}
else if (frag[pos] === -1 && frag[pos] + ct === 0) {
tmp = str.substring(mrq + 1, pos);
else if (fragment[position] === -1 && fragment[position] + ct === 0) {
tmp = str.substring(mrq + 1, position);
if (tmp.match(Formatter._parsePattern)) {
rtn += tmp.replace(Formatter._parsePattern, func);
text += tmp.replace(Formatter._parsePattern, func);
}
else {
throw 'pattern error';
}
mrq = pos + 1;
mrq = position + 1;
j++;
}
frag[pos] += ct;
ct = frag[pos];
fragment[position] += ct;
ct = fragment[position];
}
rtn += str.substr(mrq);
text += str.substr(mrq);
}
else {
throw 'pattern error';
}
return rtn;
return text;
};
/**
......@@ -68,11 +68,20 @@ String.prototype._format = function (){
args = args[0];
}
return Formatter._parse(this, function (base, value, func, params) {
return (func !== undefined && typeof Formatter[func] === 'function')
? ( params !== undefined ? Formatter[func](args[value], args, params) : Formatter[func](args[value], args) )
: args[value];
}).replace('\\}', '}').replace('\\{', '{');
return Formatter._parse(this,
/**
* remplace the tag by a formated string
* @param base compled string respect the replace pattern (not used) (see : Formatter._parsePattern )
* @param key the key tag (ex. 0 for {0})
* @param func name of function (optional)
* @param params a paramter object (optional)
* @return string
*/
function (base, key, func, params) {
return (func !== undefined && typeof Formatter[func] === 'function')
? ( params !== undefined ? Formatter[func](args[key], args, params) : Formatter[func](args[key], args) )
: args[key];
}).replace('\\}', '}').replace('\\{', '{');
};
......
......@@ -10,4 +10,4 @@ elif [ "$1" = 'dom' ]; then
else
cat baku.js baku.math.js baku.number.js baku.date.js baku.string.js baku.string.formater.js | yui-compressor --type js -o "baku$name.min.js"
fi
sed -i '1i/*! BakuJs v0.1a | (c) Zefling | license, see: github.com/Zefling/BakuJS */' "baku$name.min.js"
\ No newline at end of file
sed -i '1i/*! BakuJs v0.1b | (c) Zefling | license, see: github.com/Zefling/BakuJS */' "baku$name.min.js"
\ No newline at end of file
......@@ -4,10 +4,10 @@
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>BakuJS - test</title>
<script type="text/javascript" src="baku.js"></script>
<script type="text/javascript" src="baku-lg/en.js"></script>
<script type="text/javascript" src="baku-lg/fr.js"></script>
<script type="text/javascript" src="baku-lg/en.js"></script>
<script type="text/javascript" src="baku-lg/fr.js"></script>
<script type="text/javascript" src="baku.date.js"></script>
<script type="text/javascript" src="baku.dom.js"></script>
<script type="text/javascript" src="baku.dom.js"></script>
<script type="text/javascript" src="baku.math.js"></script>
<script type="text/javascript" src="baku.number.js"></script>
<script type="text/javascript" src="baku.string.js"></script>
......
......@@ -157,7 +157,7 @@ window.onload = function(){
Test.equals("new Number(1000000)._formatByPattern('#,####', {lg : 'fr'})", '100\u00A00000', '1000000 + #,### → 100\u00A00000');
Test.equals("new Number(123456789.987654321)._formatByPattern('#,###.00', {lg : 'en'})", '123,456,789.99', '123456789.987654321 + #,###.00 → 123,456,789.99');
Test.equals("new Number(123456789.987654321)._formatByPattern('#,###.##', {lg : 'en'})", '123,456,789.99', '123456789.987654321 + #,###.## → 123,456,789.99');
Test.equals("new Number(123456789.987654321)._formatByPattern('#,###.##', {lg : 'xx'})", '123456789.99', '123456789.987654321 + #,###.## → 123456789.99');
Test.equals("new Number(123456789.987654321)._formatByPattern('#,###.##', {lg : 'xx'})", '123,456,789.99', '123456789.987654321 + #,###.## → 123,456,789.99');
Test.equals("new Number(123.123)._formatByPattern('0000.00', {lg : 'fr'})", '0123,12', '123.123 + #,### → 0123,12');
Test.equals("new Number(123.123)._formatByPattern('0000.0###', {lg : 'fr'})", '0123,123', '123.123 + #,### → 0123,123');
Test.equals("new Number(123.123)._formatByPattern('0000.0000', {lg : 'fr'})", '0123,1230', '123.123 + #,### → 0123,1230');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment