kivi.popup_dialog: auf jQuery UI basierende Popupdialoge
[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   // Open a modal jQuery UI popup dialog. The content is loaded via AJAX.
53   //
54   // Parameters:
55   // - id: dialog DIV ID (optional; defaults to 'jqueryui_popup_dialog')
56   // - url, data, type: passed as the first three arguments to the $.ajax() call
57   // - dialog: an optional object of options passed to the $.dialog() call
58   ns.popup_dialog = function(params) {
59     var dialog;
60
61     params            = params        || { };
62     var id            = params.id     || 'jqueryui_popup_dialog';
63     var dialog_params = $.extend(
64       { // kivitendo default parameters:
65           width:  800
66         , height: 500
67         , modal:  true
68       },
69         // User supplied options:
70       params.dialog || { },
71       { // Options that must not be changed:
72         close: function(event, ui) { dialog.remove(); }
73       });
74
75     $('#' + id).remove();
76
77     dialog = $('<div style="display:none" class="loading" id="' + id + '"></div>').appendTo('body');
78     dialog.dialog(dialog_params);
79
80     $.ajax({
81       url:     params.url,
82       data:    params.data,
83       type:    params.type,
84       success: function(new_html) {
85         dialog.html(new_html);
86         dialog.removeClass('loading');
87       }
88     });
89
90     return true;
91   };
92 });
93
94 kivi = namespace('kivi');