Added actions support related files.
[ajatus.git] / js / widgets / text.js
blob05630ce4af0f81239692617db85db3d6ae01ec4a
1 /*
2  * This file is part of
3  *
4  * Ajatus - Distributed CRM
5  * @requires jQuery v1.2.1
6  * 
7  * Copyright (c) 2007 Jerry Jalava <jerry.jalava@gmail.com>
8  * Copyright (c) 2007 Nemein Oy <http://nemein.com>
9  * Website: http://ajatus.info
10  * Licensed under the GPL license
11  * http://www.gnu.org/licenses/gpl.html
12  * 
13  */
15 (function($){    
16     $.ajatus = $.ajatus || {};
17     $.ajatus.widgets = $.ajatus.widgets || {};
18     $.ajatus.widgets.core = typeof($.ajatus.widgets.core) == 'undefined' ? {} : $.ajatus.widgets.core;
19     
20     $.ajatus.widgets.core.text = {
21         name: 'text',
22         settings: {
23             max_length: 0,
24             width: 0,
25             as_json: false,
26             value_prefix: '',
27             value_suffix: ''
28         },
29         
30         get_create_tpl: function(name, default_value)
31         {
32             return [
33                 'input', { type: 'hidden', name: 'widget['+name+':name]', value: this.name }, '',
34                 'input', { type: 'hidden', name: 'widget['+name+':config]', value: $.ajatus.converter.toJSON(this.settings) }, '',
35                 'input', { type: 'hidden', name: 'widget['+name+':required]', value: this.required }, '',
36                 'span', { className: 'value_prefix' }, $.ajatus.i10n.get(this.settings.value_prefix),
37                 'input', { type: 'text', className: 'text', name: name, value: default_value }, '',
38                 'span', { className: 'value_suffix' }, $.ajatus.i10n.get(this.settings.value_suffix)
39             ];
40         },
41         get_edit_tpl: function(name, data)
42         {            
43             data.val = this.value_on_edit(data.val);
44             return [
45                 'input', { type: 'hidden', name: 'widget['+name+':name]', value: this.name }, '',
46                 'input', { type: 'hidden', name: 'widget['+name+':config]', value: $.ajatus.converter.toJSON(this.settings) }, '',
47                 'input', { type: 'hidden', name: 'widget['+name+':required]', value: this.required }, '',
48                 'input', { type: 'hidden', name: 'widget['+name+':prev_val]', value: data.val != '' ? $.ajatus.converter.toJSON(data.val): '' }, '',
49                 'span', { className: 'value_prefix' }, $.ajatus.i10n.get(this.settings.value_prefix),
50                 'input', { type: 'text', className: 'text', name: name, value: data.val }, '',
51                 'span', { className: 'value_suffix' }, $.ajatus.i10n.get(this.settings.value_suffix)
52             ];
53         },
54         get_view_tpl: function(name, data)
55         {
56             data.val = this.value_on_view(data.val);
57             return [
58                 'div', { className: data._id+'_element_'+name+'_value' }, data.val
59             ];
60         },
61         set_config: function(config)
62         {
63             this.settings = $.extend(this.settings, config);
64         },
65         value_on_save: function(value)
66         {
67             return value;
68         },
69         value_on_edit: function(value)
70         {
71             return value;
72         },
73         value_on_view: function(value, type)
74         {
75             if (typeof type == 'undefined') {
76                 var type = 'item';
77             }
78             
79             return $.ajatus.i10n.get(this.settings.value_prefix) + value.toString() + $.ajatus.i10n.get(this.settings.value_suffix);
80         },
81         loaded: function()
82         {
83         },
84         init: function(holder, form_mode)
85         {
86             if (form_mode) {
87                 this.init_form_mode(holder);
88             } else {
89                 this.init_view_mode(holder);                
90             }            
91         },
92         init_form_mode: function(holder)
93         {
94             var self = this;
95             
96             var element = $('input.text', holder);
97             element.css({
98                 width: self.settings.width > 0 ? self.settings.width : '99%'
99             }).attr({
100                 maxlength: self.settings.max_length > 0 ? self.settings.max_length : null
101             });
102         },
103         init_view_mode: function(holder)
104         {
105         },
106         
107         create_widget_details: function(data, name) {
108             var details = $.ajatus.widgets.generate_default_details(this, data, name);
110             return details;
111         },
112         create_widget_settings: function(data) {
113             var settings = $.ajatus.widgets.generate_default_settings(this, data);
114             
115             return settings;
116         }
117     };
118     
119 })(jQuery);