summaryrefslogtreecommitdiffstats
path: root/base/tps/shared/webapps/tps/js/group.js
blob: 50d7d6c6704b7cfae989f140c9dc7370f4934846 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* --- BEGIN COPYRIGHT BLOCK ---
 * 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 of the License.
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2013 Red Hat, Inc.
 * All rights reserved.
 * --- END COPYRIGHT BLOCK ---
 *
 * @author Endi S. Dewata
 */

var GroupModel = Model.extend({
    urlRoot: "/tps/rest/admin/groups",
    parseResponse: function(response) {
        return {
            id: response.id,
            groupID: response.GroupID,
            description: response.Description
        };
    },
    createRequest: function(attributes) {
        return {
            id: this.id,
            GroupID: attributes.groupID,
            Description: attributes.description
        };
    }
});

var GroupCollection = Collection.extend({
    model: GroupModel,
    urlRoot: "/tps/rest/admin/groups",
    getEntries: function(response) {
        return response.entries;
    },
    getLinks: function(response) {
        return response.Link;
    },
    parseEntry: function(entry) {
        return new GroupModel({
            id: entry.id,
            groupID: entry.GroupID,
            description: entry.Description
        });
    }
});

var GroupMemberModel = Model.extend({
    url: function() {
        var self = this;

        // There's an attribute name mismatch for group ID: the
        // server uses GroupID and the client uses groupID. In other
        // models the mismatch can be translated just fine, but in
        // this model it becomes a problem because the model needs
        // to construct the URL using the attribute.
        //
        // During read operation it needs to use the attribute that's
        // already translated for client (i.e. groupID), but during
        // add it needs to use the attribute meant for server (i.e.
        // GroupID). So the workaround is to read whichever available.
        var groupID = self.get("groupID");        // for read
        groupID = groupID || self.get("GroupID"); // for add

        var url = "/tps/rest/admin/groups/" + groupID + "/members";

        // append member ID for read
        if (self.id) url = url + "/" + self.id;

        return url;
    },
    parseResponse: function(response) {
        return {
            id: response.id,
            memberID: response.id,
            groupID: response.GroupID
        };
    },
    createRequest: function(entry) {
        return {
            id: entry.memberID,
            GroupID: entry.groupID
        };
    }
});

var GroupMemberCollection = Collection.extend({
    initialize: function(models, options) {
        var self = this;
        GroupMemberCollection.__super__.initialize.call(self, models, options);
        options = options || {};
        self.groupID = options.groupID;
        self.urlRoot = "/tps/rest/admin/groups/" + self.groupID + "/members";
    },
    getEntries: function(response) {
        return response.Member;
    },
    getLinks: function(response) {
        return response.Link;
    },
    model: function(attrs, options) {
        return new GroupMemberModel({
            groupID: this.groupID
        });
    },
    parseEntry: function(entry) {
        return new GroupMemberModel({
            id: entry.id,
            memberID: entry.id,
            groupID: entry.GroupID
        });
    }
});

var GroupMembersTableItem = TableItem.extend({
    initialize: function(options) {
        var self = this;
        GroupMembersTableItem.__super__.initialize.call(self, options);
    },
    renderColumn: function(td, templateTD) {
        var self = this;

        GroupMembersTableItem.__super__.renderColumn.call(self, td, templateTD);

        $("a", td).click(function(e) {
            e.preventDefault();
            self.table.open(self);
        });
    }
});

var GroupPage = EntryPage.extend({
    initialize: function(options) {
        var self = this;
        GroupPage.__super__.initialize.call(self, options);
    },
    setup: function() {
        var self = this;

        GroupPage.__super__.setup.call(self);

        var dialog = self.$("#member-dialog");

        var addDialog = new Dialog({
            el: dialog,
            title: "Add Member",
            readonly: ["groupID"],
            actions: ["cancel", "add"]
        });

        var editDialog = new Dialog({
            el: dialog,
            title: "Member",
            readonly: ["groupID", "memberID"],
            actions: ["close"]
        });

        self.membersTable = new ModelTable({
            el: self.$("table[name='members']"),
            pageSize: 10,
            addDialog: addDialog,
            editDialog: editDialog,
            tableItem: GroupMembersTableItem,
            parent: self
        });
    },
    renderContent: function() {
        var self = this;

        GroupPage.__super__.renderContent.call(self);

        // Since the members table is backed by a REST resource any
        // changes will be executed immediately even if the page is
        // in view mode. To avoid confusion, the members table will
        // be disabled in page edit mode.
        if (self.mode == "edit") {
            // In page edit mode, the members tables is read-only.
            self.membersTable.mode = "view";

            self.membersTable.collection = new GroupMemberCollection(null, { groupID: self.entry.id });

        } else if (self.mode == "add") {
            // In page add mode, the members table is read-only.
            self.membersTable.mode = "view";

            // self.membersTable.collection is undefined for new group

        } else { // self.mode == "view"
            // In page view mode, the members table is editable.
            self.membersTable.mode = "edit";

            self.membersTable.collection = new GroupMemberCollection(null, { groupID: self.entry.id });
        }

        self.membersTable.render();
    }
});

var GroupsTable = ModelTable.extend({
    initialize: function(options) {
        var self = this;
        GroupsTable.__super__.initialize.call(self, options);
    },
    add: function() {
        var self = this;

        window.location.hash = "#new-group";
    }
});

var GroupsPage = Page.extend({
    load: function() {
        var self = this;

        var table = new GroupsTable({
            el: $("table[name='groups']"),
            collection: new GroupCollection()
        });

        table.render();
    }
});