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