Skip to content
edit-tapschema.js 6.33 KiB
Newer Older
(function ($, jsonpatch, angular) {
    var app = angular.module('editTapSchema', []);
    function showAjaxError(errorResponse) {
        alert(errorResponse.statusText);
    app.controller('mainController', ['$scope', '$http', function ($scope, $http) {
            $scope.model = {};
            $scope.currentAddables = {
                containerName: null,
                entities: []
            $scope.ucdModel = {};

            function doAjax(action, method, data, onSuccess) {
                var httpParams = {
                    method: method,
                    url: REST_PATH + action + "?cid=" + CID
                };

                if (method === 'GET') {
                    if (data !== null) {
                        httpParams.url += "&" + $.param(data)
                    }
                } else {
                    if (data !== null) {
                        httpParams.data = JSON.stringify(data);
                    }
                    httpParams.headers = {'Content-Type': 'application/json;charset=utf-8'};
                }

                $http(httpParams).then(onSuccess, showAjaxError);
            function updateModel(response) {
                var patches = response.data;
                if (patches.length === 1 && patches[0].path === '/') {
                    // to resolve standard version issue
                    // see https://github.com/dharmafly/jsonpatch.js/issues/1
                    patches[0].path = '';
                $scope.model = jsonpatch.apply_patch($scope.model, patches);
            $scope.select = function (containerName, entityName) {
                doAjax('select', 'GET', {
                    containerName: containerName,
                    entityName: entityName
                }, updateModel);
            };
            $scope.remove = function (containerName, entityName, event) {
                event.stopPropagation();
                doAjax('remove', 'GET', {
                    containerName: containerName,
                    entityName: entityName
                }, updateModel);
            };
            $scope.undoRemove = function (containerName) {
                doAjax('undoremove', 'GET', {containerName: containerName}, updateModel);
            $scope.edit = function (entity, key) {
                var editableField = $scope.model[entity].values[key];
                doAjax('edit', 'POST', {
                    entity: entity,
                    key: key,
                    value: editableField.value
                }, function (response) {
                    editableField.isChanged = response.data;
                doAjax('update', 'POST', null, function (response) {
                    alert('Successfully updated');
                });
            // add entities
            $scope.openAddablesDialog = function (containerName) {
                doAjax('addables', 'GET', {containerName: containerName}, function (response) {
                    $scope.currentAddables.containerName = containerName;
                    $scope.currentAddables.entities = response.data;
                    $('#addablesModal').modal('show');
            function setSelectOnEntities(value) {
                var entities = $scope.currentAddables.entities;
                for (var i = 0; i < entities.length; i++) {
                    entities[i].selected = value;
                }
            }

            $scope.selectAllEntities = function () {
                setSelectOnEntities(true);
            };

            $scope.deselectAllEntities = function () {
                setSelectOnEntities(false);
            };

            $scope.addEntities = function () {
                var entities = $scope.currentAddables.entities;
                var selected = [];
                for (var i = 0; i < entities.length; i++) {
                    if (entities[i].selected) {
                        selected.push(entities[i].name);
                    }
                doAjax('add', 'POST', {
                    containerName: $scope.currentAddables.containerName,
                    entities: selected
                }, function (response) {
                    updateModel(response);
                    $('#addablesModal').modal('hide');
            // Search UCD
            $scope.openUCDDialog = function () {
                doAjax('searchUCD', 'GET', {description: $scope.model.column.values.description.value}, function (response) {
                    $scope.ucdModel = response.data;
                    $('#searchUCDModal').modal('show');
            function searchUCD() {
                doAjax('searchUCD', 'GET', {description: $scope.ucdModel.description}, function (response) {
                    $scope.ucdModel = response.data;
                });
            $scope.searchUCD = searchUCD;
            $scope.searchUCDByReturn = function (event) {
                if (event.keyCode === 13) {
                    searchUCD();
            $scope.explainUCD = function (ucdInfo, index) {
                doAjax('explainUCD', 'GET', {index: index}, function (response) {
                    ucdInfo.definition = response.data;
                });
            };
            $scope.selectUCD = function (ucd) {
                $scope.ucdModel.selectedUCD = ucd;
            };
            $scope.saveUCD = function () {
                var newUcd = $scope.ucdModel.selectedUCD;
                $('#searchUCDModal').modal('hide');
                doAjax('edit', 'POST', {
                    entity: 'column',
                    key: 'ucd',
                    value: $scope.ucdModel.selectedUCD
                }, function (response) {
                    var ucd = $scope.model.column.values.ucd;
                    ucd.value = newUcd;
                    ucd.isChanged = response.data;
            doAjax('model', 'GET', null, function (response) {
                console.log(response.data);
                $scope.model = response.data;
        }]);

})(jQuery, jsonpatch, angular);