'use strict'; angular.module('warehost') .controller('WebmenuWebCtrl',function(session,config,alert,$scope,$rootScope,$stateParams,$http){ $scope.data = []; $scope.urllist = []; alert.set({}); $scope.treeOptions = { accept: function(){ return true; }, beforeDrop: function (){ return true; }, dropped: function(e){ $scope.obj = e.source.nodeScope.$modelValue; $scope.obj.position = e.dest.index; if(e.dest.nodesScope.$nodeScope){ $scope.obj.parentid = e.dest.nodesScope.$nodeScope.$modelValue.ID; }else{ $scope.obj.parentid = null; } $http.patch(config.api+'/web/website/'+$stateParams.websiteid+'/menu/'+$scope.obj.ID,$scope.obj).then(submitresult); } }; function load() { $scope.urllist = []; $http.get(config.api+'/web/website/'+$stateParams.websiteid+'/page').then(function(res){ res.data.data.forEach(function (d) { var url = d.url; url.type = 'page'; url.obj = d; url.obj.url = null; $scope.urllist.push(url); }); $http.get(config.api+'/web/website/'+$stateParams.websiteid+'/blog').then(function(res){ res.data.data.forEach(function (d) { var url = d.url; url.type = 'blog'; url.obj = d; url.obj.url = null; $scope.urllist.push(url); }); $http.get(config.api+'/web/website/'+$stateParams.websiteid+'/menu').then(function(res){ session.set(res); $scope.data = res.data.data; }); }); }); } function submitresult(res){ session.set(res); alert.set(res); if(res.data.data){ load(); } } load(); $rootScope.$on('warehost.session',load); $scope.edit = function(a){ $scope.obj = a.$modelValue; }; $scope.save = function(){ if($scope.obj.ID){ $http.patch(config.api+'/web/website/'+$stateParams.websiteid+'/menu/'+$scope.obj.ID,$scope.obj).then(submitresult); }else{ $http.post(config.api+'/web/website/'+$stateParams.websiteid+'/menu',$scope.obj).then(submitresult); } }; $scope.delete = function(a){ $http.delete(config.api+'/web/website/'+$stateParams.websiteid+'/menu/'+a.$modelValue.ID).then(submitresult); }; $scope.add = function(a){ $scope.obj = {}; if(a){ $scope.obj.parentid = a.$modelValue.ID; $scope.parent = a.$modelValue; }else{ $scope.parent = null; } }; });