sugoi internally added

This commit is contained in:
pvincent
2021-08-27 17:54:17 +04:00
parent ef31c26525
commit 445448f43b
100 changed files with 8758 additions and 23 deletions
+102
View File
@@ -0,0 +1,102 @@
package mt.net.helper;
import mt.Compat;
/**
* BreadCrumb (fil d'ariane)
*
* For breadcrumb translation,
* create "breadcrumb_*" entries in the translation file for each section.
*
* @author fbarbut
**/
class BreadCrumb{
public var lang : String;
public var sections : Array<String>; /* raw sections like "game/tetris/highscores" */
public var translation : Hash<String>; /* translated sections */
public function new(uri:String,?translator:String->String) {
sections = [];
translation = new Hash<String>();
//remove GET params
uri = uri.split('?')[0];
if( uri.substr(0,1) == "/" )
uri = uri.substr(1);
var uri = uri.split('/');
/*modify*/
for (i in 0...uri.length) {
var u = uri[i];
if(u == null)
continue;
if(Std.parseInt(u) != null) {
uri[i] = null;
continue;
}
if(u == 'index.n' || u == '') {
u = uri[i] = "home";
continue;
}
}
/*remove nulls*/
uri = Lambda.array(Lambda.filter(uri,function(e) return e!=null));
sections = uri;
/*translation*/
if(translator != null) {
for(s in sections) {
var name = translator("breadcrumb_" + s);
if(name != "#breadcrumb_" + s + "#") {
translation.set(s, name);
}else {
//no translation
translation.set(s, s);
}
}
}
}
public function toString(?mode=1, ?format:String):Dynamic {
switch(mode) {
case 1 :
/* raw sections */
return sections;
case 2 :
/* translated sections */
return getTranslatedSections();
}
return null;
//if(format != null) {
//format = StringTools.replace(format, "::rank::", Std.string(rank));
//format = StringTools.replace(format, "::suffix::", Std.string(suffix));
//return format;
//}else {
//return rank + suffix;
//}
}
public function getTranslatedSections() {
var out = [];
for(s in sections) {
out.push(translation.get(s));
}
return out;
}
}
+21
View File
@@ -0,0 +1,21 @@
package mt.net.helper;
class Helper {
/**
* Init all helpers in applicatio view.
*/
public static function init(context:Dynamic, viewHelpers:Array<Dynamic>) {
/* gros paté (c) ncannasse */
for(vh in viewHelpers) {
var methodName = Type.getClassName(Type.getClass(vh)).split(".").pop().toLowerCase();
var toString = Reflect.field(vh, "toString");
var nargs : Int = untyped $nargs(toString);
Reflect.setField(context, methodName , Reflect.makeVarArgs(function(p:Array<Dynamic>) {
while( p.length < nargs ) p.push(null);
return Reflect.callMethod(vh, toString, p);
}));
}
}
}
+113
View File
@@ -0,0 +1,113 @@
package mt.net.helper;
/**
* Ordinal number helper
*
* @author fbarbut
**/
class Ordinal /*implements IHelper*/{
public var lang : String;
public static var AVAILABLE_LANGS = ['fr','en','es'];
public function new(_lang:String) {
lang = _lang.toLowerCase();
if(!Lambda.has(AVAILABLE_LANGS, lang)) lang = 'en';
}
/**
* Get output, like "1st" or "2nd"
* @param rank number/rank starting from 1
* @param ?sex 1:male,2:female, 3: neutral (german)
* @param ?format text format like "::rank::<span>::suffix::</span>"
*/
public function toString(rank:Int, ?sex:Int, ?format:String):String {
var suffix = "";
if(sex == null) sex = 1;
switch(lang) {
case "fr":
if(rank == 1) {
suffix = (sex==1)?"er":"ère";
}else {
suffix = "ème";
}
case "es":
//http://reglasdeortografia.com/numerales.html
suffix = (sex == 1)?"o":"a";
case "de":
switch(sex) {
case 1 :
suffix = "ter";
case 2 :
suffix = "te";
default :
suffix = "tes";
}
default:
//"en":
if(rank == 1) {
suffix = "st";
}else if(rank == 2) {
suffix = "nd";
}else if(rank == 3) {
suffix = "rd";
}else {
suffix = "th";
}
/*DE
*
* Karsten :
Hihi. Un peu compliqué pour l'Allemand comme les suffix/mots dépendend de nombre exacte. ...en plus il y un troisième sexe.
Je propose tu utilise ce modèle qui fonctionne toujours: > X : X.
Voici le modèle complète :
> masculin:
> X = 1 : Erster
> X = 2 : Zweiter
> X = 3 : Dritter
> X = 4-19 : Xter
> X = 20-1000 : Xster
... + : Les derniers 3 chiffres décident de nouveau (comme indiqué anvant)
p.ex. 232456 = Y[232]X[456]
> neutre :
> X = 1 : Erstes
> X = 2 : Zweites
> X = 3 : Drittes
> X = 4-19 : Xtes
> X = 20-1000 : Xstes
... + : Les derniers 3 chiffres décident de nouveau (comme indiqué anvant)
p.ex. 232456 = Y[232]X[456]
> feminin :
> X = 1 : Erste
> X = 2 : Zweite
> X = 3 : Dritte
> X = 4-19 : Xte
> X = 20-1000 : Xste
... + : Les derniers 3 chiffres décident de nouveau (comme indiqué anvant)
p.ex. 232456 = Y[232]X[456]>
* */
}
if(format != null) {
format = StringTools.replace(format, "::rank::", Std.string(rank));
format = StringTools.replace(format, "::suffix::", Std.string(suffix));
return format;
}else {
return rank + suffix;
}
}
}
+8
View File
@@ -0,0 +1,8 @@
C'est quoi un helper ( ou view helper ? )
C'est un petit composant qui permet de rendre en html des éléments complexes, difficiles à rendre en macro templo.
Utiliser Helper.init() avec le Context d'une application et une liste de helpers pour les rendres disponible dans le Context de l'application web.
Inspiré de http://framework.zend.com/manual/1.12/en/zend.view.helpers.html
+166
View File
@@ -0,0 +1,166 @@
package sugoi.helper;
/**
* Simple class to print a HTML table
*
* @author fbarbut
*
* It should be able to print a lot of structures :
* - a spod request result
* - arrays, lists
* - anonymous objects
*
* usage :
* var t = new Table();
* t.title = "the table";
* t.setContent( [{toto:1,tata:2},{toto:4,tata:7}] );
* neko.Lib.print( t.toString(); );
**/
class Table{
//table content
public var title : String;
public var head : Array<String>;
public var content : Array<Array<Dynamic>>;
//table CSS class
public var tableCSSClass : String;
public function new(?defaultCss:String) {
if(defaultCss != null) tableCSSClass = defaultCss;
}
public function toString(?param:Dynamic):String {
if(param != null) {
setContent(param);
return go();
}else {
return "null";
}
}
public function go():String {
var output = "";
output += "<table " + (tableCSSClass != null?"class=\"" + tableCSSClass + "\"":"") +">";
if (title != null) {
var length = 0;
for(row in content) {
for(cell in row) {
length++;
}
break;
}
//alors là je comprends pas du tout du tout du tout pourquoi content[0].length ne marche pas...
//output += content[0]+ " "+Type.getClass(content[0])+" "+content[0].length ;
output += "<tr><th colspan='"+length+"'>" + title + "</th></tr>";
}
if (head != null) {
output += "<tr>";
for ( cell in head ) {
output += "<th>" + cell + "</th>";
}
output += "</tr>";
}
for (row in content) {
output += "<tr>";
for (cell in row) {
output += "<td>"+cell+"</td>";
}
output += "</tr>";
}
output += "</table>";
return output;
}
/**
* Iterable -> Array
*/
function fromIterableToArray<T>(iterable) {
var out = [];
var iterable : Iterable<T> = cast iterable;
for (el in iterable) {
out.push(el);
}
return out;
}
/**
* Reflectable -> Array
*/
function fromReflectableToArray<T>(reflectable) {
var out = [];
//get fields
for (field in Reflect.fields(reflectable) ) {
if(!Reflect.isFunction(Reflect.field(reflectable,field)) && field!="__cache__" && field!="__lock")
out.push(Reflect.field(reflectable,field));
}
return out;
}
public function toArray<T>(c:Dynamic) {
if (c.iterator!=null) { //Reflect.hasField(c, "iterator")
//trace("fromIterableToArray "+c);
return fromIterableToArray(c);
}else {
//trace("fromReflectableToArray "+c);
return fromReflectableToArray(c);
}
}
public function setContent<T>(c:Dynamic) {
//header
head = [];
//content
content = [];
var row = [];
//on essaye de voir si il y a une deuxieme dimension au tableau (ex : liste d'objets )
try{
for (obj in toArray(c) ) {
row = [];
for (prop in toArray(obj)) {
//get header
if(head.length==0){
for (prop in Reflect.fields(obj)) {
//if the field is not a function, lets add it :
if(!Reflect.isFunction(Reflect.field(obj,prop)) && prop!="__cache__")
head.push(prop);
}
}
row.push(prop);
}
content.push(row);
}
}catch(e :Dynamic) {
content = [];
head = ["field","value"];
//a priori c'est un objet simple pas une liste , donc on liste ses propriétés
for (field in Reflect.fields(c) ) {
var row = [];
if(!Reflect.isFunction(Reflect.field(c, field)) && field != "__cache__" && field != "__lock") {
row.push(field);
row.push(Reflect.field(c, field));
}
content.push(row);
}
}
return content;
}
}