$.noConflict(); var app = angular.module('ngCreateDiploma', ['ui.bootstrap']); app.controller('CreateDiplomaCtrl', ['$scope', '$modal', '$q', function($scope, $modal, $q) { $scope.bean = new ws_safemode_pt__legislacaoBean(); $scope.fileData = { fileName: 'Nenhum ficheiro seleccionado', selectedFile: null, fileSelected: false, fileTooltip: 'Adicionar ficheiro' }; jQuery("#btn").click(function() { if($scope.fileData.fileSelected === false) { jQuery("#pdf").click(); } else { $scope.fileData.selectedFile = null; } $scope.$apply(); }); $scope.$watch("fileData.selectedFile", function(value) { if( value !== null ) { $scope.fileData.fileSelected = true; $scope.fileData.fileName = value.replace(/.*(\/|\\)/, ''); } else { $scope.fileData.fileSelected = false; $scope.fileData.fileName = 'Nenhum ficheiro seleccionado'; } }); $scope.$watch("fileData.fileSelected", function(value) { if(value === false) { $scope.fileData.fileTooltip = 'Adicionar ficheiro'; } else { $scope.fileData.fileTooltip = 'Remover ficheiro'; } }); $scope.tipo = function() { if( $scope.tipos === undefined ) { var tipos = new ws_safemode_pt__LegislacaoWS_impl_ws_safemode_pt__LegislacaoWSImplPort(); tipos.getTipos(function(result) { $scope.tipos = result._return; $scope.$apply(); }, function(x, y) { if( x === 340 ) { window.location = "/safemode/pages/legislacao/createLegislacao.do"; } else $scope.tipos = []; }); } }; $scope.tipo(); $scope.estado = function() { if( $scope.estados === undefined ) { var estados = new ws_safemode_pt__LegislacaoWS_impl_ws_safemode_pt__LegislacaoWSImplPort(); estados.getEstados(function(result) { $scope.estados = result._return; var position = jQuery.inArray("Em vigor", $scope.estados); if( position !== -1 ) { $scope.bean._estado = $scope.estados[position]; } $scope.$apply(); }, function(x, y) { if( x === 340 ) { window.location = "/safemode/pages/legislacao/createLegislacao.do"; } else $scope.estados = []; }); } }; $scope.estado(); $scope.entidadeReguladora = function() { if( $scope.entidadesReguladoras === undefined ) { var diplomas = new ws_safemode_pt__LegislacaoWS_impl_ws_safemode_pt__LegislacaoWSImplPort(); diplomas.getEntidadesReguladoras(function(result) { $scope.entidadesReguladoras = result._return; $scope.$apply(); }, function(x, y) { if( x === 340 ) { window.location = "/safemode/pages/legislacao/createLegislacao.do"; } else deferred.resolve([]); }); } }; $scope.entidadeReguladora( ); $scope.getPalavrasChave = function(filter) { var deferred = $q.defer(); var palavrasChave = new ws_safemode_pt__LegislacaoWS_impl_ws_safemode_pt__LegislacaoWSImplPort(); palavrasChave.getPalavrasChave(function(result) { deferred.resolve(result._return); }, function(x, y) { if( x === 340 ) { window.location = "/safemode/pages/legislacao/createLegislacao.do"; } else deferred.resolve([]); }, filter); return deferred.promise; }; $scope.tags = { tags: function( filter ) { return $scope.getPalavrasChave( filter ).then(function(res) { return res; }); }, selected: null, addTag: function( ) { if(this.selected !== null) { $scope.bean._palavrasChave.push( this.selected ); this.selected = null; } }, removeTag: function(tag) { var position = jQuery.inArray(tag, $scope.bean._palavrasChave); if(position !== -1) { $scope.bean._palavrasChave.splice(position, 1); } } }; $scope.loadCategorias = function() { var wsClient = new ws_safemode_pt__LegislacaoWS_impl_ws_safemode_pt__LegislacaoWSImplPort( ); wsClient.getAllCategorias(function(x, y) { $scope.categorias.listaCategorias = []; jQuery.merge( $scope.categorias.listaCategorias, x.getReturn() ); $scope.$apply(); }, function(x, y) { if( x === 340 ) { window.location = "/safemode/pages/legislacao/createLegislacao.do"; } else alert( x + " " + y ); }); }; $scope.categorias = { listaCategorias: [], listaTemas: [], listaSubtemas: [], categoria: null, tema: null, subtema: null, add: function() { if( ( $scope.categorias.subtema !== null ) && ( $scope.categorias.subtema !== undefined ) && ( $scope.categorias.subtema !== "" ) ) { $scope.bean._subtemas.push($scope.categorias.subtema); } else if( ( $scope.categorias.tema !== null ) && ( $scope.categorias.tema !== undefined ) && ( $scope.categorias.tema !== "" ) ) { $scope.bean._subtemas.push($scope.categorias.tema); } $scope.categorias.subtema = null; $scope.categorias.tema = null; $scope.categorias.categoria = null; $scope.categorias.listaTemas = []; $scope.categorias.listaSubtemas = []; }, remove: function(item) { var position = jQuery.inArray(item, $scope.bean._subtemas); if (position !== -1) { $scope.bean._subtemas.splice(position, 1); } }, loadTemas: function(item) { var wsClient = new ws_safemode_pt__LegislacaoWS_impl_ws_safemode_pt__LegislacaoWSImplPort( ); wsClient.getAllTemas(function(x, y) { $scope.categorias.listaTemas = []; jQuery.merge( $scope.categorias.listaTemas, x.getReturn() ); $scope.$apply(); }, function(x, y) { if( x === 340 ) { window.location = "/safemode/pages/legislacao/createLegislacao.do"; } else alert( x + " " + y ); }, item); }, loadSubtemas: function(item) { var wsClient = new ws_safemode_pt__LegislacaoWS_impl_ws_safemode_pt__LegislacaoWSImplPort( ); wsClient.getAllSubtemas(function(x, y) { $scope.categorias.listaSubtemas = []; jQuery.merge( $scope.categorias.listaSubtemas, x.getReturn() ); $scope.$apply(); }, function(x, y) { if( x === 340 ) { window.location = "/safemode/pages/legislacao/createLegislacao.do"; } else alert( x + " " + y ); }, item); } }; $scope.loadCategorias(); $scope.$watch("categorias.categoria", function(value) { console.log(value); if ( (value !== undefined) && (value !== null) ) { $scope.categorias.loadTemas(value); } else { $scope.categorias.listaTemas = []; $scope.categorias.tema = null; } }); $scope.$watch("categorias.tema", function(value) { if ( (value !== undefined) && (value !== null) ) { $scope.categorias.loadSubtemas(value); } else { $scope.categorias.listaSubtemas = []; $scope.categorias.subtema = null; } }); $scope.getDiplomas = function(filter) { var deferred = $q.defer(); var diplomas = new ws_safemode_pt__LegislacaoWS_impl_ws_safemode_pt__LegislacaoWSImplPort(); diplomas.getDiplomas(function(result) { deferred.resolve(result._return); }, function(x, y) { if( x === 340 ) { window.location = "/safemode/pages/legislacao/createLegislacao.do"; } else deferred.resolve([]); }, filter); return deferred.promise; }; $scope.related = { tags: function( filter ) { return $scope.getDiplomas( filter ).then(function(res) { return res; }); }, selected: null, addTag: function( ) { if(this.selected !== null) { $scope.bean._diplomasRelacionados.push( this.selected ); this.selected = null; } }, removeTag: function(tag) { var position = jQuery.inArray(tag, $scope.bean._diplomasRelacionados); if(position !== -1) { $scope.bean._diplomasRelacionados.splice(position, 1); } } }; $scope.entidades = { selected: null, addTag: function( ) { if(this.selected !== null) { $scope.bean._entidadesReguladoras.push( this.selected ); this.selected = null; } }, removeTag: function(tag) { var position = jQuery.inArray(tag, $scope.bean._entidadesReguladoras); if(position !== -1) { $scope.bean._entidadesReguladoras.splice(position, 1); } } }; $scope.revogados = { tags: function( filter ) { return $scope.getDiplomas( filter ).then(function(res) { return res; }); }, selected: null, addTag: function( ) { if(this.selected !== null) { var modalInstance = $modal.open({ templateUrl: 'revogados.html', controller: 'ModalRevogadosCtrl' }); modalInstance.result.then(function (selectedItem) { var resultado = new ws_safemode_pt__alteracaoBean(); resultado._id = $scope.revogados.selected._id; resultado._designacao = $scope.revogados.selected._designacao; resultado._detalhes = selectedItem; $scope.bean._diplomasRevogados.push( resultado ); $scope.revogados.selected = null; }, function () { console.log(''); }); } }, removeTag: function(tag) { var position = jQuery.inArray(tag, $scope.bean._diplomasRevogados); if(position !== -1) { $scope.bean._diplomasRevogados.splice(position, 1); } } }; $scope.republicados = { tags: function( filter ) { return $scope.getDiplomas( filter ).then(function(res) { return res; }); }, selected: null, addTag: function( ) { if(this.selected !== null) { var modalInstance = $modal.open({ templateUrl: 'republicados.html', controller: 'ModalRepublicadosCtrl' }); modalInstance.result.then(function (selectedItem) { var resultado = new ws_safemode_pt__alteracaoBean(); resultado._id = $scope.republicados.selected._id; resultado._designacao = $scope.republicados.selected._designacao; resultado._detalhes = selectedItem; $scope.bean._diplomasRepublicados.push( resultado ); $scope.republicados.selected = null; }, function () { console.log(''); }); } }, removeTag: function(tag) { var position = jQuery.inArray(tag, $scope.bean._diplomasRepublicados); if(position !== -1) { $scope.bean._diplomasRepublicados.splice(position, 1); } } }; $scope.alterados = { tags: function( filter ) { return $scope.getDiplomas( filter ).then(function(res) { return res; }); }, selected: null, addTag: function( ) { if(this.selected !== null) { var modalInstance = $modal.open({ templateUrl: 'alterados.html', controller: 'ModalAlteradosCtrl' }); modalInstance.result.then(function (selectedItem) { var resultado = new ws_safemode_pt__alteracaoBean(); resultado._id = $scope.alterados.selected._id; resultado._designacao = $scope.alterados.selected._designacao; resultado._detalhes = selectedItem; $scope.bean._diplomasAlterados.push( resultado ); $scope.alterados.selected = null; }, function () { console.log(''); }); } }, removeTag: function(tag) { var position = jQuery.inArray(tag, $scope.bean._diplomasAlterados); if(position !== -1) { $scope.bean._diplomasAlterados.splice(position, 1); } } }; $scope.data = { numero: null, publicacao: null, publicacao_string: null, file: null, estado: null, tipo: null, observacoes: null, disposicoesTransitorias: null, palavrasChave: null }; $scope.submit = function( ) { jQuery("body").css("cursor", "wait"); var diplomas = new ws_safemode_pt__LegislacaoWS_impl_ws_safemode_pt__LegislacaoWSImplPort(); diplomas.addDiploma(function(result) { jQuery("body").css("cursor", "auto"); alert("Diploma adicionado com successo.\nA enviar ficheiro pdf..."); //Send the pdf file... jQuery( "#createLeg" ).submit(); }, function(x, y) { jQuery("body").css("cursor", "auto"); if( x === 340 ) { window.location = "/safemode/pages/legislacao/createLegislacao.do"; } else alert("Erro ao criar o diploma, por favor verifique se os dados estão bem preenchidos"); }, $scope.bean); }; $scope.tipo = null; $scope.add = function() { $scope.tag.tags.append($scope.tag.selected); }; jQuery('#dataPublicacaoControl').datepicker({ weekStart: 1, viewMode: 0 }).on('changeDate', function(ev) { $scope.$apply(function() { console.log($scope.bean); $scope.data.publicacao = ev.date; var day = ev.date.getDate().toString(); if (day.length == 1) { day = "0" + day; } var month = ev.date.getMonth() + 1 + ""; if (month.length == 1) { month = "0" + month; } var year = ev.date.getFullYear().toString(); $scope.bean._data_publicacao = day + "/" + month + "/" + year; }); }); }]); app.controller('ModalRevogadosCtrl', ['$scope', '$modalInstance', function ($scope, $modalInstance) { $scope.selected = { item: '' }; $scope.ok = function () { $modalInstance.close($scope.selected.item); }; $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; }]); app.controller('ModalRepublicadosCtrl', ['$scope', '$modalInstance', function ($scope, $modalInstance) { $scope.selected = { item: '' }; $scope.ok = function () { $modalInstance.close($scope.selected.item); }; $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; }]); app.controller('ModalAlteradosCtrl', ['$scope', '$modalInstance', function ($scope, $modalInstance) { $scope.selected = { item: '' }; $scope.ok = function () { $modalInstance.close($scope.selected.item); }; $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; }]);