946bc83c024633465cda1c707898c9fc19ded880
[ldapsaisie.git] / public_html / includes / js / LSformElement_text_field.js
1 var LSformElement_text_field = new Class({
2     initialize: function(name,input,parent){
3       this._start = false;
4       this.name = name;
5       this.parent = parent;
6       this.input = input;
7       this.params = varLSdefault.LSjsConfig[this.name];
8       this._auto=1;
9       this.onChangeColor = '#f16d6d';
10       this.generatedValue = "";
11     },
12     
13     start: function() {
14       if (this._start) {
15         return true;
16       }
17       if ($type(this.params)) {
18         if ($type(this.params['generate_value_format'])) {
19           this.format = this.params['generate_value_format'];
20           this.oldBg=this.input.getStyle('background-color');
21           
22           this.fx = new Fx.Tween(this.input,{property: 'background-color',duration:600});
23           
24           // GenerateBtn
25           this.generateBtn = new Element('img');
26           this.generateBtn.addClass('btn');
27           this.generateBtn.src=varLSdefault.imagePath('generate.png');
28           this.generateBtn.addEvent('click',this.refreshValue.bind(this,true));
29           this.generateBtn.injectAfter(this.input);
30           varLSdefault.addHelpInfo(this.generateBtn,'LSformElement_text','generate');
31
32           // Auto
33           var force=0;
34           if (this.params.autoGenerateOnModify) {
35             force = 1;
36           }
37           this.isCreation = false;
38           if (this.input.value=="") {
39             this.isCreation = true;
40           }
41
42           if (((this.isCreation)&&(this.params.autoGenerateOnCreate))||(force)) {
43             this.dependsFields = this.parent.getDependsFields(this.format);
44             this.dependsFields.each(function(el) {
45               var input = this.parent.getInput.bind(this.parent)(el);
46               input.addEvent('change',this.refreshValue.bind(this));
47             },this);
48           }
49           this._start=true;
50         }
51       }
52     },
53     
54     getInput: function() {
55       return this.input;
56     },
57     
58     getValue: function() {
59       return this.input.value;
60     },
61     
62     refreshValue: function(force) {
63       if (force==true) {
64         this._auto=1;
65       }
66       if (((this._auto)||(force==true))&&((this.generatedValue=="")||(this.generatedValue==this.input.value)||(force==true))) {
67         var val=getFData(this.format,this.parent,'getValue');
68         if ($type(this.params['withoutAccent'])) {
69           if(this.params['withoutAccent']) {
70             val = replaceAccents(val);
71           }
72         }
73         if ($type(this.params['replaceSpaces'])) {
74           if(this.params['replaceSpaces']) {
75             val = replaceSpaces(val,this.params['replaceSpaces']);
76           }
77         }
78         if ($type(this.params['upperCase'])) {
79           if(this.params['upperCase']) {
80             val = val.toUpperCase();
81           }
82         }
83         if ($type(this.params['lowerCase'])) {
84           if(this.params['lowerCase']) {
85             val = val.toLowerCase();
86           }
87         }
88         this.input.value = val;
89         this.generatedValue = val;
90         this.fx.start(this.onChangeColor);
91         (function() {this.fx.start(this.oldBg);}).delay(1000,this);
92         this.input.fireEvent('change');
93       }
94     }
95 });