(function ($, jsonpatch, angular) { var BASE_PATH = "rest/tapschema/"; var app = angular.module('editTapSchema', []); app.config(['$httpProvider', function ($httpProvider) { $httpProvider.defaults.headers.patch = { 'Content-Type': 'application/json;charset=utf-8' }; }]); function showAjaxError(errorResponse) { alert(errorResponse.statusText); } app.controller('mainController', ['$scope', '$http', function ($scope, $http) { $scope.model = {}; $scope.currentAddables = { containerName: null, entities: [] }; $scope.ucdModel = {}; function formPost(url, data, onSuccess) { $http({ method: 'POST', url: BASE_PATH + url, data: $.param(data), headers: {'Content-Type': 'application/x-www-form-urlencoded'} }).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) { formPost('select', { containerName: containerName, entityName: entityName }, updateModel); }; $scope.remove = function (containerName, entityName, event) { event.stopPropagation(); formPost('remove', { containerName: containerName, entityName: entityName }, updateModel); }; $scope.undoRemove = function (containerName) { formPost('undoremove', {containerName: containerName}, updateModel); }; $scope.edit = function (entity, key) { var editableField = $scope.model[entity].values[key]; $http.post(BASE_PATH + 'edit', { entity: entity, key: key, value: editableField.value }).then(function (response) { editableField.isChanged = response.data; }, showAjaxError); }; $scope.update = function () { $http.post(BASE_PATH + "update").then(function (response) { updateModel(response); alert('successfully updated'); }, showAjaxError); }; // add entities $scope.openAddablesDialog = function (containerName) { formPost('addables', {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); } } $http.post(BASE_PATH + 'add', { containerName: $scope.currentAddables.containerName, entities: selected }).then(function (response) { updateModel(response); $('#addablesModal').modal('hide'); }, showAjaxError); }; // Search UCD $scope.openUCDDialog = function () { formPost('searchUCD', {description: $scope.model.column.values.description.value}, function (response) { $scope.ucdModel = response.data; $('#searchUCDModal').modal('show'); }); }; function searchUCD() { formPost('searchUCD', {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) { formPost('explainUCD', {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'); $http.post(BASE_PATH + 'edit', { entity: 'column', key: 'ucd', value: $scope.ucdModel.selectedUCD }).then(function (response) { var ucd = $scope.model.column.values.ucd; ucd.value = newUcd; ucd.isChanged = response.data; }, showAjaxError); }; // Init $http.post(BASE_PATH + "model").then(function (response) { $scope.model = response.data; }, showAjaxError); }]); })(jQuery, jsonpatch, angular);