Advertisement
Andricahyadi_

Controller.js

Sep 25th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.00 KB | None | 0 0
  1. var BASE_URL = 'http://madingbosku.com/schoolapi';
  2. var SCHOOL_ID = 8;
  3.  
  4. angular.module('starter.controllers', [])
  5.  
  6. // App Controller
  7. .controller('AppCtrl', function($scope,$rootScope,$ionicModal, $timeout,$http,$ionicActionSheet,$localStorage,$ionicHistory,$ionicPopup,$cordovaToast,$cordovaClipboard,$state,$ionicPlatform,$ionicSideMenuDelegate) {
  8.  
  9. $scope.username = $localStorage.username;
  10. $scope.loginData = {};
  11. $scope.reset_state = function(){
  12. $localStorage.page_state = '';
  13. $ionicHistory.goBack();
  14. };
  15.  
  16. $scope.doLogout = function(){
  17. $localStorage.token = undefined;
  18. var myPopup = $ionicPopup.show({
  19. title: 'MAGZ',
  20. subTitle: 'Sign Out is Successfully!',
  21. buttons: [{
  22. type: 'button button-outline button-energized',
  23. text: 'OK'
  24. }]
  25. });
  26.  
  27. $timeout(function() {
  28. myPopup.close(); //close the popup after 3 seconds for some reason
  29. window.location.href = "#/app/playlists";
  30. $rootScope.isLogin = false;
  31. }, 3000);
  32. }
  33.  
  34. $rootScope.fetch_comment = function(){
  35. $http.get(BASE_URL+'/comments/post/'+$rootScope.current_page)
  36. .success(function(res){
  37. $rootScope.current_comment = res;
  38. console.log(res);
  39. })
  40. var dataComment = Object.keys()
  41. .error(function(err){
  42. console.log(err);
  43. });
  44. }
  45.  
  46. if($localStorage.token != undefined){
  47. $rootScope.isLogin = true;
  48. } else {
  49. $rootScope.isLogin = false;
  50. }
  51.  
  52. $scope.main_menu = $localStorage.main_menu;
  53.  
  54. // Share Function
  55. function shareToFB(){
  56. $cordovaToast.show('paste the message to facebook post', 'long', 'bottom');
  57. $cordovaClipboard.copy('Kunjungin MadingBosKu di http://www.madingbosku.com');
  58. window.plugins.socialsharing.shareViaFacebook(
  59. 'Kunjungi MadingBosKu',
  60. null /* img */,
  61. 'http://www.madingbosku.com' /* url */,
  62. function() {
  63. console.log('share ok')
  64. },
  65. function(errormsg) {
  66. alert(errormsg)
  67. }
  68. );
  69. }
  70.  
  71. function shareToTwitter(){
  72. window.plugins.socialsharing.shareViaTwitter(
  73. 'Kunjungi MadingBosKu',
  74. null /* img */,
  75. 'http://www.madingbosku.com' /* url */,
  76. function() {console.log('share ok')},
  77. function(errormsg){alert(errormsg)}
  78. );
  79. }
  80.  
  81. function shareToWA(){
  82. window.plugins.socialsharing.shareViaWhatsApp(
  83. 'Kunjungi MadingBosKu',
  84. null /* img */,
  85. 'http://www.madingbosku.com' /* url */,
  86. function() {console.log('share ok')},
  87. function(errormsg){alert(errormsg)}
  88. );
  89. }
  90.  
  91. // Function Button in Playists
  92. $scope.showVersion = function() {
  93. $ionicPopup.show({
  94. title: 'MAGZ',
  95. subTitle: 'MAGZ is Version 1.6',
  96. buttons: [{
  97. text: 'OK',
  98. type: 'button-energized button-outline'
  99. }]
  100. })
  101. }
  102.  
  103. // Function Button in SinglePost
  104. $scope.inSinglePost = $state.is('app.singlepost')
  105. $scope.inPost = function() {
  106. if ($state.is('app.singlepost')) {
  107. return true
  108. } else {
  109. return false
  110. }
  111. }
  112.  
  113. $scope.showMenu = function() {
  114. if($localStorage.page_state == 'post') {
  115. var hidesheet = $ionicActionSheet.show({
  116. titleText : 'Social Share',
  117. buttons: [
  118. { text: ' Facebook' },
  119. { text: ' Twitter' },
  120. { text: ' WhatsApp' }
  121. ],
  122. cancelText: 'Cancel',
  123. cancel: function() {
  124. return true
  125. },
  126. buttonClicked: function(index) {
  127. if(index == 0){
  128. shareToFB();
  129. } else if(index == 1){
  130. shareToTwitter();
  131. } else if (index == 2) {
  132. shareToWA();
  133. }
  134. }
  135. });
  136. }
  137. }
  138.  
  139. // This Part a Dropdown Menu List
  140. var menus = $http.get(BASE_URL+'/category/'+SCHOOL_ID)
  141. .success(function(response){
  142. $scope.menus = response.data;
  143. })
  144.  
  145. $scope.mySchool = []
  146. for (var i=0; i < menus.length; i++) {
  147. $scope.mySchool[i] = {
  148. name: i,
  149. items: []
  150. };
  151. // for (var j=0; j < 3; j++) {
  152. // $scope.groups[i].items.push(i + '-' + j);
  153. // }
  154. }
  155.  
  156. // This Part a ToggleGroup MySchool Dropdown
  157. $scope.toggleGroup = function(mySchool) {
  158. if ($scope.isGroupShown(mySchool)) {
  159. $scope.shownGroup = null;
  160. } else {
  161. $scope.shownGroup = mySchool;
  162. }
  163. };
  164.  
  165. $scope.isGroupShown = function(mySchool) {
  166. return $scope.shownGroup === mySchool;
  167. };
  168.  
  169. $scope.shownGroup = $scope.mySchool[0];
  170.  
  171. // --------------------------------------- // --------------------------------------- //
  172.  
  173. $scope.entertainment = []
  174. for (var i=0; i < menus.length; i++) {
  175. $scope.entertainment[i] = {
  176. name: i,
  177. items: []
  178. };
  179. // for (var j=0; j < 3; j++) {
  180. // $scope.groups[i].items.push(i + '-' + j);
  181. // }
  182. }
  183.  
  184. // This Part a ToggleGroup Entertainment Dropdown
  185. $scope.toggleGroup = function(entertainment) {
  186. if ($scope.isGroupShown(entertainment)) {
  187. $scope.shownGroup = null;
  188. } else {
  189. $scope.shownGroup = entertainment;
  190. }
  191. };
  192.  
  193. $scope.isGroupShown = function(entertainment) {
  194. return $scope.shownGroup === entertainment;
  195. };
  196.  
  197. $scope.shownGroup = $scope.entertainment[0];
  198.  
  199. // Create the login modal that we will use later
  200. $ionicModal.fromTemplateUrl('templates/login.html', {
  201. scope: $scope
  202. }).then(function(modal) {
  203. $scope.modal = modal;
  204. });
  205.  
  206. // Triggered in the login modal to close it
  207. $scope.closeLogin = function() {
  208. $scope.modal.hide();
  209. };
  210.  
  211. // Open the login modal
  212. $scope.login = function() {
  213. $scope.modal.show();
  214. };
  215.  
  216. // Perform the login action when the user submits the login form
  217. $scope.doLogin = function() {
  218. console.log('Doing login', $scope.loginData);
  219.  
  220. // Simulate a login delay. Remove this and replace with your login
  221. // code if using a login system
  222. $timeout(function() {
  223. $scope.closeLogin();
  224. }, 900);
  225. };
  226.  
  227. $http.get(BASE_URL+'/category/'+SCHOOL_ID)
  228. .success(function(response){
  229. $scope.menus = response.data;
  230. })
  231. .error(function(err){
  232. console.log(err);
  233. }).then(function(){
  234. // Add code for continue
  235. });
  236.  
  237. // Sidemenu List
  238. $scope.toLogin = function() {
  239. window.location.href = "#/login"
  240. }
  241.  
  242. $scope.toHome = function() {
  243. $state.go('app.playlists')
  244. }
  245.  
  246. $scope.toIntroduction = function() {
  247. window.location.href = "#/app/splash"
  248. }
  249.  
  250. $scope.toAbout = function() {
  251. window.location.href = "#/app/about"
  252. }
  253.  
  254. $scope.toSpecialThanks = function() {
  255. window.location.href = "#/app/specialThanks"
  256. }
  257.  
  258. $scope.exitAppMobile = function() {
  259. var confirmPopup = $ionicPopup.show({
  260. title: 'MAGZ',
  261. subTitle: 'Are you sure you want to Exit ?',
  262. buttons: [{
  263. text: 'Cancel',
  264. type: 'button-energized button-outline'
  265. },{
  266. text: 'Ok',
  267. type: 'button-energized',
  268. onTap: function() {
  269. navigator.app.exitApp()
  270. }
  271. }
  272. ]
  273. })
  274. }
  275. })
  276.  
  277. .controller('SplashCtrl',function($scope,$http, $ionicActionSheet, $state, $localStorage, $ionicSlideBoxDelegate){
  278. $scope.previousPage = function() {
  279. $ionicSlideBoxDelegate.previous();
  280. };
  281. $scope.nextPage = function() {
  282. $ionicSlideBoxDelegate.next();
  283. };
  284. $scope.slideChanged = function(index) {
  285. $scope.slideIndex = index;
  286. };
  287. var toHomepage = function() {
  288. window.location.href = "#/app/playlists";
  289. window.localStorage['MadingSMKN2Balikpapan'] = true;
  290. }
  291. $scope.toHomepage = function() {
  292. toHomepage();
  293. var dataTest = document.getElementById('imageLast');
  294. }
  295.  
  296. if (window.localStorage['MadingSMKN2Balikpapan'] === "true") {
  297. toHomepage();
  298. } else {
  299. setTimeout(function () {
  300. navigator.splashscreen.hide()
  301. window.location.href = "#/app/playlists";
  302. });
  303. }
  304. })
  305.  
  306.  
  307. // Login Controller
  308. .controller('LoginCtrl',function($scope,$rootScope,$http,$localStorage,$ionicPopup,$timeout,$ionicPlatform,$ionicHistory,$location){
  309. $scope.data = {};
  310. $scope.attempt_login = function(){
  311. $http.post(BASE_URL+'/login',{ phone: $scope.data.phone , password: $scope.data.password}).then(function(response){
  312. if(response.status == 200){
  313. var loginSuccces = $ionicPopup.show({
  314. title: 'MAGZ',
  315. subTitle: 'Sign In is Successfully!',
  316. buttons: [{
  317. type: 'button-outline button-energized',
  318. text: 'OK'
  319. }]
  320. })
  321.  
  322. console.log(response.data);
  323. $localStorage.token = response.data.token;
  324. $localStorage.username = response.data.user.name;
  325. $localStorage.user_id = response.data.user.id;
  326. window.location.href="#/app/playlists";
  327. $rootScope.isLogin = true;
  328. } else {
  329. var myPopup = $ionicPopup.show({
  330. title: 'MAGZ',
  331. subTitle: 'Please check your username or password.',
  332. buttons: [{
  333. type: 'button-outline button-energized',
  334. text: 'OK'
  335. }]
  336. });
  337.  
  338. $timeout(function() {
  339. myPopup.close(); //close the popup after 3 seconds for some reason
  340. }, 3000);
  341. }
  342. },function(err){
  343. var myPopup = $ionicPopup.show({
  344. title: 'MAGZ',
  345. subTitle: 'Please check your username and password.',
  346. buttons: [{
  347. type: 'button-outline button-energized',
  348. text: 'OK'
  349. }]
  350. });
  351.  
  352. $timeout(function() {
  353. myPopup.close(); //close the popup after 3 seconds for some reason
  354. }, 2000);
  355. });
  356. }
  357.  
  358. })
  359.  
  360.  
  361. // Register Controller
  362. .controller('RegisterCtrl',function($scope,$http,$timeout,$ionicPopup){
  363. $scope.school_list = [];
  364. $scope.data = {};
  365. $http.get(BASE_URL+'/schools')
  366. .success(function(res){
  367. $scope.school_list = res;
  368. })
  369. .error(function(res){
  370. console.log(res);
  371. });
  372.  
  373. $scope.registerUser = function() {
  374. $scope.data.school_id = $scope.data.school_id.id;
  375. $http.post(BASE_URL+"/register",$scope.data).then(function(res){
  376.  
  377. var myPopup = $ionicPopup.show({
  378. title: 'MAGZ',
  379. template: 'Register is Successfully',
  380. buttons: [{
  381. type: 'button-outline button-energized',
  382. text: 'OK'
  383. }]
  384. })
  385.  
  386. $timeout(function() {
  387. myPopup.close(); //close the popup after 3 seconds for some reason
  388. }, 3000);
  389. window.location.href = "#/login";
  390. },function(err){
  391. console.log(err);
  392. });
  393. }
  394. })
  395.  
  396. // Rubrik Controller
  397. .controller('RubrikCtrl',function($scope,$http,$ionicLoading,$rootScope,$localStorage,$stateParams,$state){
  398. $localStorage.page_state = 'main';
  399. $scope.posts= [];
  400. $ionicLoading.show({
  401.  
  402. }).then(function(){
  403. console.log("The loading indicator is now displayed");
  404. });
  405. $http.get(BASE_URL+'/category/'+$stateParams.id+'/posts')
  406. .success(function(response){
  407. console.log(response.data);
  408. $scope.posts = response.data;
  409. $ionicLoading.hide();
  410. })
  411. .error(function(err){
  412. var myPopup = $ionicPopup.alert({
  413. title: 'Oops!!',
  414. template: 'Failed Register'
  415. });
  416.  
  417. $timeout(function() {
  418. myPopup.close(); //close the popup after 3 seconds for some reason
  419. }, 3000);
  420. }).then(function(){
  421.  
  422. });
  423. })
  424.  
  425. // Playlists Controller
  426. .controller('PlaylistsCtrl', function($scope,$http,$ionicLoading,$rootScope,$localStorage,$ionicPopup,$ionicHistory,$ionicPlatform,$location) {
  427. $scope.logoHeaderBar = "<img src='img/M.png' width='120' height='68' class='logoHeaderBar'>";
  428. $localStorage.page_state = 'main';
  429. $localStorage.main_menu = 'Mading Bos Ku';
  430. // $ionicLoading.show({
  431.  
  432. // }).then(function(){
  433. // // Add code for Optional
  434. // });
  435. $scope.posts= [];
  436. $http.get(BASE_URL+'/mading/'+SCHOOL_ID+'/latest')
  437. .success(function(response){
  438. console.log(response.data);
  439. $scope.posts = response.data;
  440. $ionicLoading.hide();
  441. })
  442. .error(function(err){
  443. console.log(err);
  444. }).then(function(){
  445. // Add code for Continue
  446. });
  447.  
  448. $scope.items = [1,2,3];
  449. $scope.doRefresh = function() {
  450. $http.get(BASE_URL+'/mading/'+SCHOOL_ID+'/latest')
  451. .success(function(response) {
  452. $scope.posts = response.data;
  453. console.log(response.data)
  454. })
  455. .finally(function() {
  456. // Stop the ion-refresher from spinning
  457. $scope.$broadcast('scroll.refreshComplete');
  458. });
  459. };
  460.  
  461. })
  462.  
  463. // About Controller
  464. .controller('AboutCtrl', function($scope,$rootScope,$stateParams,$http,$ionicLoading,$sce,$ionicActionSheet,$rootScope,$localStorage) {
  465. $scope.trustedPost = "";
  466. $scope.reset_state = function(){
  467. $localStorage.page_state = '';
  468. $ionicHistory.goBack();
  469. };
  470. })
  471.  
  472. // Support Controller
  473. .controller('specialThanksCtrl', function($scope,$rootScope,$stateParams,$http,$ionicLoading,$sce,$ionicActionSheet,$rootScope,$localStorage) {
  474. $scope.trustedPost = "";
  475. $scope.reset_state = function(){
  476. $localStorage.page_state = '';
  477. $ionicHistory.goBack();
  478. };
  479. })
  480.  
  481. // Post Controller
  482. .controller('PostCtrl', function($scope,$rootScope,$stateParams,$http,$ionicLoading,$sce,$ionicActionSheet,$rootScope,$localStorage) {
  483. $scope.trustedPost = "";
  484. $localStorage.page_state = "post";
  485. $ionicLoading.show({
  486.  
  487. }).then(function(){
  488. console.log("The loading indicator is now displayed");
  489. });
  490. $scope.post = {};
  491. $http.get(BASE_URL+'/mading/single/'+$stateParams.postId)
  492. .success(function(res){
  493. $ionicLoading.hide();
  494. $scope.post = res;
  495. $scope.trustedPost = $sce.trustAsHtml(res.content);
  496. $rootScope.pagestate = 'view';
  497. $rootScope.current_page = $stateParams.postId;
  498. $rootScope.fetch_comment();
  499. })
  500. .error(function(err){
  501. console.log(err);
  502. });
  503.  
  504. if($localStorage.token == undefined){
  505. $scope.can_comment = false;
  506. } else {
  507. $scope.can_comment = true;
  508. }
  509.  
  510. $scope.logoHeaderBar = "<img src='img/1.png' width='60' height='60' class='logoHeaderBar'>";
  511.  
  512. // Comment Function
  513. $scope.data = {};
  514. $scope.add_comment = function(){
  515. $http.post(BASE_URL+'/comments/post/'+$stateParams.postId,{ content: $scope.data.content , user_id: $localStorage.user_id }).then(function(response){
  516. if(response.status == 200){
  517. console.log(response.data);
  518. $rootScope.fetch_comment();
  519. } else {
  520. var myPopup = $ionicPopup.alert({
  521. title: 'Oops!!',
  522. template: 'Failed Post'
  523. });
  524.  
  525. $timeout(function() {
  526. myPopup.close(); //close the popup after 3 seconds for some reason
  527. }, 2000);
  528. }
  529. },function(err){
  530. var myPopup = $ionicPopup.alert({
  531. title: 'Oops!!',
  532. template: 'Failed Post'
  533. });
  534.  
  535. $timeout(function() {
  536. myPopup.close(); //close the popup after 3 seconds for some reason
  537. }, 2000);
  538. });
  539. }
  540. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement