5901c89a1aae3397f307a571d3115d47811b0bc3
[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     $('.datepicker').each(function() {
32       $(this).datepicker();
33     });
34
35     if (ns.PartPicker)
36       $('input.part_autocomplete').each(function(idx, elt){
37         kivi.PartPicker($(elt));
38       });
39   };
40
41   // Return a function object by its name (a string). Works both with
42   // global functions (e.g. "check_right_date_format") and those in
43   // namespaces (e.g. "kivi.t8").
44   // Returns null if the object is not found.
45   ns.get_function_by_name = function(name) {
46     var parts = name.match("(.+)\\.([^\\.]+)$");
47     if (!parts)
48       return window[name];
49     return namespace(parts[1])[ parts[2] ];
50   };
51 });
52
53 kivi = namespace('kivi');