PoliciesDialogView.js revision e26e5073e1266868172d72453c97f413fe2fb603
325N/A/**
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
325N/A *
325N/A * Copyright (c) 2015 ForgeRock AS. All rights reserved.
325N/A *
325N/A * The contents of this file are subject to the terms
325N/A * of the Common Development and Distribution License
325N/A * (the License). You may not use this file except in
325N/A * compliance with the License.
325N/A *
325N/A * You can obtain a copy of the License at
325N/A * http://forgerock.org/license/CDDLv1.0.html
325N/A * See the License for the specific language governing
325N/A * permission and limitations under the License.
325N/A *
325N/A * When distributing Covered Code, include this CDDL
325N/A * Header Notice in each file and include the License file
325N/A * at http://forgerock.org/license/CDDLv1.0.html
325N/A * If applicable, add the following below the CDDL Header,
325N/A * with the fields enclosed by brackets [] replaced by
325N/A * your own identifying information:
325N/A * "Portions Copyrighted [year] [name of copyright owner]"
325N/A */
325N/A
325N/A/*global define */
325N/A
325N/Adefine("org/forgerock/openidm/ui/admin/mapping/behaviors/PoliciesDialogView", [
325N/A "jquery",
325N/A "underscore",
325N/A "org/forgerock/commons/ui/common/main/AbstractView",
325N/A "org/forgerock/commons/ui/common/main/Configuration",
325N/A "org/forgerock/commons/ui/common/util/UIUtils",
325N/A "org/forgerock/openidm/ui/admin/util/InlineScriptEditor",
325N/A "org/forgerock/openidm/ui/admin/mapping/util/LinkQualifierFilterEditor",
325N/A "org/forgerock/openidm/ui/admin/util/WorkflowWidget",
325N/A "bootstrap-dialog"
325N/A], function($, _,
325N/A AbstractView,
325N/A conf,
325N/A uiUtils,
325N/A InlineScriptEditor,
325N/A LinkQualifierFilterEditor,
325N/A WorkflowWidget,
325N/A BootstrapDialog) {
325N/A
325N/A var PoliciesDialogView = AbstractView.extend({
325N/A template: "templates/admin/mapping/behaviors/PoliciesDialogTemplate.html",
325N/A el: "#dialogs",
325N/A events: {
325N/A "click .tabButtons .btn": "sectionControl"
325N/A },
325N/A model: {},
325N/A data: {},
325N/A
325N/A render: function(args, callback) {
325N/A var _this = this,
325N/A title = "";
325N/A
325N/A this.data = {
325N/A star: "★",
325N/A hollowStar: "☆",
325N/A situation: args.situation,
325N/A edit: args.edit,
325N/A defaultAction: false,
325N/A defaultWorkflow: false,
325N/A defaultScript: false,
325N/A defaultConditionScript: false,
325N/A defaultConditionFilter: true,
325N/A note: args.basePolicy.note,
325N/A mappingName: args.mappingName,
325N/A mappingProperties: args.mapProps
325N/A };
325N/A
325N/A this.model.policy = args.policy;
325N/A this.model.basePolicy = args.basePolicy;
325N/A this.model.saveCallback = args.saveCallback;
325N/A this.model.lookup = args.lookup;
325N/A this.model.currentDialog = $('<div id="SituationPolicyDialog"></div>');
325N/A this.model.passedProperties = [];
325N/A
325N/A if (this.data.edit) {
325N/A title = "Edit Policy for Situation: " + this.model.lookup[this.data.situation];
325N/A this.model.action = this.model.policy.action;
325N/A this.model.condition = this.model.policy.condition;
325N/A this.model.postAction = this.model.policy.postAction;
325N/A } else {
325N/A title = "Add Policy for Situation: " + this.model.lookup[this.data.situation];
325N/A this.model.action = this.model.basePolicy.action;
325N/A this.model.condition = this.model.basePolicy.condition;
325N/A this.model.postAction = this.model.basePolicy.postAction;
325N/A }
325N/A
325N/A if (_(this.model.action).isObject() && _(this.model.action).has("file") && this.model.action.file === "workflow/triggerWorkflowFromSync.js") {
325N/A this.data.defaultWorkflow = true;
325N/A } else if (_(this.model.action ).isObject() && _(this.model.action ).has("type")) {
325N/A this.data.defaultScript = true;
325N/A } else {
325N/A this.data.defaultAction = true;
325N/A }
325N/A
325N/A if (_(this.model.condition).isObject() && _(this.model.condition).has("type")) {
325N/A this.data.defaultConditionScript = true;
325N/A this.data.defaultConditionFilter = false;
325N/A }
325N/A
325N/A this.setElement(this.model.currentDialog);
325N/A
325N/A $('#dialogs').append(this.model.currentDialog);
325N/A BootstrapDialog.show({
325N/A title: title,
325N/A size: BootstrapDialog.SIZE_WIDE,
325N/A type: BootstrapDialog.TYPE_DEFAULT,
325N/A message: this.model.currentDialog,
325N/A onshown : function (dialogRef) {
325N/A uiUtils.renderTemplate(
325N/A _this.template,
325N/A _this.$el,
325N/A _.extend({}, conf.globalData, _this.data),
325N/A _.bind(function() {
325N/A var tempSelector,
325N/A actionScriptData = {},
325N/A workflowName,
325N/A conditionScriptData = {},
325N/A conditionFilter = "",
325N/A params = {};
325N/A
325N/A this.$el.find(".nav-tabs").tabdrop();
325N/A
325N/A // Set viable options stars
325N/A _(this.model.basePolicy.options).each(function(action) {
325N/A tempSelector = $("#defaultActionPane select option[value='"+action+"']");
325N/A tempSelector.html(tempSelector.text() + " " + this.data.hollowStar);
325N/A }, this);
325N/A
325N/A // Set default option star
325N/A tempSelector = $("#defaultActionPane select option[value='"+this.model.basePolicy.action+"']");
325N/A tempSelector.html(tempSelector.text() + " " + this.data.star);
325N/A
325N/A this.$el.find("#defaultActionPane select").val(this.model.basePolicy.action);
325N/A
325N/A // Set action value
325N/A if (this.data.defaultWorkflow) {
325N/A if (this.model.action.globals){
325N/A workflowName = this.model.action.globals.workflowName;
325N/A } else {
325N/A workflowName = $.t("templates.situationalPolicies.workflow");
325N/A }
325N/A } else if (this.data.defaultScript) {
325N/A actionScriptData = this.model.action;
325N/A } else if (this.data.edit) {
325N/A this.$el.find("#defaultActionPane select").val(this.model.action);
325N/A }
325N/A
325N/A // Set condition value
325N/A if (this.data.defaultConditionScript) {
325N/A conditionScriptData = this.model.condition;
325N/A } else {
325N/A conditionFilter = this.model.condition;
325N/A }
325N/A
325N/A // Load Script Editors
325N/A this.model.actionScriptPaneEditor = InlineScriptEditor.generateScriptEditor({
325N/A "element": this.$el.find("#actionScriptPane"),
"eventName": "actionScriptPane",
"disableValidation": false,
"validationCallback": _.bind(function(valid) {
if (this.model.currentActionTab === "scriptTab") {
if (valid) {
this.toggleActiveAlert(false);
} else {
this.toggleActiveAlert(true);
}
}
}, this),
"scriptData": actionScriptData
});
if (_.has(this.model.action, "globals") && _.has(this.model.action.globals, "params")) {
params = this.model.action.globals.params;
}
this.model.actionWorkflowEditor = WorkflowWidget.generateWorkflowWidget({
"element": this.$el.find("#actionWorkflowPane"),
"key": workflowName,
"params": params,
"sync": true,
"changeCallback": _.noop()
}, _.bind(function(valid) {
if (this.model.currentActionTab === "workflowTab") {
if (valid) {
this.toggleActiveAlert(false);
} else {
this.toggleActiveAlert(true);
}
}
}, this));
this.model.conditionScriptPaneEditor = InlineScriptEditor.generateScriptEditor({
"element": this.$el.find("#conditionScriptPane"),
"eventName": "conditionScriptPane",
"disableValidation": true,
"scriptData": conditionScriptData
});
this.model.postActionScriptPaneEditor = InlineScriptEditor.generateScriptEditor({
"element": this.$el.find("#postActionScript"),
"eventName": "postActionScript",
"disableValidation": true,
"scriptData": this.model.postAction
});
// Load Query Filter Editor
this.model.conditionFilterEditor = new LinkQualifierFilterEditor();
_.each(this.data.mappingProperties, _.bind(function(property){
this.model.passedProperties.push(property.source);
}, this));
this.model.conditionFilterEditor.render({
"mappingName" : this.data.mappingName,
"mapProps": this.model.passedProperties,
"queryFilter": conditionFilter || "",
"element": "#conditionFilterPane",
"resource" : ""
});
this.$el.find('a[data-toggle="tab"]').on('shown.bs.tab', _.bind(function (e) {
this.model.actionScriptPaneEditor.refresh();
this.model.conditionScriptPaneEditor.refresh();
this.model.postActionScriptPaneEditor.refresh();
}, this));
this.model.currentActionTab = this.$el.find("#action .tabButtons .active").attr("id");
this.actionValidation(this.model.currentActionTab);
this.$el.find('#action .tabButtons .btn').on('shown.bs.tab', _.bind(function (e) {
this.model.currentActionTab = $(e.target).attr("id");
this.actionValidation(this.model.currentActionTab);
}, this));
}, _this),
"replace");
},
buttons: [
{
label: $.t("common.form.cancel"),
action: function(dialogRef) {
dialogRef.close();
}
},
{
label: $.t("common.form.submit"),
id: "submitPolicyDialog",
cssClass: "btn-primary",
action: _.bind(function(dialogRef) {
var temp,
returnPolicy = {
"situation": this.data.situation
};
// Get action
if (this.$el.find("#action .tabButtons .active").attr("id") === "defaultActionTab") {
returnPolicy.action = this.$el.find("#defaultActionPane select").val();
} else if (this.$el.find("#action .tabButtons .active").attr("id") === "workflowTab") {
returnPolicy.action = this.model.actionWorkflowEditor.getConfiguration();
} else {
returnPolicy.action = this.model.actionScriptPaneEditor.generateScript();
}
// Get Condition
temp = this.$el.find("#restrict .tabButtons .active").attr("id");
if (temp === "conditionFilterTab" && this.model.conditionFilterEditor.getFilterString().length > 0) {
returnPolicy.condition = this.model.conditionFilterEditor.getFilterString();
} else if (temp === "conditionScriptTab" && this.model.conditionScriptPaneEditor.generateScript() !== null) {
returnPolicy.condition = this.model.conditionScriptPaneEditor.generateScript();
}
temp = this.model.postActionScriptPaneEditor.generateScript();
if (temp !== null) {
returnPolicy.postAction = temp;
}
this.model.saveCallback(returnPolicy);
dialogRef.close();
}, this)
}]
});
},
actionValidation: function(id) {
if (id === "workflowTab") {
if (!this.model.actionWorkflowEditor.getValid()) {
this.toggleActiveAlert(true);
} else {
this.toggleActiveAlert(false);
}
} else if (id === "scriptTab") {
if (this.model.actionScriptPaneEditor.generateScript() === null) {
this.toggleActiveAlert(true);
} else {
this.toggleActiveAlert(false);
}
} else {
this.toggleActiveAlert(false);
}
},
toggleActiveAlert: function(show) {
if (show) {
this.$el.find(".activeAlert").show();
this.$el.parentsUntil(".modal-dialog").find("#submitPolicyDialog").prop("disabled", true);
} else {
this.$el.find(".activeAlert").hide();
this.$el.parentsUntil(".modal-dialog").find("#submitPolicyDialog").prop("disabled", false);
}
},
sectionControl: function(event) {
var selected = $(event.target);
selected.parent().find('.active').removeClass('active');
}
});
return new PoliciesDialogView();
});