f234266cf72a53f661613384601bbbe71b66ce72
[ccsdk/features.git] /
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this software except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 (function( $, app, joey ) {
17
18         var ui = app.ns("ui");
19
20         ui.AbstractField = ui.AbstractWidget.extend({
21
22                 defaults: {
23                         name : "",                      // (required) - name of the field
24                         require: false, // validation requirements (false, true, regexp, function)
25                         value: "",                      // default value
26                         label: ""                               // human readable label of this field
27                 },
28
29                 init: function(parent) {
30                         this._super();
31                         this.el = $.joey(this._main_template());
32                         this.field = this.el.find("[name="+this.config.name+"]");
33                         this.label = this.config.label;
34                         this.require = this.config.require;
35                         this.name = this.config.name;
36                         this.val( this.config.value );
37                         this.attach( parent );
38                 },
39
40                 val: function( val ) {
41                         if(val === undefined) {
42                                 return this.field.val();
43                         } else {
44                                 this.field.val( val );
45                                 return this;
46                         }
47                 },
48
49                 validate: function() {
50                         var val = this.val(), req = this.require;
51                         if( req === false ) {
52                                 return true;
53                         } else if( req === true ) {
54                                 return val.length > 0;
55                         } else if( req.test && $.isFunction(req.test) ) {
56                                 return req.test( val );
57                         } else if( $.isFunction(req) ) {
58                                 return req( val, this );
59                         }
60                 }
61
62         });
63
64 })( this.jQuery, this.app, this.joey );