summaryrefslogtreecommitdiffstats
path: root/install/static/add.js
blob: 71d08338e09ec645c2ffcae86df868dc7a0d23ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*  Authors:
 *    Pavel Zuna <pzuna@redhat.com>
 *
 * Copyright (C) 2010 Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 only
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

/* REQUIRES: ipa.js */

var IPA_ADD_POPULATE = 1;
var IPA_ADD_UPDATE = 2;

function add_dialog_create(obj_name, adl)
{
    var add_dialog = $('<div></div>');

    function add(evt, called_from_add_and_edit) {
        var pkey = [];
        var options = {};
        var pkey_name = ipa_objs[obj_name].primary_key;

        function add_win(data, text_status, xhr) {
            if (called_from_add_and_edit) {
                var state = {};
                state[obj_name + '-facet'] = 'details';
                state[obj_name + '-pkey'] = pkey[0];
                $.bbq.pushState(state);
            }
        };

        var fields = adl[2];
        for (var i = 0; i < fields.length; ++i) {
            var f = fields[i];
            var attr = f[0];
            if (typeof f[2] == 'function') {
                var value = f[2](add_dialog, IPA_ADD_UPDATE);
                if (value != null) {
                    if (attr == pkey_name)
                        pkey = [value];
                    else
                        options[attr] = value;
                }
            }
        }

        add_dialog.find('input').each(function () {
            var jobj = $(this);
            var attr = jobj.attr('name');
            var value = jobj.val();
            if (value) {
                if (pkey.length == 0 && attr == pkey_name)
                    pkey = [jobj.val()];
                else if (options[attr] == null)
                    options[attr] = jobj.val();
            }
        });

        ipa_cmd('add', pkey, options, add_win, null, obj_name);
        add_dialog.dialog('close');
    };

    function add_and_edit(evt) {
        add(evt, true);
        add_dialog.dialog('close');
    };

    function cancel() {
        add_dialog.dialog('close');
    };

    add_dialog.attr('id', adl[0]);
    add_dialog.attr('title', adl[1]);

    var fields = adl[2];
    for (var i = 0; i < fields.length; ++i) {
        var f = fields[i];
        if (typeof f[2] == 'function') {
            f[2](add_dialog, IPA_ADD_POPULATE);
        } else {
            add_dialog.append('<label>' + f[1] + '</label>');
            add_dialog.append('<input type="text" name="' + f[0] + '" />');
        }
    }

    add_dialog.dialog({
        modal: true,
        buttons: {
            'Add': add,
            'Add and edit': add_and_edit,
            'Cancel': cancel
        }
    });
}