Guest User

app.js

a guest
Apr 6th, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. var app = angular.module('app',['ui.router', 'ui.bootstrap', 'ngRoute']);
  2.  
  3.  
  4. app.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider){
  5. $routeProvider
  6. .when("/", {
  7. templateUrl: "index.html",
  8. controller: "mainCtrl"
  9. })
  10. .when("/login", {
  11. templateUrl: "login.html",
  12. controller: "loginCtrl"
  13. })
  14. .when("/adduser",{
  15. templateUrl: "signUp.html",
  16. controller: "adduserCtrl"
  17. })
  18. }]);
  19. /*
  20. app.config(['$urlRouterProvider','$stateProvider'],function($urlRouterProvider,$stateProvider){
  21. $urlRouterProvider.otherwise('/');
  22.  
  23. $stateProvider
  24. .state('home',{
  25. url:'/',
  26. templateUrl:'partials/login.html'
  27. })
  28.  
  29. });*/
  30.  
  31.  
  32.  
  33.  
  34. app.controller("mainCtrl",function($scope,$location,$http,$uibModal){
  35.  
  36. //$http.get('/getAllTweets').success(function(res){
  37. // $scope.tweets = res;
  38. //})
  39.  
  40.  
  41. $scope.getItem = function(){
  42. query = '/item/'+$scope.tweetId;
  43. var result = [];
  44. $http.get(query).success(function(res){
  45. result.push(res.item);
  46. $scope.tweets = result;
  47.  
  48. })
  49. }
  50. $scope.searchTweets = function(){
  51. var time, lim;
  52. if($scope.searchTweet ===""){
  53. $scope.searchTweet = null;
  54. }
  55. if($scope.limit ===""){
  56. $scope.limit = null;
  57. }
  58. if(!angular.isUndefined($scope.searchTweet)){
  59. time = parseInt($scope.searchTweet);
  60. }
  61.  
  62. var jsonPost = {
  63. timestamp: time,
  64. limit: $scope.searchLimit
  65. }
  66. console.log(jsonPost);
  67. $http.post('/search',jsonPost).success(function(res){
  68. console.log(res);
  69. $scope.tweets = res.items;
  70. })
  71. }
  72.  
  73. $scope.logout = function(){
  74. $http.post('/logout').success(function(res){
  75. if(res.status === "OK"){
  76. window.location.href = "/";
  77. }
  78.  
  79. })
  80. }
  81.  
  82. $scope.deleteTweet = function(){
  83. console.log("DELETE TWEET CALLED")
  84. var query = '/item/' + $scope.deleteTweetId;
  85. $http.delete(query).success(function(res){
  86. if(res.status == "OK"){
  87. alert("TWEET DELETED");
  88. }
  89. })
  90. }
  91.  
  92. $scope.additem = function(){
  93. var jsonToPost = {
  94. content: $scope.tweet,
  95. parent: 'none'
  96. };
  97.  
  98. $http.post('/additem',jsonToPost).success(function(res){
  99. if(res.status === "OK"){
  100. alert("Tweet id: "+res.id+ "was posted");
  101. }
  102. else{
  103. alert("Fail");
  104. }
  105. //console.log(res);
  106. })
  107. }
  108.  
  109. $scope.follow = function(){
  110. var jsonToPost = {
  111. username: $scope.userToFollow,
  112. follow: true
  113. }
  114.  
  115. $http.post('/follow',jsonToPost).success(function(res){
  116. if (res.status === "OK"){
  117. alert("FOLLOWED");
  118. }
  119. else{
  120. alert("Fail");
  121. }
  122. })
  123. }
  124.  
  125.  
  126. $scope.openSearch = function(){
  127. var modalInstance = $uibModal.open({
  128. templateUrl: '/searchModal.html',
  129. controller: 'searchCtrl'
  130. })
  131. modalInstance.result.then(function (data) {
  132. $scope.tweets = data.items;
  133. })
  134. }
  135.  
  136. $scope.getUserFollowers = function(){
  137. $http.get('/user/' + $scope.usernameFollowers + '/followers').success(function(res){
  138. console.log(res);
  139. var modalInstance = $uibModal.open({
  140. templateUrl: '/followers.html',
  141. controller: 'followersCtrl',
  142. resolve: {
  143. followers: function(){
  144. return res.users;
  145. }
  146. }
  147. })
  148. })
  149. }
  150.  
  151. $scope.getUserFollowings = function(){
  152. console.log("WAS CALLED")
  153. $http.get('/user/' + $scope.usernameFollowing + '/following').success(function(res){
  154. console.log(res);
  155. var modalInstance = $uibModal.open({
  156. templateUrl: '/following.html',
  157. controller: 'followingCtrl',
  158. resolve:{
  159. following: function(){
  160. return res.users;
  161. }
  162. }
  163. })
  164. })
  165. }
  166. })
  167.  
  168. app.controller("loginCtrl",function($scope,$location,$http,$uibModal){
  169.  
  170. $scope.login = function(){
  171. var data = {
  172. username : $scope.login.username,
  173. password : $scope.login.password
  174. }
  175.  
  176. $http.post('/login', data).success(function(res){
  177. if(res.status ==="OK"){
  178. window.location.href = "/";
  179. }
  180. else{
  181. console.log(res)
  182. $scope.loginInfo = "Fail to login";
  183. }
  184. })
  185. }
  186.  
  187. $scope.signUpButton = function(){
  188. var modalInstance = $uibModal.open({
  189. templateUrl: '/adduser',
  190. controller: 'adduserCtrl',
  191.  
  192. });
  193.  
  194.  
  195. }
  196. })
  197.  
  198. app.controller("adduserCtrl",function($scope,$location,$http, $uibModalInstance){
  199. $scope.ok = function(){
  200. var data = {
  201. username : $scope.adduser.username,
  202. password : $scope.adduser.password,
  203. email : $scope.adduser.email
  204. }
  205. $http.post('/adduser',data).success(function(res){
  206. if(res.status ==="OK"){
  207. console.log(res)
  208. $scope.adduserInfo = "Please verify your account"
  209. }else{
  210. console.log(res)
  211.  
  212. $scope.adduserInfo = "Fail to signup"
  213. }
  214. })
  215. }
  216.  
  217. $scope.cancel = function(){
  218. $uibModalInstance.close();
  219. }
  220. })
  221.  
  222. app.controller("searchCtrl", function($scope,$location,$http,$uibModalInstance){
  223. $scope.options = ["true", "false"];
  224. $scope.ok = function(){
  225. var selection;
  226. if ($scope.selectedFollowing == "true"){
  227. console.log("TRUEEEE");
  228. selection = true;
  229. }
  230. else{
  231. selection = false;
  232. }
  233. var data = {
  234. timestamp : $scope.timestamp,
  235. limit: $scope.limit,
  236. q: $scope.q,
  237. username: $scope.username,
  238. following: selection
  239. }
  240. $http.post('/search',data).success(function(res){
  241. if(res.status == "OK"){
  242. console.log(res);
  243. $uibModalInstance.close(res);
  244. }
  245. else{
  246. console.log(res);
  247. }
  248. })
  249. }
  250.  
  251. $scope.cancel = function(){
  252. $uibModalInstance.close();
  253. }
  254. })
  255.  
  256. app.controller("followersCtrl",function($scope,$location,$http,$uibModalInstance,followers){
  257. $scope.follows = followers;
  258. $scope.ok = function(){
  259. $uibModalInstance.close();
  260. }
  261. $scope.cancel = function(){
  262. $uibModalInstance.close();
  263. }
  264. })
  265.  
  266. app.controller("followingCtrl",function($scope,$location,$http,$uibModalInstance,following){
  267. $scope.follows = following;
  268. $scope.ok = function(){
  269. $uibModalInstance.close();
  270. }
  271. $scope.cancel = function(){
  272. $uibModalInstance.close();
  273. }
  274. })
Add Comment
Please, Sign In to add comment