3 * Copyright (C) 2015 "IoT.bzh"
4 * Author "Fulup Ar Foll"
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details..
22 var tmpl = '<tip-modal tip="tip"></tip-modal>' +
23 '<label for="{{name}}-intext">{{label}} <i ng-show="required" ng-click="ToBeDefined" ' +
24 'class="required {{status}} fi-checkbox" title="Free Value But Mandatory Argument" alt="?"> </i></label>'+
26 ' type="{{type}}" id="{{name}}-intext" placeholder="{{placeholder}}" class="status-{{status}}"'+
27 ' ng-model="value" ng-blur="validate()" ng-focus="selected()" '+
28 ' ng-model-options="{ updateOn: \'default blur\', debounce: {default: 500, blur: 0} }"' +
29 '><alert data-ng-show="!valid&&errmsg">{{errmsg}}</alert>';
31 var emailpatern = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
33 angular.module('InputText',['JQueryEmu'])
35 .directive('inputText', function(JQemu) {
36 function mymethods(scope, elem, attrs) {
38 // default value at 1st rendering
41 scope.status = 'untouch';
43 scope.input = elem.find ("input");
46 // requirer is use to increment requested counter
47 if ("required" in attrs) {
49 elem.addClass ("required");
52 // user enter input reset error status
53 scope.selected = function () {
56 scope.status = 'touch';
59 scope.validate = function () {
61 // get value from input field bypassing Angular ng-model
62 console.log ("Clicked InputText name=%s value=%s valid=%s", scope.name, scope.value, scope.valid);
64 // form is not untouched anymore
65 scope.parent.removeClass ("ng-pristine");
67 // if value not null clean up string
70 // remove leading and trailling space
71 scope.value = scope.value.trim();
73 // remove any space is not allowed
74 if ('nospace' in attrs) {
75 scope.value=scope.value.replace(/\s/g, '');
78 if ('lowercase' in attrs) {
79 scope.value = scope.value.toLowerCase();
82 // check minimum lenght
83 if ("minlen" in attrs) {
84 if (scope.value.length < attrs.minlen) {
85 scope.status='invalid';
86 scope.errmsg=scope.name + ': Mininum Lengh= ' + attrs.minlen + ' Characters';
91 if ('email' in attrs) {
92 if (!emailpatern.test (scope.value)) {
93 scope.status='invalid';
94 scope.errmsg='invalid email address';
100 if (scope.required) {
101 scope.status='invalid';
102 scope.errmsg=scope.name + ': Required Attribute';
107 // If local control fail let's refuse input
109 if (scope.required && scope.valid) {
111 if (scope.l4acounter.validated > 0) scope.l4acounter.validated --;
113 // use call to update form scope on form completeness
114 scope.callback (attrs.name, null, scope.done);
116 // localcheck is OK backup may nevertheless change status to false
117 if (scope.required && !scope.valid) scope.l4acounter.validated ++;
118 scope.status='valid';
120 scope.callback (attrs.name, scope.value, scope.done);
125 // this method can be called from controller to update widget status
126 scope.done=function (data) {
127 console.log ("Text-Input Callback ID="+ attrs.name + " data=", data);
128 for (var i in data) scope[i] = data[i];
131 // Export some attributes within directive scope for template
132 scope.label = attrs.label;
133 scope.name = attrs.name;
134 scope.placeholder = attrs.placeholder;
135 scope.type = attrs.type || "text";
136 scope.tip = attrs.tip;
138 // search for form within parent elemnts
139 scope.parent = JQemu.parent (elem, "FORM");
141 // email enforce lowercase and nospace
142 if ("email" in attrs) {
143 attrs.lowercase=true;
148 if (scope.required) {
149 scope.l4acounter = scope.parent.data ("l4acounter");
150 if (!scope.l4acounter) {
151 scope.l4acounter = {required:1, validated:0};
152 console.log("Field "+scope.name+" is required (1st)");
153 scope.parent.data ("l4acounter", scope.l4acounter);
155 console.log("Field "+scope.name+" is required");
156 scope.l4acounter.required ++;
160 // refresh validation each time controler update value
161 scope.$watch ('value', function(){
162 if(scope.value) scope.validate(); }
178 console.log ("InputText Loaded");