Files
freeipa/install/ui/add.js
T

173 lines
5.0 KiB
JavaScript
Raw Normal View History

/*jsl:import ipa.js */
2010-08-23 22:32:23 -04:00
/* Authors:
2010-09-16 10:28:07 -04:00
* Pavel Zuna <pzuna@redhat.com>
2010-10-27 22:32:30 -05:00
* Endi Sukma Dewata <edewata@redhat.com>
2010-08-23 22:32:23 -04:00
*
* Copyright (C) 2010 Red Hat
* see file 'COPYING' for use and warranty information
*
2010-12-09 13:59:11 +01:00
* 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, either version 3 of the License, or
* (at your option) any later version.
2010-08-23 22:32:23 -04:00
*
* 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
2010-12-09 13:59:11 +01:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2010-09-16 10:28:07 -04:00
*/
2010-08-23 22:32:23 -04:00
/* REQUIRES: ipa.js */
2011-01-12 19:51:22 -05:00
IPA.add_dialog = function (spec) {
2010-10-27 22:32:30 -05:00
spec = spec || {};
2011-01-12 19:51:22 -05:00
var that = IPA.dialog(spec);
2010-11-09 14:22:31 -06:00
2011-05-27 11:32:17 -04:00
that.method = spec.method || 'add';
2011-07-08 12:06:30 -04:00
that.pre_execute_hook = spec.pre_execute_hook;
2011-08-05 17:12:21 +02:00
that.on_error = spec.on_error ;
that.retry = typeof spec.retry !== 'undefined' ? spec.retry : true;
that.command = null;
2010-11-09 14:22:31 -06:00
2011-07-25 12:15:14 -04:00
function show_edit_page(entity,result){
var pkey_name = entity.metadata.primary_key;
2011-06-22 13:21:40 -04:00
var pkey = result[pkey_name];
if (pkey instanceof Array) {
pkey = pkey[0];
}
2011-07-25 12:15:14 -04:00
IPA.nav.show_entity_page(that.entity, 'default', pkey);
2011-06-22 13:21:40 -04:00
}
that.show_edit_page = spec.show_edit_page || show_edit_page;
2011-01-18 12:12:47 -05:00
that.add = function(record, on_success, on_error) {
2010-10-25 19:55:57 -04:00
2011-05-06 17:04:09 -04:00
var field, value, pkey_prefix;
2011-07-25 12:15:14 -04:00
var pkey_name = that.entity.metadata.primary_key;
2010-10-27 22:32:30 -05:00
2011-01-18 12:12:47 -05:00
var command = IPA.command({
2011-07-25 12:15:14 -04:00
entity: that.entity.name,
2011-05-27 11:32:17 -04:00
method: that.method,
2011-08-05 17:12:21 +02:00
retry: that.retry,
2011-01-18 12:12:47 -05:00
on_success: on_success,
on_error: on_error
});
2011-08-05 17:12:21 +02:00
that.command = command;
2010-10-27 22:32:30 -05:00
2011-07-25 12:15:14 -04:00
pkey_prefix = that.entity.get_primary_key_prefix();
2011-05-06 17:04:09 -04:00
for (var h=0; h<pkey_prefix.length; h++) {
command.add_arg(pkey_prefix[h]);
}
2011-01-28 16:46:19 -05:00
2011-05-16 22:23:20 -05:00
var fields = that.fields.values;
for (var i=0; i<fields.length; i++) {
fields[i].validate();
}
2011-07-06 15:43:50 -04:00
var required_fields_filled = true;
for (i=0; i<fields.length; i++) {
2011-05-16 22:23:20 -05:00
field = fields[i];
if (!field.valid) return;
2011-01-18 12:12:47 -05:00
2011-07-06 15:43:50 -04:00
required_fields_filled = field.check_required() &&
required_fields_filled;
2011-01-28 16:46:19 -05:00
value = record[field.name];
2011-01-18 12:12:47 -05:00
if (!value) continue;
if (field.name == pkey_name) {
command.add_arg(value);
} else {
command.set_option(field.name, value);
}
2010-10-25 19:55:57 -04:00
}
2010-09-16 10:28:07 -04:00
2011-05-27 12:04:20 -05:00
var sections = that.sections.values;
for (var j=0; j<sections.length; j++) {
var section = sections[j];
2011-01-28 16:46:19 -05:00
2011-05-16 22:23:20 -05:00
var section_fields = section.fields.values;
for (var k=0; k<section_fields.length; k++) {
field = section_fields[k];
2011-01-28 16:46:19 -05:00
if (!field.valid) return;
2011-07-06 15:43:50 -04:00
required_fields_filled = field.check_required() &&
required_fields_filled;
2011-01-28 16:46:19 -05:00
value = record[field.name];
if (!value) continue;
if (field.name == pkey_name) {
command.add_arg(value);
} else {
command.set_option(field.name, value);
}
}
}
2011-01-28 09:36:25 -06:00
//alert(JSON.stringify(command.to_json()));
2011-07-08 12:06:30 -04:00
if (that.pre_execute_hook){
that.pre_execute_hook(command);
}
2011-07-06 15:43:50 -04:00
if (required_fields_filled){
command.execute();
}
2011-07-08 12:06:30 -04:00
2010-09-16 10:28:07 -04:00
};
2011-07-25 12:15:14 -04:00
/*dialog initialization*/
that.add_button(IPA.messages.buttons.add, function() {
var record = {};
that.save(record);
that.add(
record,
function(data, text_status, xhr) {
var facet = IPA.current_entity.get_facet();
var table = facet.table;
table.refresh();
that.close();
2011-08-05 17:12:21 +02:00
},
that.on_error);
2011-07-25 12:15:14 -04:00
});
that.add_button(IPA.messages.buttons.add_and_add_another, function() {
var record = {};
that.save(record);
that.add(
record,
function(data, text_status, xhr) {
var facet = IPA.current_entity.get_facet();
var table = facet.table;
table.refresh();
that.reset();
2011-08-05 17:12:21 +02:00
},
that.on_error);
2011-07-25 12:15:14 -04:00
});
that.add_button(IPA.messages.buttons.add_and_edit, function() {
var record = {};
that.save(record);
that.add(
record,
function(data, text_status, xhr) {
that.close();
var result = data.result.result;
that.show_edit_page(that.entity,result);
2011-08-05 17:12:21 +02:00
},
that.on_error);
2011-07-25 12:15:14 -04:00
});
that.add_button(IPA.messages.buttons.cancel, function() {
that.close();
});
2010-10-27 22:32:30 -05:00
return that;
};
2010-08-23 22:32:23 -04:00