1 namespace("kivi", function(ns) {
21 ns.setup_formats = function(params) {
22 var res = (params.dates || "").match(/^([ymd]+)([^a-z])([ymd]+)[^a-z]([ymd]+)$/);
24 ns._date_format = { sep: res[2] };
25 ns._date_format[res[1].substr(0, 1)] = 0;
26 ns._date_format[res[3].substr(0, 1)] = 1;
27 ns._date_format[res[4].substr(0, 1)] = 2;
30 res = (params.times || "").match(/^([hm]+)([^a-z])([hm]+)$/);
32 ns._time_format = { sep: res[2] };
33 ns._time_format[res[1].substr(0, 1)] = 0;
34 ns._time_format[res[3].substr(0, 1)] = 1;
37 res = (params.numbers || "").match(/^\d*([^\d]?)\d+([^\d])\d+$/);
45 ns.parse_date = function(date) {
46 if (date === undefined)
52 if (date === '0' || date === '00')
55 var parts = date.replace(/\s+/g, "").split(ns._date_format.sep);
56 var today = new Date();
59 // assume fixed pattern, and extract parts again
60 if (parts.length == 1) {
62 parts = date.match(/../g);
63 if (date.length == 8) {
64 parts[ns._date_format.y] += parts.splice(ns._date_format.y + 1, 1)
67 if (date.length == 6 || date.length == 4) {
70 if (date.length == 1 || date.length == 2) {
72 parts[ ns._date_format.y ] = today.getFullYear();
73 parts[ ns._date_format.m ] = today.getMonth() + 1;
74 parts[ ns._date_format.d ] = date;
81 if (parts.length == 3) {
82 var year = +parts[ ns._date_format.y ] || 0 * 1 || (new Date()).getFullYear();
86 year += year > 70 ? 1900 : 2000;
90 (parts[ ns._date_format.m ] || (today.getMonth() + 1)) * 1 - 1, // Months are 0-based.
91 (parts[ ns._date_format.d ] || today.getDate()) * 1
93 } else if (parts.length == 2) {
95 (new Date()).getFullYear(),
96 (parts[ (ns._date_format.m > ns._date_format.d) * 1 ] || (today.getMonth() + 1)) * 1 - 1, // Months are 0-based.
97 (parts[ (ns._date_format.d > ns._date_format.m) * 1 ] || today.getDate()) * 1
102 return isNaN(date.getTime()) ? undefined : date;
105 ns.format_date = function(date) {
106 if (isNaN(date.getTime()))
109 var parts = [ "", "", "" ]
110 parts[ ns._date_format.y ] = date.getFullYear();
111 parts[ ns._date_format.m ] = (date.getMonth() < 9 ? "0" : "") + (date.getMonth() + 1); // Months are 0-based, but days are 1-based.
112 parts[ ns._date_format.d ] = (date.getDate() < 10 ? "0" : "") + date.getDate();
113 return parts.join(ns._date_format.sep);
116 ns.parse_time = function(time) {
117 var now = new Date();
119 if (time === undefined)
128 // special case 1: military time in fixed "hhmm" format
129 if (time.length == 4) {
130 var res = time.match(/(\d\d)(\d\d)/);
132 now.setHours(res[1], res[2]);
139 var parts = time.replace(/\s+/g, "").split(ns._time_format.sep);
140 if (parts.length == 2) {
141 now.setHours(parts[ns._time_format.h], parts[ns._time_format.m]);
147 ns.format_time = function(date) {
148 if (isNaN(date.getTime()))
151 var parts = [ "", "" ]
152 parts[ ns._time_format.h ] = date.getHours().toString().padStart(2, '0');
153 parts[ ns._time_format.m ] = date.getMinutes().toString().padStart(2, '0');
154 return parts.join(ns._time_format.sep);
157 ns.parse_amount = function(amount) {
158 if (amount === undefined)
164 if (ns._number_format.decimalSep == ',')
165 amount = amount.replace(/\./g, "").replace(/,/g, ".");
167 amount = amount.replace(/[\',]/g, "");
169 // Make sure no code wich is not a math expression ends up in eval().
170 if (!amount.match(/^[0-9 ()\-+*/.]*$/))
173 amount = amount.replace(/^0+(\d+)/, '$1');
183 ns.round_amount = function(amount, places) {
184 var neg = amount >= 0 ? 1 : -1;
185 var mult = Math.pow(10, places + 1);
186 var temp = Math.abs(amount) * mult;
187 var diff = Math.abs(1 - temp + Math.floor(temp));
188 temp = Math.floor(temp) + (diff <= 0.00001 ? 1 : 0);
190 temp += dec >= 5 ? 10 - dec: dec * -1;
192 return neg * temp / mult;
195 ns.format_amount = function(amount, places) {
196 amount = amount || 0;
198 if ((places !== undefined) && (places >= 0))
199 amount = ns.round_amount(amount, Math.abs(places));
201 var parts = ("" + Math.abs(amount)).split(/\./);
203 var dec = parts.length > 1 ? parts[1] : "";
204 var sign = amount < 0 ? "-" : "";
206 if (places !== undefined) {
207 while (dec.length < Math.abs(places))
210 if ((places > 0) && (dec.length > Math.abs(places)))
211 dec = d.substr(0, places);
214 if ((ns._number_format.thousandSep !== "") && (intg.length > 3)) {
215 var len = ((intg.length + 2) % 3) + 1,
217 res = intg.substr(0, len);
218 while (start < intg.length) {
219 res += ns._number_format.thousandSep + intg.substr(start, 3);
226 var sep = (places !== 0) && (dec !== "") ? ns._number_format.decimalSep : "";
228 return sign + intg + sep + dec;
231 ns.t8 = function(text, params) {
232 text = ns._locale[text] || text;
235 if( Object.prototype.toString.call( params ) === '[object Array]' ) {
236 var len = params.length;
238 for(var i=0; i<len; ++i) {
241 text = text.split("#"+ key).join(value);
244 else if( typeof params == 'object' ) {
247 text = text.split("#{"+ key +"}").join(value);
254 ns.setupLocale = function(locale) {
258 ns.set_focus = function(element) {
259 var $e = $(element).eq(0);
260 if ($e.data('ckeditorInstance'))
261 ns.focus_ckeditor_when_ready($e);
266 ns.focus_ckeditor_when_ready = function(element) {
267 $(element).data('ckeditorInstance').on('instanceReady', function() { ns.focus_ckeditor(element); });
270 ns.focus_ckeditor = function(element) {
271 $(element).data('ckeditorInstance').focus();
274 ns.selectall_ckeditor = function(element) {
275 var editor = $(element).ckeditorGet();
276 var editable = editor.editable();
277 if (editable.is('textarea')) {
278 var textarea = editable.$;
281 textarea.createTextRange().execCommand('SelectAll');
283 textarea.selectionStart = 0;
284 textarea.selectionEnd = textarea.value.length;
287 if (editable.is('body'))
288 editor.document.$.execCommand('SelectAll', false, null);
291 var range = editor.createRange();
292 range.selectNodeContents(editable);
296 editor.forceNextSelectionCheck();
297 editor.selectionChange();
301 ns.init_tabwidget = function(element) {
302 var $element = $(element);
304 var elementId = $element.attr('id');
307 var cookieName = 'jquery_ui_tab_'+ elementId;
308 if (!window.location.hash) {
309 // only activate if there's no hash to overwrite it
310 tabsParams.active = $.cookie(cookieName);
312 tabsParams.activate = function(event, ui) {
313 var i = ui.newTab.parent().children().index(ui.newTab);
314 $.cookie(cookieName, i);
318 $element.tabs(tabsParams);
321 ns.init_text_editor = function(element) {
323 all: [ [ 'Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript' ], [ 'BulletedList', 'NumberedList' ], [ 'RemoveFormat' ] ],
324 default: [ [ 'Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript' ], [ 'BulletedList', 'NumberedList' ], [ 'RemoveFormat' ] ]
328 var buttons = layouts[ $e.data('texteditor-layout') || 'default' ] || layouts['default'];
332 removePlugins: 'resize',
333 extraPlugins: 'inline_resize',
335 disableAutoInline: true,
339 config.height = $e.height();
340 config.width = $e.width();
342 var editor = CKEDITOR.inline($e.get(0), config);
343 $e.data('ckeditorInstance', editor);
345 if ($e.hasClass('texteditor-autofocus'))
346 editor.on('instanceReady', function() { ns.focus_ckeditor($e); });
349 ns.reinit_widgets = function() {
350 ns.run_once_for('.datepicker', 'datepicker', function(elt) {
354 if (ns.Part) ns.Part.reinit_widgets();
355 if (ns.CustomerVendor) ns.CustomerVendor.reinit_widgets();
356 if (ns.Validator) ns.Validator.reinit_widgets();
358 if (ns.ProjectPicker)
359 ns.run_once_for('input.project_autocomplete', 'project_picker', function(elt) {
360 kivi.ProjectPicker($(elt));
364 ns.run_once_for('input.chart_autocomplete', 'chart_picker', function(elt) {
365 kivi.ChartPicker($(elt));
369 var func = kivi.get_function_by_name('local_reinit_widgets');
373 ns.run_once_for('.tooltipster', 'tooltipster', function(elt) {
375 contentAsHTML: false,
376 theme: 'tooltipster-light'
380 ns.run_once_for('.tooltipster-html', 'tooltipster-html', function(elt) {
383 theme: 'tooltipster-light'
387 ns.run_once_for('.tabwidget', 'tabwidget', kivi.init_tabwidget);
388 ns.run_once_for('.texteditor', 'texteditor', kivi.init_text_editor);
391 ns.submit_ajax_form = function(url, form_selector, additional_data) {
392 $(form_selector).ajaxSubmit({
394 data: additional_data,
395 success: ns.eval_json_result
401 // This function submits an existing form given by "form_selector"
402 // and sets the "action" input to "action_to_call" before submitting
403 // it. Any existing input named "action" will be removed prior to
405 ns.submit_form_with_action = function(form_selector, action_to_call) {
406 $('[name=action]').remove();
408 var $form = $(form_selector);
409 var $hidden = $('<input type=hidden>');
411 $hidden.attr('name', 'action');
412 $hidden.attr('value', action_to_call);
413 $form.append($hidden);
418 // This function exists solely so that it can be found with
419 // kivi.get_functions_by_name() and called later on. Using something
420 // like "var func = history["back"]" works, but calling it later
421 // with "func.apply()" doesn't.
422 ns.history_back = function() {
426 // Return a function object by its name (a string). Works both with
427 // global functions (e.g. "focus_by_name") and those in namespaces (e.g.
429 // Returns null if the object is not found.
430 ns.get_function_by_name = function(name) {
431 var parts = name.match("(.+)\\.([^\\.]+)$");
434 return namespace(parts[1])[ parts[2] ];
437 // Open a modal jQuery UI popup dialog. The content can be either
438 // loaded via AJAX (if the parameter 'url' is given) or simply
439 // displayed if it exists in the DOM already (referenced via
440 // 'id') or given via param.html. If an existing DOM div should be used then
441 // the element won't be removed upon closing the dialog which allows
442 // re-opening it later on.
445 // - id: dialog DIV ID (optional; defaults to 'jqueryui_popup_dialog')
446 // - url, data, type: passed as the first three arguments to the $.ajax() call if an AJAX call is made, otherwise ignored.
447 // - dialog: an optional object of options passed to the $.dialog() call
448 // - load: an optional function that is called after the content has been loaded successfully (only if an AJAX call is made)
449 ns.popup_dialog = function(params) {
452 params = params || { };
453 var id = params.id || 'jqueryui_popup_dialog';
454 var custom_close = params.dialog ? params.dialog.close : undefined;
455 var dialog_params = $.extend(
456 { // kivitendo default parameters:
461 // User supplied options:
462 params.dialog || { },
463 { // Options that must not be changed:
464 close: function(event, ui) {
465 dialog.dialog('close');
470 if (params.url || params.html)
475 if (!params.url && !params.html) {
476 // Use existing DOM element and show it. No AJAX call.
479 .bind('dialogopen', function() {
480 ns.run_once_for('.texteditor-in-dialog,.texteditor-dialog', 'texteditor', kivi.init_text_editor);
482 .dialog(dialog_params);
486 $('#' + id).remove();
488 dialog = $('<div style="display:none" class="loading" id="' + id + '"></div>').appendTo('body');
489 dialog.dialog(dialog_params);
492 dialog.html(params.html);
494 // no html? get it via ajax
499 success: function(new_html) {
500 dialog.html(new_html);
501 dialog.removeClass('loading');
511 // Run code only once for each matched element
513 // This allows running the function 'code' exactly once for each
514 // element that matches 'selector'. This is achieved by storing the
515 // state with jQuery's 'data' function. The 'identification' is
516 // required for differentiating unambiguously so that different code
517 // functions can still be run on the same elements.
519 // 'code' can be either a function or the name of one. It must
520 // resolve to a function that receives the jQueryfied element as its
524 ns.run_once_for = function(selector, identification, code) {
525 var attr_name = 'data-run-once-for-' + identification.toLowerCase().replace(/[^a-z]+/g, '-');
526 var fn = typeof code === 'function' ? code : ns.get_function_by_name(code);
528 console.error('kivi.run_once_for(..., "' + code + '"): No function by that name found');
532 $(selector).filter(function() { return $(this).data(attr_name) !== true; }).each(function(idx, elt) {
534 $elt.data(attr_name, true);
539 // Run a function by its name passing it some arguments
541 // This is a function useful mainly for the ClientJS functionality.
542 // It finds a function by its name and then executes it on an empty
543 // object passing the elements in 'args' (an array) as the function
544 // parameters retuning its result.
546 // Logs an error to the console and returns 'undefined' if the
547 // function cannot be found.
548 ns.run = function(function_name, args) {
549 var fn = ns.get_function_by_name(function_name);
551 return fn.apply({}, args || []);
553 console.error('kivi.run("' + function_name + '"): No function by that name found');
557 ns.detect_duplicate_ids_in_dom = function() {
561 $('[id]').each(function() {
562 if (this.id && ids[this.id]) {
564 console.warn('Duplicate ID #' + this.id);
570 console.log('No duplicate IDs found :)');
573 ns.validate_form = function(selector) {
574 if (!kivi.Validator) {
575 console.log('kivi.Validator is not loaded');
577 return kivi.Validator.validate_all(selector);
581 // Verifies that at least one checkbox matching the
582 // "checkbox_selector" is actually checked. If not, an error message
583 // is shown, and false is returned. Otherwise (at least one of them
584 // is checked) nothing is shown and true returned.
586 // Can be used in checks when clicking buttons.
587 ns.check_if_entries_selected = function(checkbox_selector) {
588 if ($(checkbox_selector + ':checked').length > 0)
591 alert(kivi.t8('No entries have been selected.'));
596 ns.switch_areainput_to_textarea = function(id) {
597 var $input = $('#' + id);
601 var $area = $('<textarea></textarea>');
603 $area.prop('rows', 3);
604 $area.prop('cols', $input.prop('size') || 40);
605 $area.prop('name', $input.prop('name'));
606 $area.prop('id', $input.prop('id'));
607 $area.val($input.val());
609 $input.parent().replaceWith($area);
614 kivi = namespace('kivi');