/* Authors: * Pavel Zuna * Adam Young * Endi S. Dewata * * 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 */ /* IPA Object Details - populating definiton lists from entry data */ /* REQUIRES: ipa.js */ var ipa_details_cache = {}; IPA.is_field_writable = function(rights){ if (!rights){ alert('no right'); } return rights.indexOf('w') > -1; }; function ipa_details_field(spec) { spec = spec || {}; var that = ipa_widget(spec); that.create = spec.create || create; that.load = spec.load || load; that.save = spec.save || save; function create(container) { var title = that.name; var label = ''; var param_info = ipa_get_param_info(that.entity_name, that.name); if (param_info) label = param_info['label']; if (!label) label = that.label; $('
', { id: that.name, title: title, html: label + ':' }).appendTo(container); } function load(container, result) { var multivalue = false; var hint_span = null; var dd; var dt = $('dt[title='+that.name+']', container); if (!dt.length) return; var param_info = ipa_get_param_info(that.entity_name, that.name); if (param_info) { if (param_info['multivalue'] || param_info['class'] == 'List') multivalue = true; var hint = param_info['hint']; if (hint){ hint_span = $('',{ 'class': 'attrhint', 'html': 'Hint: ' + hint}); } } var value = result[that.name]; var rights = 'rsc'; if (result.attributelevelrights){ rights = result.attributelevelrights[this.name] || rights ; } if (value) { dd = ipa_create_first_dd( that.name,ipa_create_input( that.entity_name, that.name, value[0],hint_span,rights) ); dt.after(dd); var last_dd = dd; for (var i = 1; i < value.length; ++i) { dd = ipa_create_other_dd( that.name, ipa_create_input(that.entity_name, that.name, value[i],hint_span,rights) ); last_dd.after(dd); last_dd = dd; } if (multivalue && IPA.is_field_writable(rights) ) { dd = ipa_create_other_dd( that.name, _ipa_a_add_template.replace('A', that.name) ); last_dd.after(dd); } } else { if (multivalue && IPA.is_field_writable(rights)) { dd = ipa_create_first_dd( that.name, _ipa_a_add_template.replace('A', that.name) ); dt.after(dd); } else { dd = ipa_create_first_dd( that.name, ipa_create_input( that.entity_name, that.name,'',hint_span,rights) ); dt.after(dd); } } } function save(container) { var values = []; var dd = $('dd[title='+that.name+']', container); dd.each(function () { var input = $('input', $(this)); if (!input.length) return; if (input.is('.strikethrough')) return; var value = $.trim(input.val()); if (!value) value = ''; values.push(value); }); return values; } return that; } function ipa_details_section(spec){ spec = spec || {}; var that = {}; that.name = spec.name || ''; that.label = spec.label || ''; that.template = spec.template; that._entity_name = spec.entity_name; that.fields = []; that.fields_by_name = {}; that.superior = function(name) { var method = that[name]; return function () { return method.apply(that, arguments); }; }; that.__defineGetter__("entity_name", function(){ return that._entity_name; }); that.__defineSetter__("entity_name", function(entity_name){ that._entity_name = entity_name; for (var i=0; i', { 'name': field.name }).appendTo(container); field.create(span); } }; that.setup = function(container) { this.container = container; if (that.template) return; var fields = that.fields; for (var i = 0; i < fields.length; ++i) { var field = fields[i]; var span = $('span[name='+field.name+']', this.container).first(); field.setup(span); } }; that.load = function(result) { var fields = that.fields; if (that.template) { var template = IPA.get_template(that.template); this.container.load( template, function(data, text_status, xhr) { for (var i = 0; i < fields.length; ++i) { var field = fields[i]; var span = $('span[name='+field.name+']', this.container).first(); field.setup(span); field.load(span, result); } } ); return; } for (var j=0; j', { 'id': that.name, 'class': 'entryattrs' }).appendTo(container); var fields = that.fields; for (var i = 0; i < fields.length; ++i) { var field = fields[i]; var span = $('', { 'name': field.name }).appendTo(dl); field.create(span); } }; /* populate definition lists with the class 'entryattrs' with entry attributes * * The list has to be specially crafted for this function to work properly: *
tags should have the 'title' attribute set to an LDAP attribute name * OR to a javascript function name prefixed with 'call_', which will be given * the
object and entry_attrs as arguments. * Example: *
*
First Name:
*
Some Attribute:
*
* * arguments: * result - 'result' field as returned by ipa *-show commnads * (basically an associative array with attr:value pairs) */ that.load = function(result) { /* remove all
tags i.e. all attribute values */ $('dd', that.container).remove(); /* go through all
tags and pair them with newly created
s */ that.section_load(result); }; // Deprecated: Used for backward compatibility only. function input(spec){ that.create_field(spec); return that; } that.input = input; return that; } // Deprecated: Used for backward compatibility only. function ipa_stanza(spec) { return ipa_details_list_section(spec); } function ipa_details_facet(spec) { spec = spec || {}; var that = ipa_facet(spec); that.is_dirty = spec.is_dirty || ipa_details_is_dirty; that.create = spec.create || ipa_details_create; that.setup = spec.setup || ipa_details_setup; that.load = spec.load || ipa_details_load; that.update = spec.update || ipa_details_update; that.reset = spec.reset || ipa_details_reset; that.display = spec.display || ipa_details_display; that.sections = []; that.sections_by_name = {}; that.__defineGetter__("entity_name", function(){ return that._entity_name; }); that.__defineSetter__("entity_name", function(entity_name){ that._entity_name = entity_name; for (var i=0; i', { 'id': spec.id, 'html': spec.label, 'class': 'ui-state-default ui-corner-all input_link' }); if (spec.click) button.click(spec.click); if (spec.class) button.addClass(spec.class); if (spec.icon) button.append(' '); return button; } function ipa_details_is_dirty() { var pkey = $.bbq.getState(this.entity_name + '-pkey', true) || ''; return pkey != this.pkey; } function ipa_details_create(container) { var facet = this; if (!container) { alert('ERROR: ipa_details_create: Missing container argument!'); return; } var entity_name = container.attr('id'); container.attr('title', entity_name); var details = $('
', { 'class': 'content' }).appendTo(container); var buttons = $('
  • ', { 'class': 'details-buttons' }).prependTo($('.action-panel ul')); buttons.append(ipa_button({ 'label': 'Reset', 'icon': 'ui-icon-refresh', 'class': 'details-reset', 'click': function() { facet.reset(container); return false; } })); var pkey_name = IPA.metadata[facet.entity_name].primary_key; buttons.append(ipa_button({ 'label': 'Update', 'icon': 'ui-icon-check', 'class': 'details-update', 'click': function() { facet.update(container, ipa_details_cache[facet.entity_name][pkey_name][0]); return false; } })); details.append('
    '); details.append('
    '); for (var i = 0; i < facet.sections.length; ++i) { var section = facet.sections[i]; details.append($('

    ',{ click: function(){_h2_on_click(this)}, html:"− "+section.label })); var div = $('
    ', { 'id': facet.entity_name+'-'+facet.name+'-'+section.name, 'class': 'details-section' }).appendTo(details); section.create(div); details.append('
    '); } } function ipa_details_setup(container) { var that = this; for (var i = 0; i < that.sections.length; ++i) { var section = that.sections[i]; var div = $( '#'+that.entity_name+'-'+that.name+'-'+section.name, container ); section.setup(div); } } function ipa_details_load(container) { var that = this; var entity = IPA.get_entity(that.entity_name); that.pkey = $.bbq.getState(that.entity_name + '-pkey', true) || ''; if (!that.pkey && !entity.default_facet) return; function on_success(data, text_status, xhr) { var result = data.result.result; ipa_details_cache[that.entity_name] = $.extend(true, {}, result); for (var i = 0; i < that.sections.length; ++i) { var section = that.sections[i]; section.load(result); } } function on_failure(xhr, text_status, error_thrown) { var details = $('.details', container).empty(); details.append('

    Error: '+error_thrown.name+'

    '); details.append('

    '+error_thrown.title+'

    '); details.append('

    '+error_thrown.message+'

    '); } var params = []; if (that.pkey) params.push(that.pkey); ipa_cmd( 'show', params, {all: true, rights: true}, on_success, on_failure, that.entity_name ); } function ipa_details_update(container, pkey, on_win, on_fail) { var facet = this; var entity_name = facet.entity_name; function update_on_win(data, text_status, xhr) { if (on_win) on_win(data, text_status, xhr); if (data.error) return; var result = data.result.result; ipa_details_cache[entity_name] = $.extend(true, {}, result); facet.display(result); } function update_on_fail(xhr, text_status, error_thrown) { if (on_fail) on_fail(xhr, text_status, error_thrown); } if (!pkey) return; var values; var modlist = {'all': true, 'setattr': [], 'addattr': [], 'rights': true}; var attrs_wo_option = {}; for (var i=0; i 1){ modlist[field.name] = values; } else if (param_info['multivalue']){ modlist[field.name] = []; } } else { if (values.length) attrs_wo_option[field.name] = values; } } } for (attr in attrs_wo_option) { values = attrs_wo_option[attr]; modlist['setattr'].push(attr + '=' + values[0]); for (var i = 1; i < values.length; ++i) modlist['addattr'].push(attr + '=' + values[i]); } ipa_cmd('mod', [pkey], modlist, update_on_win, null, entity_name); } /* HTML templates for ipa_details_display() */ var _ipa_a_add_template = 'Add'; var _ipa_span_doc_template = 'Hint: D'; var _ipa_span_hint_template = 'Hint: D'; function ipa_details_display(result) { var facet = this; for (var i=0; i', { 'class': 'first', 'title': field_name }).append(content); } function ipa_create_other_dd(field_name, content){ return $('
    ', { 'class': 'other', 'title': field_name }).append(content); } function ipa_insert_first_dd(jobj, content){ ipa_insert_dd(jobj, content, "first"); } function ipa_insert_dd(jobj, content, dd_class){ jobj.after( $('
    ',{ "class": dd_class }).append(content)) } /* mapping of parameter types to handlers used to create inputs */ var _ipa_param_type_2_handler_map = { 'Str': _ipa_create_text_input, 'Int': _ipa_create_text_input, 'Bool': _ipa_create_text_input, 'List': _ipa_create_text_input }; /* create an HTML element for displaying/editing an attribute * arguments: * attr - LDAP attribute name * value - the attributes value */ function ipa_create_input(entity_name, attr, value,hint,rights) { var input = $("