157009cb56e815302bc93dce30881e922e45f06c
[src/app-framework-demo.git] / afb-client / app / Frontend / widgets / FormInput / InputPassword.js
1 /* 
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 (function() {
17 'use strict';
18
19 var tmpl = '<input-text  class="password" tip="{{tip1}}"  placeholder="{{place1}}"' +
20            'label="{{label1}}" callback="valid1" name="{{name}}-1" value="pass1" required minlen="{{minlen}}" type="password" >' +
21            '</input-text>' + 
22            '<input-text  class="password" tip="tip2"  placeholder="{{place2}}"' +
23            'label="{{label2}}" callback="valid2" name="{{name}}-2" value="pass2" required minlen="{{minlen}}" type="password" > '+
24            '</input-text>';
25
26 angular.module('InputPassword',[])
27
28 .directive('inputPassword', function() {
29     function mymethods(scope, elem, attrs) {
30     
31     scope.valid1 = function (name, value) {
32         console.log ("Clicked InputPassword1 name=%s value=%s", name, value);        
33         scope.firstpwd = value;
34     };
35     
36     scope.valid2 = function (name, value, done) {        
37         console.log ("Clicked InputPassword2 name=%s value=%s", name, value);        
38         
39         // if both passwd equal then call form CB
40         if (scope.firstpwd !== value) {
41           done({valid: false, status: 'invalid', errmsg: "both password should match"});  
42         } else {  
43           scope.callback (attrs.name, value);
44         }
45                   
46      };
47      
48      // this method can be called from controller to update widget status
49      scope.done=function (data) {
50        console.log ("Text-Input Callback ID="+ attrs.name + " data=", data);
51        for (var i in data) scope[i] = data[i];         
52      };
53      
54      // Export some attributes within directive scope for template
55      scope.name   = attrs.name;
56      scope.label1 = attrs.label || 'Password';
57      scope.label2 = attrs.label || 'Password Verification';
58      scope.place1 = attrs.placeholder1 || 'User Password';
59      scope.tip1   = attrs.tip || 'Choose a Password';
60      scope.place2 = attrs.placeholder1 || 'Password Verification';
61      scope.tip2   = attrs.tip    || 'Confirme your Password';
62      scope.minlen = attrs.minlen || 10;
63      
64      if ("required" in attrs) scope.required = 'required';
65          
66     }
67     
68     return {
69         restrict: 'E',
70         template: tmpl,
71         link: mymethods,
72         scope: {
73             callback : '=',
74         }
75     };
76 });
77
78 console.log ("InputPassword Loaded");
79 })();