/* 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 = {}; var ipa_entity_details_list = {}; function ipa_stanza(spec){ var that = {}; that.name = spec.name || ''; that.label = spec.label || ''; function input(spec){ /*Was ipa_details_field_setup*/ function setup(container, dl, section) { var obj_name = container.attr('title'); var title = this.name; var label = ''; var param_info = ipa_get_param_info(obj_name, this.name); if (param_info) label = param_info['label']; if (!label) label = this.label; $('
', { id: this.name, title: title, html: label + ':' }).appendTo(dl); } /*Was ipa_details_field_load*/ function load(container, dt, entry_attrs) { var obj_name = container.attr('id'); var multivalue = false; var hint_span = null; var dd; var param_info = ipa_get_param_info(obj_name, this.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 = entry_attrs[this.name]; if (value) { dd = ipa_create_first_dd( this.name, ipa_create_input(obj_name, this.name, value[0],hint_span) ); dt.after(dd); var last_dd = dd; for (var i = 1; i < value.length; ++i) { dd = ipa_create_other_dd( this.name, ipa_create_input(obj_name, this.name, value[i],hint_span) ); last_dd.after(dd); last_dd = dd; } if (multivalue) { dd = ipa_create_other_dd( this.name, _ipa_a_add_template.replace('A', this.name) ); last_dd.after(dd); } } else { if (multivalue) { dd = ipa_create_first_dd( this.name, _ipa_a_add_template.replace('A', this.name) /*.append(hint_span)*/ ); dt.after(dd); } else { dd = ipa_create_first_dd( this.name, ipa_create_input(obj_name, this.name, '') /*.append(hint_span)*/ ); dt.after(dd); } } } /*Was ipa_details_field_save*/ function save(container) { var field = this; var values = []; var dd = $('dd[title='+field.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; } that.fields.push(spec); that.controls[spec.name] = spec; if (!spec.setup){ spec.setup = setup; } if (!spec.load){ spec.load = load; } if (!spec.save){ spec.save = save; } return that; }; that.input = input; that.fields = []; that.controls={}; return that; }; function ipa_make_button(which,text,details_class){ var button_class= details_class + " ui-state-default ui-corner-all input_link "; return $('',{ "class": button_class }). append(' '). append(text); } function ipa_details_create(container, sections) { if (!container) { alert('ERROR: ipa_details_create: Missing container argument!'); return; } var obj_name = container.attr('id'); container.attr('title', obj_name); container.addClass('details-container'); var details = $('
', { 'class': 'details' }).appendTo(container); var buttons = $('
', { 'class': 'details-buttons' }).appendTo(details); buttons.append(ipa_make_button('ui-icon-refresh','Reset','details-reset')); buttons.append(ipa_make_button('ui-icon-check','Update','details-update')); details.append('
'); for (var i = 0; i < sections.length; ++i) { var section = sections[i]; ipa_details_section_setup(container, details, section); } } function ipa_details_section_setup(container, details, section) { var id = section.name; var name = section.label; var fields = section.fields; if (!fields) return; details.append($("

",{ click: function(){_h2_on_click(this)}, html:"− "+name })); var dl = $('
',{ id:id, "class":"entryattrs" }).appendTo(details); for (var i = 0; i < fields.length; ++i) { var field = fields[i]; field.setup(container, dl, section); } details.append('
'); } function ipa_details_load(container, pkey, on_win, on_fail) { var obj_name = container.attr('id'); function load_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[obj_name] = $.extend(true, {}, result); ipa_details_display(container, result); } function load_on_fail(xhr, text_status, error_thrown) { if (on_fail) on_fail(xhr, text_status, error_thrown); var details = $('.details', container).empty(); details.append('

Error: '+error_thrown.name+'

'); details.append('

URL: '+this.url+'

'); details.append('

'+error_thrown.message+'

'); } var params = [pkey]; if (!pkey){ params = []; } ipa_cmd( 'show', params, {all: true}, load_on_win, load_on_fail, obj_name ); } function ipa_details_update(container, pkey, on_win, on_fail) { var obj_name = container.attr('id'); 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[obj_name] = $.extend(true, {}, result); ipa_details_display(container, 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': []}; var attrs_wo_option = {}; var sections = ipa_entity_get_details_sections(obj_name); 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, update_on_fail, obj_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'; /* 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: * entry_attrs - 'result' field as returned by ipa *-show commnads * (basically an associative array with attr:value pairs) */ function ipa_details_display(container, entry_attrs) { var obj_name = container.attr('id'); /* remove all
tags i.e. all attribute values */ $('dd', container).remove(); /* go through all
tags and pair them with newly created
s */ var sections = ipa_entity_get_details_sections(obj_name); 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(obj_name, attr, value,hint) { var input = $("