Merge branch 'berichte-bestellnummer-des-kunden'
[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.reinit_widgets = function() {
31     ns.run_once_for('.datepicker', 'datepicker', function(elt) {
32       $(elt).datepicker();
33     });
34
35     if (ns.PartPicker)
36       ns.run_once_for('input.part_autocomplete', 'part_picker', function(elt) {
37         kivi.PartPicker($(elt));
38       });
39
40     var func = kivi.get_function_by_name('local_reinit_widgets');
41     if (func)
42       func();
43   };
44
45   ns.submit_ajax_form = function(url, form_selector, additional_data) {
46     $(form_selector).ajaxSubmit({
47       url:     url,
48       data:    additional_data,
49       success: ns.eval_json_result
50     });
51
52     return true;
53   };
54
55   // Return a function object by its name (a string). Works both with
56   // global functions (e.g. "check_right_date_format") and those in
57   // namespaces (e.g. "kivi.t8").
58   // Returns null if the object is not found.
59   ns.get_function_by_name = function(name) {
60     var parts = name.match("(.+)\\.([^\\.]+)$");
61     if (!parts)
62       return window[name];
63     return namespace(parts[1])[ parts[2] ];
64   };
65
66   // Open a modal jQuery UI popup dialog. The content can be either
67   // loaded via AJAX (if the parameter 'url' is given) or simply
68   // displayed if it exists in the DOM already (referenced via
69   // 'id'). If an existing DOM div should be used then the element
70   // won't be removed upon closing the dialog which allows re-opening
71   // it later on.
72   //
73   // Parameters:
74   // - id: dialog DIV ID (optional; defaults to 'jqueryui_popup_dialog')
75   // - url, data, type: passed as the first three arguments to the $.ajax() call if an AJAX call is made, otherwise ignored.
76   // - dialog: an optional object of options passed to the $.dialog() call
77   ns.popup_dialog = function(params) {
78     var dialog;
79
80     params            = params        || { };
81     var id            = params.id     || 'jqueryui_popup_dialog';
82     var dialog_params = $.extend(
83       { // kivitendo default parameters:
84           width:  800
85         , height: 500
86         , modal:  true
87       },
88         // User supplied options:
89       params.dialog || { },
90       { // Options that must not be changed:
91         close: function(event, ui) { if (params.url) dialog.remove(); else dialog.dialog('close'); }
92       });
93
94     if (!params.url) {
95       // Use existing DOM element and show it. No AJAX call.
96       dialog = $('#' + id).dialog(dialog_params);
97       return true;
98     }
99
100     $('#' + id).remove();
101
102     dialog = $('<div style="display:none" class="loading" id="' + id + '"></div>').appendTo('body');
103     dialog.dialog(dialog_params);
104
105     $.ajax({
106       url:     params.url,
107       data:    params.data,
108       type:    params.type,
109       success: function(new_html) {
110         dialog.html(new_html);
111         dialog.removeClass('loading');
112       }
113     });
114
115     return true;
116   };
117
118   // Run code only once for each matched element
119   //
120   // This allows running the function 'code' exactly once for each
121   // element that matches 'selector'. This is achieved by storing the
122   // state with jQuery's 'data' function. The 'identification' is
123   // required for differentiating unambiguously so that different code
124   // functions can still be run on the same elements.
125   //
126   // 'code' can be either a function or the name of one. It must
127   // resolve to a function that receives the jQueryfied element as its
128   // sole argument.
129   //
130   // Returns nothing.
131   ns.run_once_for = function(selector, identification, code) {
132     var attr_name = 'data-run-once-for-' + identification.toLowerCase().replace(/[^a-z]+/g, '-');
133     var fn        = typeof code === 'function' ? code : ns.get_function_by_name(code);
134     if (!fn) {
135       console.error('kivi.run_once_for(..., "' + code + '"): No function by that name found');
136       return;
137     }
138
139     $(selector).filter(function() { return $(this).data(attr_name) != true; }).each(function(idx, elt) {
140       var $elt = $(elt);
141       $elt.data(attr_name, true);
142       fn($elt);
143     });
144   };
145
146   // Run a function by its name passing it some arguments
147   //
148   // This is a function useful mainly for the ClientJS functionality.
149   // It finds a function by its name and then executes it on an empty
150   // object passing the elements in 'args' (an array) as the function
151   // parameters retuning its result.
152   //
153   // Logs an error to the console and returns 'undefined' if the
154   // function cannot be found.
155   ns.run = function(function_name, args) {
156     var fn = ns.get_function_by_name(function_name);
157     if (fn)
158       return fn.apply({}, args);
159
160     console.error('kivi.run("' + function_name + '"): No function by that name found');
161     return undefined;
162   };
163 });
164
165 kivi = namespace('kivi');