summaryrefslogtreecommitdiffstats
path: root/install/static/entity.js
blob: 6e2b501b0989b4549123888bef0fcc69a7b23b0d (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*  Authors:
 *    Pavel Zuna <pzuna@redhat.com>
 *    Endi S. Dewata <edewata@redhat.com>
 *    Adam Young <ayoung@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, details.js, search.js, add.js */

function ipa_facet(spec) {

    spec = spec || {};

    var that = {};
    that.display_class = spec.display_class || 'entity-facet';
    that.name = spec.name;
    that.label = spec.label;
    that._entity_name = spec.entity_name;

    that.init = spec.init || init;
    that.create = spec.create || create;
    that.setup = spec.setup || setup;
    that.load = spec.load || load;

    that.__defineGetter__("entity_name", function(){
        return that._entity_name;
    });

    that.__defineSetter__("entity_name", function(entity_name){
        that._entity_name = entity_name;
    });

    that.create_action_panel = ipa_facet_create_action_panel;

    that.superior = function(name) {
        var method = that[name];
        return function () {
            return method.apply(that, arguments);
        };
    };

    function init() {
    }

    function create(container) {
    }

    function setup(container) {
        that.container = container;
    }

    function load() {
    }

    that.get_client_area = function() {
        return $('.client', that.container);
    };

    that.get_action_panel = function() {
        return $('.action-panel', that.container);
    };

    that.facet_init = that.init;
    that.facet_create = that.create;
    that.facet_setup = that.setup;

    return that;
}

function ipa_entity(spec) {

    spec = spec || {};

    var that = {};
    that.name = spec.name;
    that.label = spec.label;

    that.setup = spec.setup || ipa_entity_setup;

    that.dialogs = [];
    that.dialogs_by_name = {};

    that.facets = [];
    that.facets_by_name = {};

    that.facet_name = null;

    that.autogenerate_associations = false;

    that.associations = [];
    that.associations_by_name = {};

    that.superior = function(name) {
        var method = that[name];
        return function () {
            return method.apply(that, arguments);
        };
    };

    that.get_dialog = function(name) {
        return that.dialogs_by_name[name];
    };

    that.add_dialog = function(dialog) {
        dialog.entity_name = that.name;
        that.dialogs.push(dialog);
        that.dialogs_by_name[dialog.name] = dialog;
    };

    that.get_facet = function(name) {
        return that.facets_by_name[name];
    };

    that.add_facet = function(facet) {
        facet.entity_name = that.name;
        that.facets.push(facet);
        that.facets_by_name[facet.name] = facet;
    };

    that.get_associations = function() {
        return that.associations;
    };

    that.get_association = function(name) {
        return that.associations_by_name[name];
    };

    that.add_association = function(config) {
        that.associations.push(config);
        that.associations_by_name[config.name] = config;
    };

    that.create_association = function(spec) {
        var config = ipa_association_config(spec);
        that.add_association(config);
        return config;
    };

    that.create_association_facet = function(other_entity, attribute_member) {

        var label = IPA.metadata[other_entity].label;

        if (!attribute_member) {
            attribute_member = ipa_get_member_attribute(
                that.entity_name, other_entity
            );
        }

        return ipa_association_facet({
            'name': attribute_member+'_'+other_entity,
            'label': label,
            'other_entity': other_entity
        });
    };

    that.create_association_facets = function() {

        var attribute_members = IPA.metadata[that.name].attribute_members;

        for (var attribute_member in attribute_members) {
            var other_entities = attribute_members[attribute_member];

            for (var j = 0; j < other_entities.length; j++) {
                var other_entity = other_entities[j];

                var facet = that.create_association_facet(other_entity, attribute_member);
                if (that.get_facet(facet.name)) continue;
                that.add_facet(facet);
            }
        }
    };

    that.init = function() {

        if (that.autogenerate_associations) {
            that.create_association_facets();
        }

        for (var i=0; i<that.facets.length; i++) {
            var facet = that.facets[i];
            facet.init();
        }
    };

    that.entity_init = that.init;

    return that;
}

/* use this to track individual changes between two hashchange events */
var window_hash_cache = {};

function ipa_get_entity(entity_name) {

    var entity = IPA.get_entity(entity_name);
    if (entity) return entity;

    entity = ipa_entity({
        'name': entity_name
    });

    IPA.add_entity(entity);
    return entity;
}

function ipa_entity_get_search_facet(entity_name) {

    var entity = ipa_get_entity(entity_name);

    var facet = entity.get_facet('search');
    if (facet) return facet;

    facet = ipa_search_facet({
        'name': 'search',
        'label': 'Search'
    });
    entity.add_facet(facet);

    return facet;
}

function ipa_entity_set_search_definition(entity_name, data) {

    var facet = ipa_entity_get_search_facet(entity_name);

    for (var i=0; i<data.length; i++) {
        var defn = data[i];
        facet.create_column({
            'name': defn[0],
            'label': defn[1],
            'setup': defn[2]
        });
    }
}

function ipa_entity_set_add_definition(entity_name, data) {

    var entity = ipa_get_entity(entity_name);

    var dialog = ipa_add_dialog({
        'name': 'add',
        'title': data[1]
    });
    entity.add_dialog(dialog);
    dialog.init();

    for (var i=0; i<data[2].length; i++) {
        var field = data[2][i];
        dialog.add_field(ipa_text_widget({
            name: field[0],
            label: field[1],
            setup: field[2],
            undo: false
        }));
    }
}

function ipa_entity_get_add_dialog(entity_name) {

    var entity = ipa_get_entity(entity_name);
    return entity.get_add_dialog();
}

function ipa_entity_get_details_facet(entity_name) {

    var entity = ipa_get_entity(entity_name);

    var facet = entity.get_facet('details');
    if (facet) return facet;

    facet = ipa_details_facet({
        'name': 'details',
        'label': 'Details'
    });
    entity.add_facet(facet);

    return facet;
}

function ipa_entity_set_details_definition(entity_name, sections) {

    var facet = ipa_entity_get_details_facet(entity_name);

    for (var i=0; i<sections.length; i++) {
        var section = sections[i];
        facet.add_section(section);
    }
}

function ipa_entity_set_association_definition(entity_name, data) {

    var entity = ipa_get_entity(entity_name);

    entity.autogenerate_associations = true;

    for (var other_entity in data) {
        var config = data[other_entity];
        entity.create_association({
            'name': other_entity,
            'associator': config.associator,
            'add_method': config.add_method,
            'delete_method': config.delete_method
        });
    }
}

function ipa_entity_set_facet_definition(entity_name, list) {

    var entity = ipa_get_entity(entity_name);

    for (var i=0; i<list.length; i++) {
        var facet = list[i];
        entity.add_facet(facet);
    }
}

function ipa_details_only_setup(container){
    ipa_entity_setup.call(this, container, 'details');
}

function ipa_current_facet(entity){
    return $.bbq.getState(entity.name + '-facet', true) || entity.default_facet || 'search';
}

function ipa_entity_setup(container) {

    var entity = this;

    var facet_name = ipa_current_facet(entity);


    var facet = entity.get_facet(facet_name);
    if (!facet) return;

    if (IPA.entity_name == entity.name) {
        if (entity.facet_name == facet_name) {
            if (!facet.is_dirty()) return;

        } else {
            entity.facet_name = facet_name;
        }
    } else {
        IPA.entity_name = entity.name;
    }

    container.empty();

    facet.create_action_panel(container);
    facet.create(container);
    container.children().last().addClass('client');
    facet.setup(container);
    facet.refresh();
}



/*Returns the entity requested, as well as:
  any nested tabs underneath it or
  its parent tab and the others nested at the same level*/

IPA.nested_tabs = function(entity_name){

    var siblings = [];

    if (!IPA.tab_set) {
        siblings.push(entity_name);
        return siblings;
    }

    for (var top_tab_index = 0;
         top_tab_index < IPA.tab_set.length;
         top_tab_index += 1){
        var top_tab =  IPA.tab_set[top_tab_index];
        for (var subtab_index = 0;
             subtab_index < top_tab.children.length;
             subtab_index += 1){
            if(top_tab.children[subtab_index].name){
                if (top_tab.children[subtab_index].name === entity_name){
                    siblings.push(entity_name);
                    if (top_tab.children[subtab_index].children){
                        var  nested_entities = top_tab.children[subtab_index].children;
                        for (var nested_index = 0;
                             nested_index < nested_entities.length;
                             nested_index += 1){
                            siblings.push (nested_entities[nested_index].name);
                        }
                    }
                }else{
                    if (top_tab.children[subtab_index].children){
                        var  nested_entities = top_tab.children[subtab_index].children;
                        for (var nested_index = 0;
                             nested_index < nested_entities.length;
                             nested_index += 1){
                            if (nested_entities[nested_index].name === entity_name){
                                siblings.push(top_tab.children[subtab_index].name);
                                for (var nested_index2 = 0;
                                     nested_index2 < nested_entities.length;
                                     nested_index2 += 1){
                                    siblings.push(nested_entities[nested_index2].name);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return siblings;
}



function ipa_facet_create_action_panel(container) {

    var that = this;
    var entity_name = that.entity_name;
    var action_panel = $('<div/>', {
        "class": "action-panel",
        html: $('<h3>',{
            text: IPA.metadata[entity_name].label
        })
    }).appendTo(container);
    function build_link(other_facet,label){
        var li = $('<li/>', {
            "class" : other_facet.display_class,
            title: other_facet.name,
            text: label,
            click: function(entity_name, other_facet_name) {
                return function() {
                    if($(this).hasClass('entity-facet-disabled')){
                        return false;
                    }
                    var this_pkey = $('input[id=pkey]', action_panel).val();
                    IPA.switch_and_show_page(
                        entity_name, other_facet_name,
                        this_pkey);
                    return false;
                };
            }(entity_name, other_facet_name)
        });
        return li;
    }
    /*Note, for debugging purposes, it is useful to set var pkey_type = 'text';*/
    var pkey_type = 'hidden';
    $('<input/>', {
        'type': pkey_type,
        id:'pkey',
        name:'pkey'
    }).appendTo(action_panel);
    var ul = $('<ul/>', {'class': 'action'}).appendTo(action_panel);
    var entity = IPA.get_entity(entity_name);
    var facet_name =  ipa_current_facet(entity);
    var other_facet = entity.facets[0];
    var other_facet_name = other_facet.name;
    var nested_tabs = IPA.nested_tabs(entity_name);
    var main_facet = build_link(other_facet,other_facet.label)
    for (var nested_index = 0 ;
         nested_index < nested_tabs.length;
         nested_index += 1){
        if (nested_tabs[nested_index] === entity_name){
            /*assume for now that entities with only a single facet
              do not have search*/
            if (entity.facets.length > 0 ){
                main_facet.text( 'List ' +  IPA.metadata[entity_name].label);
            }
            main_facet.appendTo(ul);

            ul.append($('<li><span class="action-controls"/></li>'));
            for (var i=1; i<entity.facets.length; i++) {
                other_facet = entity.facets[i];
                other_facet_name = other_facet.name;
                ul.append(build_link(other_facet,other_facet.label));
            }
        }else{
            $('<li/>', {
                title: nested_tabs[nested_index],
                text: IPA.metadata[nested_tabs[nested_index]].label,
                "class": "search-facet",
                click: function() {
                    var state = {};
                    state[nested_tabs[0]+'-entity'] =
                        this.title;
                    nav_push_state(state);
                    return false;
                }
            }).appendTo(ul);
        }
    }
    /*When we land on the search page, disable all facets
          that require a pkey until one is selected*/
    if (facet_name === 'search'){
        $('.entity-facet', action_panel).addClass('entity-facet-disabled');
    }
    return action_panel;
}