PolicySetsView.js revision 96de20ec25a9001dc3d113e983657571cd9a905e
0N/A/**
0N/A * The contents of this file are subject to the terms of the Common Development and
0N/A * Distribution License (the License). You may not use this file except in compliance with the
0N/A * License.
0N/A *
0N/A * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
0N/A * specific language governing permission and limitations under the License.
0N/A *
0N/A * When distributing Covered Software, include this CDDL Header Notice in each file and include
0N/A * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
0N/A * Header, with the fields enclosed by brackets [] replaced by your own identifying
0N/A * information: "Portions copyright [year] [name of copyright owner]".
0N/A *
0N/A * Portions copyright 2014-2015 ForgeRock AS.
0N/A */
0N/A
0N/A/*global define, FileReader*/
0N/A
0N/Adefine("org/forgerock/openam/ui/admin/views/realms/authorization/policySets/PolicySetsView", [
0N/A "jquery",
0N/A "underscore",
0N/A "backbone",
0N/A "backbone.paginator",
0N/A "backgrid",
0N/A "backgrid-filter",
0N/A "org/forgerock/commons/ui/common/backgrid/extension/ThemeablePaginator",
0N/A "backgrid.selectall",
0N/A "org/forgerock/commons/ui/common/main/Configuration",
0N/A "org/forgerock/commons/ui/common/main/EventManager",
0N/A "org/forgerock/commons/ui/common/main/Router",
0N/A "org/forgerock/commons/ui/common/util/Constants",
0N/A "org/forgerock/openam/ui/common/util/BackgridUtils",
0N/A "org/forgerock/openam/ui/common/util/URLHelper",
0N/A "org/forgerock/openam/ui/admin/models/authorization/PolicySetModel",
0N/A "org/forgerock/openam/ui/admin/views/realms/authorization/common/AbstractListView",
0N/A "org/forgerock/openam/ui/admin/delegates/PoliciesDelegate"
0N/A], function ($, _, Backbone, BackbonePaginator, Backgrid, BackgridFilter, ThemeablePaginator, BackgridSelectAll,
0N/A Configuration, EventManager, Router, Constants, BackgridUtils, URLHelper, PolicySetModel, AbstractListView,
0N/A PoliciesDelegate) {
0N/A return AbstractListView.extend({
0N/A template: "templates/admin/views/realms/authorization/policySets/PolicySetsTemplate.html",
0N/A // Used in AbstractListView
0N/A toolbarTemplate: "templates/admin/views/realms/authorization/policySets/PolicySetsToolbarTemplate.html",
0N/A events: {
0N/A "click #addNewPolicySet": "addNewPolicySet",
0N/A "click #importPolicies": "startImportPolicies",
0N/A "click #exportPolicies": "exportPolicies",
0N/A "change [name=upload]": "readImportFile"
0N/A },
0N/A render: function (args, callback) {
0N/A var self = this,
0N/A PolicySets,
0N/A columns,
0N/A grid,
0N/A paginator,
0N/A ClickableRow;
0N/A
0N/A this.realmPath = args[0];
0N/A this.data.selectedItems = [];
0N/A
0N/A PolicySets = Backbone.PageableCollection.extend({
0N/A url: URLHelper.substitute("__api__/applications"),
0N/A model: PolicySetModel,
0N/A state: BackgridUtils.getState(),
0N/A queryParams: BackgridUtils.getQueryParams({
0N/A filterName: "eq"
0N/A }),
0N/A parseState: BackgridUtils.parseState,
0N/A parseRecords: BackgridUtils.parseRecords,
0N/A sync: function (method, model, options) {
0N/A options.beforeSend = function (xhr) {
0N/A xhr.setRequestHeader("Accept-API-Version", "protocol=1.0,resource=2.0");
0N/A };
0N/A return BackgridUtils.sync(method, model, options);
0N/A }
0N/A });
0N/A
0N/A ClickableRow = BackgridUtils.ClickableRow.extend({
0N/A callback: function (e) {
0N/A var $target = $(e.target);
0N/A
0N/A if ($target.parents().hasClass("row-actions")) {
0N/A return;
0N/A }
0N/A
0N/A self.editRecord(e, this.model.id, Router.configuration.routes.realmsPolicySetEdit);
0N/A }
0N/A });
0N/A
0N/A columns = [
0N/A {
0N/A name: "name",
0N/A label: $.t("console.authorization.policySets.list.grid.0"),
0N/A cell: BackgridUtils.TemplateCell.extend({
0N/A iconClass: "fa-folder",
0N/A template: "templates/admin/backgrid/cell/IconAndNameCell.html",
0N/A rendered: function () {
0N/A this.$el.find("i.fa").addClass(this.iconClass);
0N/A }
0N/A }),
0N/A headerCell: BackgridUtils.FilterHeaderCell,
0N/A sortType: "toggle",
0N/A editable: false
0N/A },
0N/A {
0N/A name: "",
0N/A cell: BackgridUtils.TemplateCell.extend({
0N/A className: "row-actions",
0N/A template: "templates/admin/backgrid/cell/RowActionsCell.html",
0N/A events: {
0N/A "click .edit-row-item": "editItem",
0N/A "click .delete-row-item": "deleteItem"
0N/A },
0N/A editItem: function (e) {
0N/A self.editRecord(e, this.model.id, Router.configuration.routes.realmsPolicySetEdit);
0N/A },
0N/A deleteItem: function (e) {
0N/A self.deleteRecord(e, this.model.id);
0N/A }
0N/A }),
0N/A sortable: false,
0N/A editable: false
0N/A }
0N/A ];
0N/A
0N/A this.data.items = new PolicySets();
0N/A
0N/A grid = new Backgrid.Grid({
0N/A columns: columns,
0N/A row: ClickableRow,
0N/A collection: self.data.items,
0N/A className: "backgrid table table-hover",
0N/A emptyText: $.t("console.common.noResults")
0N/A });
0N/A
0N/A paginator = new Backgrid.Extension.ThemeablePaginator({
0N/A collection: self.data.items,
0N/A windowSize: 3
0N/A });
0N/A
0N/A this.bindDefaultHandlers();
0N/A
0N/A this.parentRender(function () {
0N/A this.renderToolbar();
0N/A
0N/A this.$el.find(".backgrid-container").append(grid.render().el);
0N/A this.$el.find(".panel-body").append(paginator.render().el);
0N/A
0N/A this.data.items.fetch({ reset: true }).done(function () {
0N/A if (callback) {
0N/A callback();
0N/A }
0N/A }).fail(function () {
0N/A Router.routeTo(Router.configuration.routes.realms, {
0N/A args: [],
0N/A trigger: true
0N/A });
0N/A });
0N/A });
0N/A },
0N/A
0N/A startImportPolicies: function () {
0N/A this.$el.find("[name=upload]").trigger("click");
0N/A },
0N/A
0N/A importPolicies: function (e) {
0N/A PoliciesDelegate.importPolicies(e.target.result)
0N/A .done(function () {
0N/A EventManager.sendEvent(Constants.EVENT_DISPLAY_MESSAGE_REQUEST, "policiesUploaded");
0N/A })
0N/A .fail(function (e) {
0N/A var applicationNotFoundInRealm = " application not found in realm",
0N/A responseText = e ? e.responseText : "",
0N/A messages = $($.parseXML(responseText)).find("message"),
0N/A message = messages.length ? messages[0].textContent : "",
0N/A index = message ? message.indexOf(applicationNotFoundInRealm) : -1;
0N/A
0N/A if (index > -1) {
0N/A EventManager.sendEvent(Constants.EVENT_DISPLAY_MESSAGE_REQUEST, {
0N/A key: "policiesImportFailed",
0N/A applicationName: message.slice(0, index)
0N/A });
0N/A } else {
0N/A EventManager.sendEvent(Constants.EVENT_DISPLAY_MESSAGE_REQUEST, "policiesUploadFailed");
0N/A }
0N/A });
0N/A },
0N/A
0N/A readImportFile: function () {
0N/A var file = this.$el.find("[name=upload]")[0].files[0],
0N/A reader = new FileReader();
0N/A reader.onload = this.importPolicies;
0N/A if (file) {
0N/A reader.readAsText(file, "UTF-8");
605N/A }
0N/A },
0N/A
0N/A exportPolicies: function () {
0N/A var realm = this.realmPath === "/" ? "" : this.realmPath;
0N/A this.$el.find("#exportPolicies").attr("href",
0N/A Constants.host + "/" + Constants.context + "/xacml" + realm + "/policies");
0N/A },
0N/A
0N/A addNewPolicySet: function (e) {
0N/A Router.routeTo(Router.configuration.routes.realmsPolicySetNew, {
0N/A args: [encodeURIComponent(this.realmPath)],
0N/A trigger: true
0N/A });
0N/A }
0N/A });
0N/A});
0N/A