1 namespace("kivi", function(ns) {
4 ns.t8 = function(text, params) {
5 var text = ns._locale[text] || text;
7 if( Object.prototype.toString.call( params ) === '[object Array]' ) {
8 var len = params.length;
10 for(var i=0; i<len; ++i) {
12 var value = params[i];
13 text = text.split("#"+ key).join(value);
16 else if( typeof params == 'object' ) {
17 for(var key in params) {
18 var value = params[key];
19 text = text.split("#{"+ key +"}").join(value);
26 ns.setupLocale = function(locale) {
30 ns.reinit_widgets = function() {
31 ns.run_once_for('.datepicker', 'datepicker', function(elt) {
36 ns.run_once_for('input.part_autocomplete', 'part_picker', function(elt) {
37 kivi.PartPicker($(elt));
41 ns.submit_ajax_form = function(url, form_selector, additional_data) {
42 $(form_selector).ajaxSubmit({
44 data: additional_data,
45 success: ns.eval_json_result
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("(.+)\\.([^\\.]+)$");
59 return namespace(parts[1])[ parts[2] ];
62 // Open a modal jQuery UI popup dialog. The content can be either
63 // loaded via AJAX (if the parameter 'url' is given) or simply
64 // displayed if it exists in the DOM already (referenced via
65 // 'id'). If an existing DOM div should be used then the element
66 // won't be removed upon closing the dialog which allows re-opening
70 // - id: dialog DIV ID (optional; defaults to 'jqueryui_popup_dialog')
71 // - url, data, type: passed as the first three arguments to the $.ajax() call if an AJAX call is made, otherwise ignored.
72 // - dialog: an optional object of options passed to the $.dialog() call
73 ns.popup_dialog = function(params) {
76 params = params || { };
77 var id = params.id || 'jqueryui_popup_dialog';
78 var dialog_params = $.extend(
79 { // kivitendo default parameters:
84 // User supplied options:
86 { // Options that must not be changed:
87 close: function(event, ui) { if (params.url) dialog.remove(); else dialog.dialog('close'); }
91 // Use existing DOM element and show it. No AJAX call.
92 dialog = $('#' + id).dialog(dialog_params);
98 dialog = $('<div style="display:none" class="loading" id="' + id + '"></div>').appendTo('body');
99 dialog.dialog(dialog_params);
105 success: function(new_html) {
106 dialog.html(new_html);
107 dialog.removeClass('loading');
114 // Run code only once for each matched element
116 // This allows running the function 'code' exactly once for each
117 // element that matches 'selector'. This is achieved by storing the
118 // state with jQuery's 'data' function. The 'identification' is
119 // required for differentiating unambiguously so that different code
120 // functions can still be run on the same elements.
122 // 'code' can be either a function or the name of one. It must
123 // resolve to a function that receives the jQueryfied element as its
127 ns.run_once_for = function(selector, identification, code) {
128 var attr_name = 'data-run-once-for-' + identification.toLowerCase().replace(/[^a-z]+/g, '-');
129 var fn = typeof code === 'function' ? code : ns.get_function_by_name(code);
131 console.error('kivi.run_once_for(..., "' + code + '"): No function by that name found');
135 $(selector).filter(function() { return $(this).data(attr_name) != true; }).each(function(idx, elt) {
137 $elt.data(attr_name, true);
142 // Run a function by its name passing it some arguments
144 // This is a function useful mainly for the ClientJS functionality.
145 // It finds a function by its name and then executes it on an empty
146 // object passing the elements in 'args' (an array) as the function
147 // parameters retuning its result.
149 // Logs an error to the console and returns 'undefined' if the
150 // function cannot be found.
151 ns.run = function(function_name, args) {
152 var fn = ns.get_function_by_name(function_name);
154 return fn.apply({}, args);
156 console.error('kivi.run("' + function_name + '"): No function by that name found');
161 kivi = namespace('kivi');