mitweb: move entity model to entity module
[nit.git] / share / nitweb / javascripts / entities.js
index 255602c..2f7c6f5 100644 (file)
 
 (function() {
        angular
-               .module('entities', ['ui', 'model'])
+               .module('entities', ['ngSanitize', 'ui'])
 
-               .controller('EntityCtrl', ['Model', '$routeParams', '$scope', function(Model, $routeParams, $scope) {
-                       Model.loadEntity($routeParams.id,
+               .factory('Model', [ '$http', function($http) {
+                       return {
+
+                               loadEntity: function(id, cb, cbErr) {
+                                       $http.get('/api/entity/' + id)
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadEntityLinearization: function(id, cb, cbErr) {
+                                       $http.get('/api/linearization/' + id)
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadEntityDefs: function(id, cb, cbErr) {
+                                       $http.get('/api/defs/' + id)
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadEntityCode: function(id, cb, cbErr) {
+                                       $http.get('/api/code/' + id)
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               loadEntityGraph: function(id, cb, cbErr) {
+                                       $http.get('/api/graph/inheritance/' + id + '?cdepth=3')
+                                               .success(cb)
+                                               .error(cbErr);
+                               },
+
+                               search: function(q, n, cb, cbErr) {
+                                       $http.get('/api/search?q=' + q + '&n=' + n)
+                                               .success(cb)
+                                               .error(cbErr);
+                               }
+                       };
+               }])
+
+               .controller('EntityCtrl', ['Model', 'Metrics', 'Feedback', '$stateParams', '$scope', '$sce', function(Model, Metrics, Feedback, $stateParams, $scope, $sce) {
+                       $scope.entityId = $stateParams.id;
+
+                       this.loadEntityLinearization = function() {
+                               Model.loadEntityLinearization($stateParams.id,
+                                       function(data) {
+                                               $scope.linearization = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.loadEntityDefs = function() {
+                               Model.loadEntityDefs($stateParams.id,
+                                       function(data) {
+                                               $scope.defs = data;
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.loadEntityCode = function() {
+                               Model.loadEntityCode($stateParams.id,
+                                       function(data) {
+                                               $scope.code = data;
+                                       }, function(err) {
+                                               $scope.code = err;
+                                       });
+                       };
+
+                       this.loadEntityGraph = function(e) {
+                               Model.loadEntityGraph($stateParams.id,
+                                       function(data) {
+                                               $scope.graph = $sce.trustAsHtml(data);
+                                       }, function(err) {
+                                               $scope.error = err;
+                                       });
+                       };
+
+                       this.loadStructuralMetrics = function() {
+                               Metrics.loadStructuralMetrics($stateParams.id,
+                                       function(data) {
+                                               $scope.metrics = data;
+                                       }, function(message, status) {
+                                               $scope.error = {message: message, status: status};
+                                       });
+                       };
+
+                       Model.loadEntity($stateParams.id,
                                function(data) {
                                        $scope.mentity = data;
-                               }, function(err) {
-                                       $scope.error = err;
+                               }, function(message, status) {
+                                       $scope.error = {message: message, status: status};
                                });
                }])
 
                        };
                })
 
+               .directive('entityNamespace', function() {
+                       return {
+                               restrict: 'E',
+                               scope: {
+                                       namespace: '='
+                               },
+                               templateUrl: '/directives/entity/namespace.html',
+                               link: function ($scope, element, attrs) {
+                                       $scope.isObject = function(obj) {
+                                               return typeof obj === 'object';
+                                       };
+                                       $scope.isArray = function(obj) {
+                                               return Array.isArray(obj);
+                                       };
+                                       $scope.isString = function(obj) {
+                                               return typeof obj === 'string';
+                                       };
+                               }
+                       };
+               })
+
                .directive('entityTag', function() {
                        return {
                                restrict: 'E',
                        };
                })
 
-               .directive('entityCard', function() {
+               .directive('entityGraph', function() {
                        return {
                                restrict: 'E',
                                scope: {
-                                       mentity: '='
+                                       mentity: '=',
+                                       graph: '='
                                },
                                replace: true,
-                               templateUrl: '/directives/entity/card.html'
+                               templateUrl: '/directives/entity/graph.html'
                        };
                })
 
+               .directive('entityCard', ['Feedback', function(Feedback) {
+                       return {
+                               restrict: 'E',
+                               scope: {
+                                       mentity: '=',
+                                       defaultTab: '@',
+                                       noSynopsis: '='
+                               },
+                               replace: true,
+                               templateUrl: '/directives/entity/card.html',
+                               link: function ($scope, element, attrs) {
+                                       $scope.currentTab = $scope.defaultTab ? $scope.defaultTab : 'signature';
+
+                                       $scope.loadEntityStars = function() {
+                                               Feedback.loadEntityStars($scope.mentity.full_name,
+                                                       function(data) {
+                                                               $scope.ratings = data;
+                                                       }, function(message, status) {
+                                                               $scope.error = {message: message, status: status};
+                                                       });
+                                       };
+                               }
+                       };
+               }])
+
                .directive('entityList', function() {
                        return {
                                restrict: 'E',
                                scope: {
                                        listEntities: '=',
+                                       listId: '@',
                                        listTitle: '@',
                                        listObjectFilter: '=',
                                },
                                }
                        };
                })
+
+               .directive('entityLinearization', function() {
+                       return {
+                               restrict: 'E',
+                               scope: {
+                                       listEntities: '=',
+                                       listTitle: '@',
+                                       listFocus: '='
+                               },
+                               templateUrl: '/directives/entity/linearization.html'
+                       };
+               })
+
+               .directive('entityDef', ['Model', function(Model, Code) {
+                       return {
+                               restrict: 'E',
+                               scope: {
+                                       definition: '=',
+                                       focus: '='
+                               },
+                               templateUrl: '/directives/entity/defcard.html',
+                               link: function ($scope, element, attrs) {
+                                       $scope.codeId = 'code_' + $scope.definition.full_name.replace(/[^a-zA-Z0-9]/g, '_');
+
+                                       $scope.isActive = function() {
+                                               return $scope.focus.full_name == $scope.definition.full_name;
+                                       }
+
+                                       $scope.loadCardCode = function() {
+                                               if(!$scope.code) {
+                                                       Model.loadEntityCode($scope.definition.full_name,
+                                                               function(data) {
+                                                                       $scope.code = data;
+                                                                       setTimeout(function() { // smooth collapse
+                                                                               $('#' + $scope.codeId).collapse('show')
+                                                                       }, 1);
+                                                               }, function(err) {
+                                                                       $scope.code = err;
+                                                               });
+                                               } else {
+                                                       if($('#' + $scope.codeId).hasClass('in')) {
+                                                               $('#' + $scope.codeId).collapse('hide');
+                                                       } else {
+                                                               $('#' + $scope.codeId).collapse('show');
+                                                       }
+                                               }
+                                       };
+
+                                       if($scope.isActive()) $scope.loadCardCode();
+                               }
+                       };
+               }])
+
+               .controller('StarsCtrl', ['Feedback', '$scope', function(Feedback, $scope) {
+                       $ctrl = this;
+
+                       this.postStar = function(rating) {
+                               Feedback.postEntityStarDimension($scope.mentity.full_name,
+                               $scope.dimension, rating,
+                               function(data) {
+                                       $scope.mean = data.mean;
+                                       $scope.list = data.ratings;
+                                       $scope.user = data.user;
+                                       $ctrl.loadEntityStars($scope);
+                               }, function(err) {
+                                       $scope.err = err;
+                               });
+                       }
+
+                       this.loadEntityStars = function($scope) {
+                               Feedback.loadEntityStars($scope.mentity.full_name,
+                                       function(data) {
+                                               $scope.ratings = data;
+                                       }, function(message, status) {
+                                               $scope.error = {message: message, status: status};
+                                       });
+                       };
+               }])
+
+               .directive('entityRating', ['Feedback', function(Feedback) {
+                       return {
+                               restrict: 'E',
+                               scope: {
+                                       mentity: '=',
+                                       ratings: '='
+                               },
+                               controller: 'StarsCtrl',
+                               controllerAs: 'ratingsCtrl',
+                               templateUrl: '/directives/entity/rating.html'
+                       };
+               }])
+
+               .directive('entityStars', ['Feedback', function(Feedback) {
+                       return {
+                               restrict: 'E',
+                               scope: {
+                                       mentity: '=',
+                                       dimension: '@',
+                                       mean: '=',
+                                       list: '=',
+                                       user: '=',
+                                       refresh: '=',
+                                       ratings: '='
+                               },
+                               controller: 'StarsCtrl',
+                               controllerAs: 'starsCtrl',
+                               templateUrl: '/directives/entity/stars.html'
+                       };
+               }])
 })();