Няма описание

controllers.js 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. angular.module('starter.controllers', ["angular-humanize", 'mcot.filters'])
  2. .controller('AppCtrl', function($scope, $ionicModal, $timeout, $cordovaFacebook, mcotcms, $cordovaSQLite, $location, $ionicHistory, $ionicPopup, $state, $ionicLoading, $cordovaLocalNotification, $rootScope, $interval, $cordovaOauth, $cordovaInAppBrowser, $http) {
  3. $scope.data = {
  4. bcount: 0
  5. };
  6. $scope.igLogin = function(){
  7. $cordovaOauth.instagram("aede22fcf7a145779da9a3cd094069f5", ["basic"])
  8. .then(function(success){
  9. console.log(success);
  10. var token = success.access_token;
  11. $http.get("https://api.instagram.com/v1/users/self/?access_token="+token)
  12. .then(function(success){
  13. console.log(success);
  14. }, function(error){
  15. console.log(error);
  16. });
  17. },function(error){
  18. console.log(error);
  19. });
  20. };
  21. $scope.twitterlogin = function(){
  22. console.log("twitter login");
  23. var api_key = "VNA6F7wAAwn10KJ7vqhdUPVzR"; //Enter your Consumer Key (API Key)
  24. var api_secret = "bJDNQhpZPlScH45WuARaJtvv0fMTR68AVTI3VbsfImeht8tt3S"; // Enter your Consumer Secret (API Secret)
  25. console.log("twitterlogin function got called");
  26. /*
  27. var options = {
  28. location: 'yes',
  29. clearcache: 'yes',
  30. toolbar: 'no'
  31. };
  32. $cordovaInAppBrowser.open('http://ngcordova.com', '_blank')
  33. .then(function(event) {
  34. // success
  35. })
  36. .catch(function(event) {
  37. // error
  38. });*/
  39. /*
  40. $cordovaOauth.twitter(api_key, api_secret).then(function(result) {
  41. console.log(result);
  42. }, function(error){
  43. console.log(error);
  44. });*/
  45. $cordovaOauth.google("314112088577-mqifo59b09psg38fg5l3s27e8h2ihj2c.apps.googleusercontent.com", ["email"]).then(function(result) {
  46. console.log("Response Object -> " + JSON.stringify(result));
  47. }, function(error) {
  48. console.log("Error -> " + error);
  49. });
  50. };
  51. $rootScope.$on('$cordovaInAppBrowser:loaderror', function(e, event) {
  52. console.log("load error");
  53. console.log(e);
  54. console.log(event);
  55. });
  56. $scope.authenticate = function(provider) {
  57. $auth.authenticate(provider);
  58. };
  59. /*
  60. $interval(function() {
  61. //$scope.fetchNotification();
  62. $cordovaLocalNotification.schedule({
  63. id: Math.floor(Date.now() / 1000),
  64. title: 'test noti',
  65. text: 'description',
  66. }).then(function(result) {
  67. console.log(result);
  68. });
  69. console.log("noti");
  70. }, 5000);*/
  71. $scope.fetchNotification = function(){
  72. var token = mcotcms.get_token();
  73. if( token !== null ){
  74. mcotcms.fetch_noti(token, {type: 'post'})
  75. .then(function(success){
  76. console.log("fetch_noti");
  77. $scope.data.bcount = success.data.output.length;
  78. for(var i = 0; i < success.data.output.length; i++) {
  79. post = success.data.output[i];
  80. console.log("the post");
  81. console.log(post);
  82. $cordovaLocalNotification.schedule({
  83. id: post._id.$oid,
  84. title: post.title,
  85. text: post.description,
  86. data: {
  87. oid: success.data.output[i]._id.$oid
  88. }
  89. }).then(function(result) {
  90. console.log(result);
  91. });
  92. }
  93. }, function(error){
  94. });
  95. }
  96. };
  97. $scope.scheduleSingleNotification = function() {
  98. var now = new Date().getTime();
  99. var _10SecondsFromNow = new Date(now + 10 * 1000);
  100. console.log("run notification");
  101. /*
  102. $cordovaLocalNotification.schedule({
  103. id: 1,
  104. title: 'Title here',
  105. text: 'Text here',
  106. at: _10SecondsFromNow
  107. }).then(function(result) {
  108. console.log(result);
  109. });*/
  110. };
  111. $rootScope.$on('$cordovaLocalNotification:trigger',
  112. function(event, notification, state) {
  113. console.log("trigger");
  114. console.log(event);
  115. console.log(notification);
  116. console.log(state);
  117. });
  118. $rootScope.$on('$cordovaLocalNotification:update',
  119. function(event, notification, state) {
  120. console.log("update");
  121. console.log(event);
  122. console.log(notification);
  123. console.log(state);
  124. });
  125. $rootScope.$on('$cordovaLocalNotification:click',
  126. function(event, notification, state) {
  127. console.log("click");
  128. console.log(event);
  129. console.log(notification);
  130. console.log(state);
  131. var d = angular.fromJson(notification.data);
  132. console.log(d);
  133. $state.go("app.view", { id: d.oid});
  134. });
  135. $scope.loadData = function() {
  136. //console.log(db);
  137. console.log(window.localStorage);
  138. console.log("load call");
  139. mcotcms.load_personal_data("token")
  140. .then(
  141. function(res) {
  142. if (res.rows.length > 0) {
  143. $scope.newMessage = res.rows.item(0);
  144. $scope.statusMessage = "Message loaded successful, cheers!";
  145. }
  146. },
  147. function(error) {
  148. $scope.statusMessage = "Error on loading: " + error.message;
  149. }
  150. );
  151. };
  152. // With the new view caching in Ionic, Controllers are only called
  153. // when they are recreated or on app start, instead of every page change.
  154. // To listen for when this page is active (for example, to refresh data),
  155. // listen for the $ionicView.enter event:
  156. //$scope.$on('$ionicView.enter', function(e) {
  157. //});
  158. // Form data for the login modal
  159. $scope.loginData = {};
  160. $scope.signupData = {};
  161. $scope.showAlert = function(title, text) {
  162. var alertPopup = $ionicPopup.alert({
  163. title: title,
  164. template: text
  165. });
  166. alertPopup.then(function(res) {
  167. console.log('Thank you for not eating my delicious ice cream cone');
  168. });
  169. };
  170. $scope.logout = function() {
  171. if (angular.isDefined($scope.modal))
  172. $scope.modal.hide();
  173. if (angular.isDefined($scope.signup_modal))
  174. $scope.signup_modal.hide();
  175. mcotcms.clear_data();
  176. $ionicHistory.nextViewOptions({
  177. disableBack: true
  178. });
  179. $state.go('app.index');
  180. $cordovaFacebook.logout()
  181. .then(function(success) {
  182. // success
  183. console.log("logout");
  184. console.log(success);
  185. }, function(error) {
  186. // error
  187. });
  188. };
  189. $scope.doSignUp = function() {
  190. if ($scope.signupData.password == $scope.signupData.confirm_password) {
  191. $scope.signupData.message = "Success";
  192. mcotcms.add_user_by_form({
  193. email: $scope.signupData.email,
  194. password: $scope.signupData.password
  195. })
  196. .then(function(success) {
  197. console.log("add user ");
  198. //mcotcms.execute();
  199. //mcotcms.select();
  200. console.log(success);
  201. $scope.signup_modal.hide();
  202. $scope.$emit('requireLogin', {})
  203. /*
  204. $cordovaSQLite.execute(db, 'INSERT INTO Messages (message) VALUES (?)', ["tum"])
  205. .then(function(result) {
  206. $scope.statusMessage = "Message saved successful, cheers!";
  207. }, function(error) {
  208. $scope.statusMessage = "Error on saving: " + error.message;
  209. })*/
  210. }, function(error) {
  211. console.log("add erro user ");
  212. console.log(error);
  213. $scope.$emit("signupError", {
  214. msg: error.data.error
  215. })
  216. //console.log(error);
  217. //$scope.statusMessage = err.data.error;
  218. //console.log(err);
  219. });
  220. } else {
  221. $scope.signupData.message = "Not Matched";
  222. }
  223. };
  224. $scope.getStatus = function() {
  225. $cordovaFacebook.getLoginStatus()
  226. .then(function(success) {
  227. console.log("get status");
  228. console.log(success);
  229. /*
  230. { authResponse: {
  231. userID: "12345678912345",
  232. accessToken: "kgkh3g42kh4g23kh4g2kh34g2kg4k2h4gkh3g4k2h4gk23h4gk2h34gk234gk2h34AndSoOn",
  233. session_Key: true,
  234. expiresIn: "5183738",
  235. sig: "..."
  236. },
  237. status: "connected"
  238. }
  239. */
  240. }, function(error) {
  241. // error
  242. });
  243. };
  244. $scope.fblogin = function() {
  245. $cordovaFacebook.login(["public_profile", "email", "user_friends"])
  246. .then(function(login_obj) {
  247. console.log("fb login");
  248. console.log(login_obj);
  249. $cordovaFacebook.api("me", ["public_profile"])
  250. .then(function(me_obj) {
  251. console.log("me");
  252. //console.log(me_obj);
  253. mcotcms.add_user({
  254. 'login': login_obj,
  255. 'me': me_obj
  256. })
  257. .then(function(result) {
  258. console.log(" success add_user ");
  259. console.log(result.data.token);
  260. mcotcms.store_token(result.data.token)
  261. .then(function(result) {
  262. $scope.$emit('signinSuccess', {});
  263. }, function(error) {
  264. console.log(error);
  265. });
  266. }, function(err) {
  267. console.log(err);
  268. });
  269. }, function(error) {
  270. console.log("error fb");
  271. });
  272. // { id: "634565435",
  273. // lastName: "bob"
  274. // ...
  275. // }
  276. }, function(error) {
  277. console.log("error fb");
  278. });
  279. };
  280. // Create the login modal that we will use later
  281. $ionicModal.fromTemplateUrl('templates/login.html', {
  282. scope: $scope
  283. }).then(function(modal) {
  284. $scope.modal = modal;
  285. });
  286. $scope.openLoginDialog = function() {
  287. console.log("modal");
  288. console.log($scope.modal);
  289. };
  290. // Create the login modal that we will use later
  291. $scope.$on('requireLogin', function(event, args) {
  292. if( angular.isDefined($scope.modal)) {
  293. $scope.modal.remove();
  294. }
  295. $ionicModal.fromTemplateUrl('templates/login.html', {
  296. scope: $scope
  297. }).then(function(modal) {
  298. $scope.modal = modal;
  299. $scope.modal.show();
  300. });
  301. //$scope.openLoginDialog();
  302. });
  303. $scope.$on('logoutEvent', function(event, args) {
  304. $scope.logout();
  305. });
  306. $scope.$on('signinError', function(event, args) {
  307. console.log("Sign In Error");
  308. $scope.showAlert("Sign In Error", args.msg);
  309. });
  310. $scope.$on('showLoading', function(event, args) {
  311. console.log("show loading");
  312. $ionicLoading.show({
  313. template: args.text
  314. }).then(function() {
  315. console.log("The loading indicator is now displayed");
  316. });
  317. });
  318. $scope.$on('hideLoading', function(event, args) {
  319. $ionicLoading.hide().then(function() {
  320. console.log("The loading indicator is now hidden");
  321. });
  322. });
  323. $scope.$on('signupError', function(event, args) {
  324. console.log("Sign Up Error");
  325. $scope.showAlert("Sign Up Error", args.msg);
  326. });
  327. $scope.$on('signinSuccess', function(event, args) {
  328. console.log("Sign in success");
  329. $state.go("app.index");
  330. $scope.closeLogin();
  331. $scope.closeSignUp();
  332. });
  333. $ionicModal.fromTemplateUrl('templates/signup.html', {
  334. scope: $scope
  335. }).then(function(su_modal) {
  336. $scope.signup_modal = su_modal;
  337. });
  338. // Triggered in the login modal to close it
  339. $scope.closeLogin = function() {
  340. console.log("close sigin");
  341. if(angular.isDefined($scope.modal))
  342. $scope.modal.hide();
  343. var token = mcotcms.get_token();
  344. console.log(token);
  345. if (token == 'null') {
  346. console.log('null ?');
  347. $ionicHistory.nextViewOptions({
  348. disableBack: true
  349. });
  350. $state.go('app.index');
  351. }
  352. };
  353. $scope.closeSignUp = function() {
  354. if( angular.isDefined($scope.signup_modal))
  355. $scope.signup_modal.hide();
  356. var token = mcotcms.get_token();
  357. if (token == null) {
  358. $ionicHistory.nextViewOptions({
  359. disableBack: true
  360. });
  361. $state.go('app.index');
  362. }
  363. };
  364. // Open the login modal
  365. $scope.login = function() {
  366. $scope.modal.show();
  367. };
  368. // Perform the login action when the user submits the login form
  369. $scope.doLogin = function() {
  370. console.log('Doing login', $scope.loginData);
  371. // Simulate a login delay. Remove this and replace with your login
  372. // code if using a login system
  373. mcotcms.login($scope.loginData.username, $scope.loginData.password)
  374. .then(function(success) {
  375. console.log(success);
  376. mcotcms.store_token(success.data.access_token)
  377. .then(function(result) {
  378. console.log(result);
  379. }, function(error) {
  380. console.log(error);
  381. });
  382. }, function(error) {
  383. $scope.$emit("signinError", {
  384. msg: error.data.description
  385. })
  386. console.log(error);
  387. });
  388. $timeout(function() {
  389. $scope.closeLogin();
  390. }, 1000);
  391. };
  392. })
  393. .controller('RequireLoginCtrl', function($scope, mcotcms, $location) {
  394. console.log("check login ...");
  395. var token = mcotcms.get_token();
  396. if (token == 'null' || token == null) {
  397. console.log("token null");
  398. mcotcms.store_token(null);
  399. $scope.$emit('requireLogin', {});
  400. }
  401. })
  402. .controller('UtilCtrl', function($scope, mcotcms, $location) {
  403. $scope.toDate = function(mongoDate) {
  404. console.log(mongoDate)
  405. if( angular.isDefined(mongoDate) ) {
  406. return new Date(mongoDate.$date);
  407. }else {
  408. return new Date();
  409. }
  410. };
  411. $scope.go = function ( path ) {
  412. $location.path( path );
  413. };
  414. $scope.fav = function(post_id, type) {
  415. var token = mcotcms.get_token();
  416. mcotcms.add_fav(token, post_id, type)
  417. .then(function(success) {
  418. console.log(success);
  419. }, function(error) {
  420. console.log(error);
  421. });
  422. };
  423. })
  424. .controller('LoadMoreCtrl', function($scope, mcotcms, PAGE_LIMIT) {
  425. $scope.current_page = 0;
  426. $scope.is_empty = true;
  427. $scope.moreDataCanBeLoaded = function() {
  428. console.log("more data canbe loaded");
  429. return $scope.is_empty == false;
  430. };
  431. $scope.firstLoad = function() {
  432. console.log("load 1");
  433. $scope.is_empty = true;
  434. if (angular.isUndefined($scope.results)) {
  435. $scope.results = [];
  436. }
  437. if (angular.isDefined($scope.special_action)) {
  438. console.log("wait im");
  439. if ($scope.special_action == "get_fav") {
  440. var the_query = mcotcms.get_fav($scope.token, {
  441. 'page': $scope.current_page++,
  442. 'type': 'post'
  443. });
  444. } else if ($scope.special_action == "get_nearby") {
  445. console.log("get nearby");
  446. var the_query = mcotcms.nearby($scope.position, {
  447. 'page': $scope.current_page++,
  448. 'type': 'post'
  449. });
  450. }
  451. } else {
  452. var the_query = mcotcms.get_posts({
  453. 'query': $scope.query,
  454. 'page': $scope.current_page++,
  455. 'version': $scope.post_version
  456. });
  457. }
  458. the_query.then(function successCallback(response) {
  459. // this callback will be called asynchronously
  460. // when the response is available
  461. console.log("first load");
  462. console.log(response.data)
  463. if (response.data.output.length == 0) {
  464. $scope.is_empty = true;
  465. return;
  466. }
  467. var output = [];
  468. // for(var i = 0; i < 10; i++ ){
  469. output = output.concat(response.data.output);
  470. //}
  471. console.log("update results");
  472. $scope.results = $scope.results.concat(output);
  473. console.log($scope.results);
  474. $scope.is_empty = false;
  475. //$scope.$broadcast('scroll.infiniteScrollComplete');
  476. //$scope.results = response.data.output;
  477. }, function errorCallback(response) {
  478. // called asynchronously if an error occurs
  479. // or server returns response with an error status.
  480. });
  481. };
  482. $scope.firstLoad();
  483. $scope.loadMoreData = function() {
  484. console.log("load more 2");
  485. if ($scope.is_empty == true) {
  486. return;
  487. }
  488. if (angular.isUndefined($scope.results)) {
  489. console.log("recreate ");
  490. $scope.results = [];
  491. }
  492. if (angular.isDefined($scope.special_action)) {
  493. console.log("wait im");
  494. if ($scope.special_action == "get_fav") {
  495. var the_query = mcotcms.get_fav($scope.token, {
  496. 'page': $scope.current_page++,
  497. 'type': 'post'
  498. });
  499. } else if ($scope.special_action == "get_nearby") {
  500. var the_query = mcotcms.nearby($scope.position, {
  501. 'page': $scope.current_page++,
  502. 'type': 'post'
  503. });
  504. }
  505. } else {
  506. var the_query = mcotcms.get_posts({
  507. 'query': $scope.query,
  508. 'page': $scope.current_page++,
  509. 'version': $scope.post_version
  510. })
  511. }
  512. the_query.then(function successCallback(response) {
  513. // this callback will be called asynchronously
  514. // when the response is available
  515. console.log("... =>");
  516. console.log(response.data.output.length);
  517. if (response.data.output.length == 0) {
  518. $scope.is_empty = true;
  519. return;
  520. }
  521. console.log("not empty ");
  522. var output = [];
  523. /*
  524. for(var i = 0; i < 10; i++ ){*/
  525. output = output.concat(response.data.output);
  526. /*}*/
  527. console.log("update results");
  528. $scope.results = $scope.results.concat(output);
  529. $scope.is_empty = false;
  530. $scope.$broadcast('scroll.infiniteScrollComplete');
  531. //$scope.results = response.data.output;
  532. }, function errorCallback(response) {
  533. // called asynchronously if an error occurs
  534. // or server returns response with an error status.
  535. });
  536. };
  537. $scope.$on('$stateChangeSuccess', function() {
  538. $scope.loadMoreData();
  539. });
  540. })
  541. .controller('FavCtrl', function($scope, mcotcms, $controller) {
  542. $scope.query = {};
  543. $scope.post_version = "short";
  544. $scope.special_action = "get_fav";
  545. $controller('RequireLoginCtrl', {
  546. $scope: $scope
  547. });
  548. $scope.token = mcotcms.get_token();
  549. if ($scope.token != 'null') {
  550. $controller('LoadMoreCtrl', {
  551. $scope: $scope
  552. });
  553. }
  554. })
  555. .controller('ByAuthorCtrl', function($scope, $stateParams, $controller, mcotcms) {
  556. console.log($stateParams);
  557. $scope.catname = $stateParams.uid;
  558. $scope.uid = $stateParams.uid;
  559. $scope.query = { 'author': $stateParams.uid };
  560. $scope.post_version = "short";
  561. mcotcms.by_author($scope.uid)
  562. .then(function(success){
  563. console.log("author");
  564. console.log(success);
  565. $scope.by_author = success.data.output;
  566. },function(error){
  567. });
  568. $controller('LoadMoreCtrl', {
  569. $scope: $scope
  570. });
  571. })
  572. .controller('NearbyCtrl', function($scope, mcotcms, $controller, $cordovaGeolocation, $ionicLoading) {
  573. $scope.query = {};
  574. var posOptions = {
  575. timeout: 10000,
  576. enableHighAccuracy: false
  577. };
  578. $scope.$emit("showLoading", {
  579. text: "Getting Current Position"
  580. });
  581. $cordovaGeolocation
  582. .getCurrentPosition(posOptions)
  583. .then(function(position) {
  584. var lat = position.coords.latitude
  585. var long = position.coords.longitude
  586. $scope.position = position.coords;
  587. $scope.post_version = "short";
  588. $scope.special_action = "get_nearby";
  589. console.log(position.coords);
  590. console.log("get nearby call");
  591. $scope.$emit("hideLoading", {});
  592. $controller('LoadMoreCtrl', {
  593. $scope: $scope
  594. });
  595. }, function(err) {
  596. // error
  597. });
  598. })
  599. .controller('PlaylistsCtrl', function($scope) {
  600. $scope.playlists = [{
  601. title: 'Reggae',
  602. id: 1
  603. }, {
  604. title: 'Chill',
  605. id: 2
  606. }, {
  607. title: 'Dubstep',
  608. id: 3
  609. }, {
  610. title: 'Indie',
  611. id: 4
  612. }, {
  613. title: 'Rap',
  614. id: 5
  615. }, {
  616. title: 'Cowbell',
  617. id: 6
  618. }];
  619. $scope.settingsList = [{
  620. text: "Wireless",
  621. checked: true
  622. }, {
  623. text: "GPS",
  624. checked: false
  625. }, {
  626. text: "Bluetooth",
  627. checked: false
  628. }];
  629. })
  630. .controller('SettingCtrl', function($scope, mcotcms, $controller) {
  631. $controller('RequireLoginCtrl', {
  632. $scope: $scope
  633. });
  634. var token = mcotcms.get_token();
  635. $scope.token = token;
  636. $scope.openUserLink = function() {
  637. window.open('http://mcot-simplitic.simplico.net/user_panel/'+$scope.token, '_system');
  638. };
  639. console.log("token ", token);
  640. $scope.categories = [{
  641. title: "nearme",
  642. value: true
  643. }, {
  644. title: "Breaking News",
  645. value: true
  646. }, {
  647. title: "ข่าวพาดหัว",
  648. value: true
  649. }, {
  650. title: "health",
  651. value: true
  652. }, {
  653. title: "fashion",
  654. value: true
  655. }, {
  656. title: "perspective",
  657. value: true
  658. }, {
  659. title: "idea",
  660. value: true
  661. }, {
  662. title: "outlook",
  663. value: true
  664. }, {
  665. title: "phototalk",
  666. value: true
  667. }, ];
  668. $scope.$on('$destroy', function() {
  669. console.log("leaving from setting");
  670. console.log($scope.categories);
  671. mcotcms.save_settings(token, $scope.categories);
  672. });
  673. mcotcms.get_settings(token)
  674. .then(function(success) {
  675. var cat_serv = success.data.output;
  676. console.log(cat_serv);
  677. for (var i in $scope.categories) {
  678. c = $scope.categories[i];
  679. console.log(cat_serv[c.title]);
  680. if (cat_serv[c.title] != true) {
  681. c.value = false;
  682. }
  683. }
  684. }, function(error) {
  685. console.log("errror ..", error);
  686. mcotcms.store_token(null);
  687. //$scope.$emit("requireLogin", {});
  688. });
  689. $scope.logout = function() {
  690. console.log("logout ...");
  691. $scope.$emit('logoutEvent', {});
  692. };
  693. })
  694. .controller('IndexPageCtrl', function($scope, mcotcms, $controller) {
  695. $scope.query = { 'categories.text': 'TNA'};
  696. $scope.post_version = "short";
  697. $controller('UtilCtrl', {
  698. $scope: $scope
  699. });
  700. $controller('LoadMoreCtrl', {
  701. $scope: $scope
  702. });
  703. /*
  704. mcotcms.all_posts()
  705. .then(function successCallback(response) {
  706. // this callback will be called asynchronously
  707. // when the response is available
  708. var output = [];
  709. for(var i = 0; i < 10; i++ ){
  710. output = output.concat(response.data.output);
  711. }
  712. $scope.results = output ;
  713. //$scope.results = response.data.output;
  714. }, function errorCallback(response) {
  715. // called asynchronously if an error occurs
  716. // or server returns response with an error status.
  717. });*/
  718. })
  719. .controller('PlaylistCtrl', function($scope, $stateParams) {})
  720. .controller('LoginCtrl', function($scope, $stateParams, $cordovaFacebook) {
  721. $scope.login = function() {
  722. $cordovaFacebook.login(["public_profile", "email", "user_friends"])
  723. .then(function(success) {
  724. console.log(success);
  725. // { id: "634565435",
  726. // lastName: "bob"
  727. // ...
  728. // }
  729. }, function(error) {
  730. // error
  731. });
  732. };
  733. })
  734. .controller('ViewCtrl', function($scope, $stateParams, mcotcms, $cordovaDatePicker, $cordovaSocialSharing, $filter, $controller, $sce, PUBLIC_TOKEN) {
  735. //var deviceType = (navigator.userAgent.match(/iPad/i)) == "iPad" ? "iPad" : (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "iPhone" : (navigator.userAgent.match(/Android/i)) == "Android" ? "Android" : (navigator.userAgent.match(/BlackBerry/i)) == "BlackBerry" ? "BlackBerry" : "null";
  736. var isIOS = ionic.Platform.isIOS();
  737. var isAndroid = ionic.Platform.isAndroid();
  738. //console.log(deviceType);
  739. console.log("is IOS ", isIOS);
  740. console.log("is ANd ", isAndroid);
  741. var id = $stateParams.id;
  742. $controller('UtilCtrl', {
  743. $scope: $scope
  744. });
  745. $scope.openMap = function(loc) {
  746. window.open("https://www.google.com/maps?q="+loc[1]+","+loc[0],'_system');
  747. };
  748. mcotcms.get_share(id)
  749. .then(function(success){
  750. console.log(success);
  751. $scope.shareCounts = success.data.output;
  752. },function(error){
  753. });
  754. mcotcms.get_post_by_id(id)
  755. .then(function successCallback(response) {
  756. $scope.post = response.data.output[0];
  757. $scope.post_body = $sce.trustAsHtml($scope.post.body);
  758. mcotcms.by_author($scope.post['author'])
  759. .then(function(success){
  760. console.log("author");
  761. console.log(success);
  762. $scope.by_author = success.data.output;
  763. },function(error){
  764. });
  765. mcotcms.post_api(PUBLIC_TOKEN, "LIST", { query: {'categories.text': $scope.post.categories[$scope.post.categories.length - 1].text} })
  766. .then(function(success){
  767. $scope.more_ons = success.data.output;
  768. }, function(error){
  769. });
  770. }, function errorCallback(response) {});
  771. $scope.doSomething = function() {
  772. var options = {
  773. date: new Date(),
  774. mode: 'date', // or 'time'
  775. minDate: new Date() - 10000,
  776. allowOldDates: true,
  777. allowFutureDates: false,
  778. doneButtonLabel: 'DONE',
  779. doneButtonColor: '#F2F3F4',
  780. cancelButtonLabel: 'CANCEL',
  781. cancelButtonColor: '#000000'
  782. };
  783. $cordovaDatePicker.show(options).then(function(date) {
  784. alert(date);
  785. });
  786. };
  787. $scope.share = function() {
  788. var link_url = "http://www.tnamcot.com/view/" + $scope.post._id.$oid;
  789. $cordovaSocialSharing
  790. //.share($scope.post.title + " | " + $scope.post.description + ' ' + link_url, null, null, link_url) // Share via native share sheet
  791. .share(null, null, null, link_url) // Share via native share sheet
  792. .then(function(result) {
  793. // Success!
  794. console.log("share result ", result);
  795. if (result == true) {
  796. console.log("share true !!");
  797. mcotcms.update_share($scope.post._id.$oid)
  798. .then(function successCallBack(response) {
  799. console.log("share ok");
  800. }, function errorCallback(response) {
  801. console.log("fail");
  802. });
  803. }
  804. console.log("Share completed? ", result.completed); // On Android apps mostly return false even while it's true
  805. console.log("Shared to app: ", result.app); // On Android result.app is currently empty. On iOS it's empty when sharing is cancelled (result.completed=false)
  806. }, function(err) {
  807. // An error occured. Show a message to the user
  808. console.log("share error ", err);
  809. });
  810. };
  811. })
  812. .controller('ListPageCtrl', function($scope, $stateParams, $controller, mcotcms) {
  813. console.log($stateParams);
  814. $scope.catname = $stateParams.catname;
  815. $scope.query = {'categories.text': $stateParams.catname};
  816. $controller('LoadMoreCtrl', {
  817. $scope: $scope
  818. });
  819. })
  820. .controller('SearchCtrl', function($scope, $stateParams, $http, $ionicHistory, mcotcms, $controller) {
  821. console.log("search ctrl");
  822. $scope.search = "";
  823. $scope.search2 = "";
  824. $scope.query = {'categories.text': 'TNA'};
  825. $scope.post_version = "short";
  826. $controller('UtilCtrl', {
  827. $scope: $scope
  828. });
  829. $controller('LoadMoreCtrl', {
  830. $scope: $scope
  831. });
  832. $scope.change = function(v) {
  833. $scope.query = {'title': {'$regex': v} };
  834. $scope.results = [];
  835. $scope.current_page = 0;
  836. $scope.firstLoad();
  837. };
  838. });