LSformElement :: text : fixed bug breaking autoGenerateOnModify feature
[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');
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 inputs = varLSform.getInput.bind(this.parent)(el);
46               if (inputs.length>0) {
47                 inputs.each(function(input) {
48                   input.addEvent('change',this.refreshValue.bind(this));
49                 },this);
50               }
51             },this);
52           }
53           this._start=true;
54         }
55       }
56     },
57     
58     refreshValue: function(force) {
59       if (force==true) {
60         this._auto=1;
61       }
62       if (((this._auto)||(force==true))&&((this.generatedValue=="")||(this.generatedValue==this.input.value)||(force==true))) {
63         var val=getFData(this.format,varLSform,'getValue');
64         if ($type(this.params['withoutAccent'])) {
65           if(this.params['withoutAccent']) {
66             val = replaceAccents(val);
67           }
68         }
69         if ($type(this.params['replaceSpaces'])) {
70           if(this.params['replaceSpaces']) {
71             val = replaceSpaces(val,this.params['replaceSpaces']);
72           }
73         }
74         if ($type(this.params['upperCase'])) {
75           if(this.params['upperCase']) {
76             val = val.toUpperCase();
77           }
78         }
79         if ($type(this.params['lowerCase'])) {
80           if(this.params['lowerCase']) {
81             val = val.toLowerCase();
82           }
83         }
84         this.input.value = val;
85         this.generatedValue = val;
86         this.fx.start(this.onChangeColor);
87         (function() {this.fx.start(this.oldBg);}).delay(1000,this);
88         this.input.fireEvent('change');
89       }
90     }
91 });