Commit 493740fd authored by Zéfling's avatar Zéfling 🎨

Merge branch 'gh-pages'

parents ec7882c3 33cbc2d6
#### netbeans ####
nbproject
#### backup #####
.bak
*.bak
*.kate-swp
*.min.js
#### generated files #####
.directory
# BakuJS
Quelques petites méthodes en plus, par ajout sur le prototype des objets de base :
## Date
- <code>Date._getMonthName()</code> : nom du mois
- <code>Date._getDayName()</code> : nom du jour de la semaine
- <code>Date._getDayOfYear()</code> : numéro du jour de l'année
- <code>Date._getWeek()</code> : numéro de semaine
- <code>Date._toStringFormat()</code> : formatage de la date
## Formatter
## Math
- <code>Math._roundDecimal()</code> : arrondir à une décimale précise
Some small functions by adding on the prototype of the base objects.
The idea is for the formatter is to approach the formatting possibilities of fmt:message in JSTL.
## Number
- <code>Number._formatByPattern()</code> : formatage par pattern
- <code>Number._format()</code> : formatage
## String
- <code>String._format()</code> : formateur {0, func, params}
- <code>String._padLeft()</code> : aggrandir une chaine par la gauche avec une chaine
- <code>String._padRight()</code> : aggrandir une chaine par la droite avec une chaine
## String : String.format() > Formatter
- choice : {0,choice,1# an|1> ans}
- number : {0, number, #,##0}
- date : {0,date, dd/MM/yyyy}
### Exemples :
### Examples :
```js
"Il y a {0, number, #,##0:fr} jour{0, choice,1<=|1>s}".format(1200); // Il y a 1 200 jours
"Il y a {jours, number, #,##0:fr} jour{jours, choice,1<=|1>s}".format({jours : 1}); // Il y a 1 jour
"Le resultat est {0, choice, 10<{1}|10>={2}}.".format([12, 'petit', 'grand']); // Le resultat est grand.
'Il y a {0, number, #,##0:fr} jour{0, choice,1<=|1>s}'._format(1200); // 'Il y a 1 200 jours'
'Il y a {jours, number, #,##0:fr} jour{jours, choice,1<=|1>s}'._format({jours : 1}); // 'Il y a 1 jour'
'Rendez-vous le {date, date, dd/MM/yyyy}'._format({date : '2016-04-10'}); // 'Rendez-vous le 10/04/2016'
'Le resultat est {0, choice, 10<{1}|10>={2}}.'._format([12, 'petit', 'grand']); // 'Le resultat est grand.'
```
## DOM
### Raccourcis
- <code>baku.dom.id = document.elementById</code>
- <code>baku.dom.first = document.querySelector</code>
- <code>baku.dom.list = document.querySelectorAll</code>
- <code>baku.dom.new = document.createElement</code>
### Aides
- <code>HTMLElement._css()</code> : ajout s'implifié de CSS sur un élement du DOM
- <code>HTMLElement._addClass()</code> : ajout s'implifié d'une classe sur un élement du DOM
- <code>HTMLElement._rmClass()</code> : ajout s'implifié d'une classe sur un élement du DOM
- <code>NodeList._css()</code> : ajout s'implifié de CSS sur une liste d'élements du DOM
- <code>NodeList._addClass()</code> : ajout s'implifié d'une classe sur une liste d'élements du DOM
- <code>NodeList._rmClass()</code> : suppression s'implifié d'une classe sur une liste d'élements du DOM
### Exemples
Some shortcuts to avoid using a big library to do simple operations on the DOM with selectors.
### Examples
```js
baku.dom.list('h2 + div')
._css({'color' : 'red', 'text-decoration' : 'underline'})
._rmClass('ko');
```
## Compilation
```bash
# dom + formatter
compress.sh full
# dom only
compress.sh dom
# formatter only
compress.sh
```
## Test
Voir la page de test : http://zefling.github.io/BakuJS/test.html
\ No newline at end of file
See the test page: http://test.ikilote.net/BakuJS/test.html
If you propose a correction or additional content, thank you to make all the tests are always ok, and/or add new.
\ No newline at end of file
......@@ -86,7 +86,7 @@ var _dateFormatters = {
h : [/(^|[^\\])h/ , function (d) {return d.getHours() > 12 ? d.getHours() % 12 : (d.getHours() === 0 ? 12 : d.getHours());}],
mm : [/(^|[^\\])mm/ , function (d) {return new String(d.getMinutes())._padLeft(2, '0');}],
m : [/(^|[^\\])m/ , function (d) {return d.getMinutes();}],
ss : [/(^|[^\\])ss/ , function (d) {return new Sring(d.getSeconds())._padLeft(2, '0');}],
ss : [/(^|[^\\])ss/ , function (d) {return new String(d.getSeconds())._padLeft(2, '0');}],
s : [/(^|[^\\])s/ , function (d) {return d.getSeconds();}],
SSS : [/(^|[^\\])S{3,}/ , function (d) {return new String(d.getMilliseconds())._padLeft(3, '0');}],
S : [/(^|[^\\])S+/ , function (d) {return new String(d.getMilliseconds());}]
......
/**
* BakuJs DOM shortcut library
*/
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.getElementById(id); },
/**
* get a elements by name ( name="xxx" )
* @param name the name element
* @return HTMLElement
*/
'name' : function (name) { return document.getElementsByName(name); },
/**
* get all elements that containts all classes list in params ( class="xxx yyy" )
* @param classes class list (separete with space)
* @param element base HTMLElement
* @return NodeList
*/
'class' : function (classes, element) { return (element || document).getElementsByClassName(classes); },
/**
* get all elements that respect a selector
* @param selector CSS selector
* @param element base HTMLElement
* @return HTMLCollection
*/
'first' : function (selector, element) { return (element || document).querySelector(selector); },
/**
* get all elements that respect a selector
* @param selector CSS selector
* @param element base HTMLElement
* @return HTMLElement or null with no result
*/
'list' : function (selector, element) { return (element || document).querySelectorAll(selector); },
/**
* create a element
* @param name name element
* @return NodeList
*/
'new' : function (name) { return document.createElement(name); },
/**
* create fragment document
* @param name name element
* @return DOM fragment
*/
'frag' : function (name) { return document.createDocumentFragment(name); },
/**
* create de text element
* @param text text
* @return DOM text
*/
'text' : function (text) { return document.createTextNode(text); }
};
// 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 value on object of element.
* @get if value === undefined
* @set if value !== undefined || name is a object
* @param attr attr name
* @param secure true is index exist, false for creation
* @param name index or a list {'name' : 'value'}
* @param value value (optional)
* @return a value if @get, 'this' if @set
*/
HTMLElement.prototype._css = function (name, value) {
Element.prototype._getInAttrObject = function (attr, secure, name, value) {
var list = this[attr];
if (!list) {
throw "It is not possible: " + attr + " not found.";
}
if (typeof(name) !== 'object' && value === undefined) {
if (this.style[name]) {
return this.style[name];
if (attr === 'attributes') {
this.getAttribute(key);
} else if (list[name]) {
return list[name];
}
} else {
var styles = {};
var attrs = {};
if (value === undefined) {
styles = name;
attrs = name;
} else {
styles[name] = value;
attrs[name] = value;
}
for(var key in styles) {
if (this.style[key] !== undefined) {
this.style[key] = styles[key];
for(var key in attrs) {
if (attr === 'attributes') {
this.setAttribute(key, attrs[key]);
} else if (!secure || secure && list[key] !== undefined) {
list[key] = attrs[key];
}
}
}
return this;
};
/**
* set or get 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
*/
Element.prototype._css = function (name, value) {
return this._getInAttrObject('style', true, name, value);
};
/**
* set or get a dataset on the element.
* @get if value === undefined
* @set if value !== undefined || name is a object
* @param name dataset name or a dataset list {'name' : 'value'}
* @param value value of dataset (optional)
* @return a value if @get, 'this' if @set
*/
Element.prototype._data = function (name, value) {
return this._getInAttrObject('dataset', false, name, value);
};
/**
* set or get a attribute on the element.
* @get if value === undefined
* @set if value !== undefined || name is a object
* @param name attribute name or a attribute list {'name' : 'value'}
* @param value value of attribute (optional)
* @return a value if @get, 'this' if @set
*/
Element.prototype._attr = function (name, value) {
return this._getInAttrObject('attributes', false, 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 list of classes. Ex. "class1 class2" or ["class1", "class2"]
* @return the element
*/
HTMLElement.prototype._addClass = function (classes) {
Element.prototype._addClass = function (classes) {
if (baku.isString(classes)) {
classes = classes.split(/\s+/);
}
......@@ -56,11 +153,11 @@ 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 list of classes. Ex. "class1 class2" or ["class1", "class2"]
* @return the element
*/
HTMLElement.prototype._rmClass = function (classes) {
Element.prototype._rmClass = function (classes) {
if (baku.isString(classes)) {
classes = classes.split(/\s+/);
}
......@@ -70,16 +167,89 @@ HTMLElement.prototype._rmClass = function (classes) {
return this;
};
/**
* remove one or more classes on element
* @param name name of class or a list of classes. Ex. "class1 class2" or ["class1", "class2"]
* @param active if true add classes, id false remove classes
* @return the element
*/
Element.prototype._toggleClass = function (classes, active) {
this[active ? '_addClass' : '_rmClass' ](classes);
return this;
};
/**
* get Id in a element
* @param id id
* @return node or undefined if not found
*/
Element.prototype._getById = function (id) {
var children = this.childNodes;
var l = children.length;
for (var i = 0; i < l; i++) {
if (children[i].id === id) {
return children[i];
} else if (children[i] instanceof Element) {
var node = children[i]._getById(id);
if (node instanceof Element) {
return node;
}
}
}
return undefined;
}
/**
* alias for getElementsByClassName
*/
Element.prototype._getByClass = function (selector) { return this.getElementsByClassName(selector) }
/**
* alias for querySelector
*/
Element.prototype._first = function (selector) { return this.querySelector(selector) }
/**
* alias for querySelectorAll
*/
Element.prototype._list = function (selector) { return this.querySelectorAll(selector) }
/**
* add event on element
* @param event name of event ('click', 'keyup', 'select' ...)
* @param func the function to attach from the element
* @param funcReturn if true returns function, else returns the element (default : false)
* @return the function or element
*/
Element.prototype._event = function (event, func, funcReturn) {
this.addEventLister(event, func, false);
return funcReturn ? this : func;
}
/**
* remove event on element
* @param event name of event ('click', 'keyup', 'select' ...)
* @param func function to be detached from the element
* @param funcReturn if true returns function, else returns the element (default : false)
* @return the function or element
*/
Element.prototype._rmEvent = function (event, func, funcReturn) {
this.removeEventListener(event, func, false);
return funcReturn ? this : func;
}
/**
* 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) {
for (var i in this) {
if (this[i] instanceof HTMLElement) {
if (this[i] instanceof Element) {
this[i]._css(name, value);
}
}
......@@ -87,14 +257,14 @@ 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 classes name of class or a list of classes. Ex. "class1 class2" or ["class1", "class2"]
* @return the list
*/
NodeList.prototype._addClass =
DomArray.prototype._addClass = function (name) {
NodeList.prototype._addClass =
DomArray.prototype._addClass = function (classes) {
for (var i in this) {
if (this[i] instanceof HTMLElement) {
if (this[i] instanceof Element) {
this[i]._addClass(name);
}
}
......@@ -102,14 +272,14 @@ 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 classes name of class or a list of classes. Ex. "class1 class2" or ["class1", "class2"]
* @return the list
*/
NodeList.prototype._rmClass =
DomArray.prototype._rmClass = function (name) {
NodeList.prototype._rmClass =
DomArray.prototype._rmClass = function (classes) {
for (var i in this) {
if (this[i] instanceof HTMLElement) {
if (this[i] instanceof Element) {
this[i]._rmClass(name);
}
}
......@@ -117,11 +287,53 @@ 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
* remove one or more classes on the element list
* @param classes name of class or a list of classes. Ex. "class1 class2" or ["class1", "class2"]
* @param active if true add classes, id false remove classes
* @return the list
*/
NodeList.prototype._toggleClass =
DomArray.prototype._toggleClass = function (classes, active) {
this[active ? '_addClass' : '_rmClass' ](classes);
return this;
};
/**
* add event on list of elements
* @param event name of event ('click', 'keyup', 'select' ...)
* @param func the function to attach from the list of elements
* @param funcReturn if true returns function, else returns the element (default : false)
* @return the function or the list of elements
*/
NodeList.prototype._event =
DomArray.prototype._event = function (event, func, modeReturn) {
for (var i in this) {
this[i].addEventLister(event, func, false);
}
return funcReturn ? this : func;
};
/**
* remove event on list of elements
* @param event name of event ('click', 'keyup', 'select' ...)
* @param func function to be detached from the list of elements
* @param funcReturn if true returns function, else returns the element (default : false)
* @return the function or the list of elements
*/
NodeList.prototype._first =
NodeList.prototype._rmEvent =
DomArray.prototype._event = function (event, func, modeReturn) {
for (var i in this) {
this[i].removeEventListener(event, func, false);
}
return funcReturn ? this : func;
};
/**
* 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) {
var e = new HTMLElement();
for (var i in this) {
......@@ -133,19 +345,19 @@ 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 =
NodeList.prototype._list =
DomArray.prototype._list = function (selector) {
var e, i, j, list = new DomArray();
for (i in this) {
if (this[i] instanceof HTMLElement) {
if (this[i] instanceof Element) {
e = this[i].querySelectorAll(selector);
if (e.length > 0) {
for(j in e) {
if (e[j] instanceof HTMLElement) {
if (e[j] instanceof Element) {
list[list.length] = e[j];
}
}
......@@ -154,5 +366,3 @@ DomArray.prototype._list = function (selector) {
}
return list;
};
var baku = {};
/**
* get message by language
* @param lg language id
* @param path path of message
* @return message or 'undefined' if not found
*/
baku.lg = function(lg, path) {
var text;
if (this.lg.lg[lg]) {
text = baku.get(this.lg.lg[lg], path.split('.'));
}
return text;
};
baku.version = '0.3 a2';
/**
* add a language object
* @param lg object
*/
baku.lg.add = function (lg) {
this.lg = baku.extend(this.lg, lg);
* test if it's a string
* @param string object test
* @return true is string is a string object
*/
baku.isString = function(string) {
return typeof(string) === 'string' || string instanceof String;
};
/**
* test if it's a string
* @param s object test
* @return true is s is a string object
* test if it's a array
* @param array object test
* @return true is s is a array object
*/
baku.isString= function(s) {
return typeof(s) === 'string' || s instanceof String;
baku.isArray = function(array) {
return typeof(array) === 'object' || array instanceof Array;
};
/**
......@@ -38,7 +27,7 @@ baku.isString= function(s) {
* @return merged object
*/
baku.extend = function (base, add) {
if (typeof(base) === 'object' && !(base instanceof Array)) {
if (typeof(base) === 'object' && !(base instanceof Array) && add) {
for(var i in add) {
base[i] = baku.extend(base[i], add[i]);
}
......@@ -63,4 +52,38 @@ baku.get = function (o, path) {
val = base;
}
return val;
};
\ No newline at end of file
};
/**
* get message by language
* @param lg language id
* @param path path of message
* @return message or 'undefined' if not found
*/
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 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;
};
......@@ -6,10 +6,10 @@ if (!Math.trunc) {
}
/**
* arrondi à la décimale choisie
* @param number nombre à arrondir
* @param decimalSize nombre de chiffres après la virgule
* @return la chaine formatée
* rounded to the chosen decimal
* @param number number to round
* @param decimalSize number of decimals after the dot
* @return rounded number
*/
Math._roundDecimal = function(number, decimalSize) {
return Math.round( +(number +'e'+ (decimalSize))) / +('1e'+decimalSize);
......
baku.number = {
/** default pattern for formatting */
formatDefautPattern :'#,###',
/**
* parse the number formatter pattern
* @param pattern formatter pattern (default: '#,###')
* @return formatted string number
*/
parse : function (pattern) {
if (!baku.isString(pattern)) {
throw 'patten error : is not a string';
}
var groupingSize = 0,
zeroDigitSize = 0,