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