Refactor: init_tabwidgets() in kivi-Namespace verschoben
[kivitendo-erp.git] / js / kivi.js
1 namespace("kivi", function(ns) {
2   ns._locale = {};
3
4   ns.t8 = function(text, params) {
5     var text = ns._locale[text] || text;
6
7     if( Object.prototype.toString.call( params ) === '[object Array]' ) {
8       var len = params.length;
9
10       for(var i=0; i<len; ++i) {
11         var key = i + 1;
12         var value = params[i];
13         text = text.split("#"+ key).join(value);
14       }
15     }
16     else if( typeof params == 'object' ) {
17       for(var key in params) {
18         var value = params[key];
19         text = text.split("#{"+ key +"}").join(value);
20       }
21     }
22
23     return text;
24   };
25
26   ns.setupLocale = function(locale) {
27     ns._locale = locale;
28   };
29
30   ns.init_tabwidget = function(element) {
31     var $element   = $(element);
32     var tabsParams = {};
33     var elementId  = $element.attr('id');
34
35     if (elementId) {
36       var cookieName      = 'jquery_ui_tab_'+ elementId;
37       tabsParams.active   = $.cookie(cookieName);
38       tabsParams.activate = function(event, ui) {
39         var i = ui.newTab.parent().children().index(ui.newTab);
40         $.cookie(cookieName, i);
41       };
42     }
43
44     $element.tabs(tabsParams);
45   };
46
47   ns.reinit_widgets = function() {
48     ns.run_once_for('.datepicker', 'datepicker', function(elt) {
49       $(elt).datepicker();
50     });
51
52     if (ns.PartPicker)
53       ns.run_once_for('input.part_autocomplete', 'part_picker', function(elt) {
54         kivi.PartPicker($(elt));
55       });
56
57     var func = kivi.get_function_by_name('local_reinit_widgets');
58     if (func)
59       func();
60
61     ns.run_once_for('.tooltip', 'tooltip', function(elt) {
62       $(elt).tooltip();
63     });
64
65     ns.run_once_for('.tabwidget', 'tabwidget', kivi.init_tabwidget);
66   };
67
68   ns.submit_ajax_form = function(url, form_selector, additional_data) {
69     $(form_selector).ajaxSubmit({
70       url:     url,
71       data:    additional_data,
72       success: ns.eval_json_result
73     });
74
75     return true;
76   };
77
78   // Return a function object by its name (a string). Works both with
79   // global functions (e.g. "check_right_date_format") and those in
80   // namespaces (e.g. "kivi.t8").
81   // Returns null if the object is not found.
82   ns.get_function_by_name = function(name) {
83     var parts = name.match("(.+)\\.([^\\.]+)$");
84     if (!parts)
85       return window[name];
86     return namespace(parts[1])[ parts[2] ];
87   };
88
89   // Open a modal jQuery UI popup dialog. The content can be either
90   // loaded via AJAX (if the parameter 'url' is given) or simply
91   // displayed if it exists in the DOM already (referenced via
92   // 'id'). If an existing DOM div should be used then the element
93   // won't be removed upon closing the dialog which allows re-opening
94   // it later on.
95   //
96   // Parameters:
97   // - id: dialog DIV ID (optional; defaults to 'jqueryui_popup_dialog')
98   // - url, data, type: passed as the first three arguments to the $.ajax() call if an AJAX call is made, otherwise ignored.
99   // - dialog: an optional object of options passed to the $.dialog() call
100   ns.popup_dialog = function(params) {
101     var dialog;
102
103     params            = params        || { };
104     var id            = params.id     || 'jqueryui_popup_dialog';
105     var dialog_params = $.extend(
106       { // kivitendo default parameters:
107           width:  800
108         , height: 500
109         , modal:  true
110       },
111         // User supplied options:
112       params.dialog || { },
113       { // Options that must not be changed:
114         close: function(event, ui) { if (params.url) dialog.remove(); else dialog.dialog('close'); }
115       });
116
117     if (!params.url) {
118       // Use existing DOM element and show it. No AJAX call.
119       dialog = $('#' + id).dialog(dialog_params);
120       return true;
121     }
122
123     $('#' + id).remove();
124
125     dialog = $('<div style="display:none" class="loading" id="' + id + '"></div>').appendTo('body');
126     dialog.dialog(dialog_params);
127
128     $.ajax({
129       url:     params.url,
130       data:    params.data,
131       type:    params.type,
132       success: function(new_html) {
133         dialog.html(new_html);
134         dialog.removeClass('loading');
135       }
136     });
137
138     return true;
139   };
140
141   // Run code only once for each matched element
142   //
143   // This allows running the function 'code' exactly once for each
144   // element that matches 'selector'. This is achieved by storing the
145   // state with jQuery's 'data' function. The 'identification' is
146   // required for differentiating unambiguously so that different code
147   // functions can still be run on the same elements.
148   //
149   // 'code' can be either a function or the name of one. It must
150   // resolve to a function that receives the jQueryfied element as its
151   // sole argument.
152   //
153   // Returns nothing.
154   ns.run_once_for = function(selector, identification, code) {
155     var attr_name = 'data-run-once-for-' + identification.toLowerCase().replace(/[^a-z]+/g, '-');
156     var fn        = typeof code === 'function' ? code : ns.get_function_by_name(code);
157     if (!fn) {
158       console.error('kivi.run_once_for(..., "' + code + '"): No function by that name found');
159       return;
160     }
161
162     $(selector).filter(function() { return $(this).data(attr_name) != true; }).each(function(idx, elt) {
163       var $elt = $(elt);
164       $elt.data(attr_name, true);
165       fn($elt);
166     });
167   };
168
169   // Run a function by its name passing it some arguments
170   //
171   // This is a function useful mainly for the ClientJS functionality.
172   // It finds a function by its name and then executes it on an empty
173   // object passing the elements in 'args' (an array) as the function
174   // parameters retuning its result.
175   //
176   // Logs an error to the console and returns 'undefined' if the
177   // function cannot be found.
178   ns.run = function(function_name, args) {
179     var fn = ns.get_function_by_name(function_name);
180     if (fn)
181       return fn.apply({}, args);
182
183     console.error('kivi.run("' + function_name + '"): No function by that name found');
184     return undefined;
185   };
186 });
187
188 kivi = namespace('kivi');