Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 70.92 KB | None | 0 0
  1. define(["platform-config", "underscore", "jquery", "backbone",
  2. "init",
  3. "common-services/utils",
  4. "common-services/ServicesLoader",
  5. "services/Connection",
  6. "utils",
  7. "views/SimpleLayout",
  8. "views/ReadingZoneMainView",
  9. "views/InterestSettingsView",
  10. "views/InterestsLinksSidebarView",
  11. "views/UserVerificationView",
  12. "views/CollectionsAdminView",
  13. "views/ErrorView",
  14. "views/SignupView",
  15. "views/ResetPasswordView",
  16. "views/DialogAuthView",
  17. "views/ArticleView",
  18. "views/CreateCollectionView",
  19. "views/AddArticleToCollectionsView",
  20. "backbone.layoutmanager",
  21. "jquery.hammer"
  22. ],
  23. function (platformConfig, _, $, Backbone, init, commonUtils, servicesLoader, Connection, utils, SimpleLayout,
  24. ReadingZoneMainView, InterestSettingsView, InterestsLinksSidebarView, UserVerificationView,
  25. CollectionsAdminView, ErrorView, SginupView, ResetPasswordView, DialogAuthView, ArticleView,
  26. CreateCollectionView, AddArticleToCollectionsView) {
  27. 'use strict';
  28.  
  29. var SERVICE = {
  30. UPDATES: 'updates',
  31. LOGIN: 'login',
  32. // BOOKMARKS: 'bookmarks',
  33. INTERESTS: 'interests',
  34. PERSONALIZED_ARTICLES: 'personalized-articles',
  35. KEEN: 'keen',
  36. SESSION_TRACKER: 'session-tracker',
  37. FACEBOOK: 'facebook',
  38. GOOGLE: 'google',
  39. ARTICLE: 'article',
  40. COLLECTIONS: 'collections',
  41. CHANNELS_MAP: 'channels-map',
  42. ME_CHANNELS_MAP: 'me-channels-map'
  43. };
  44.  
  45. var TABS = {
  46. ARTICLES: {
  47. id: '#page-content--article',
  48. index: 0,
  49. active: true
  50. },
  51. COLLECTIONS: {
  52. id: '#page-content--collections',
  53. index: 1,
  54. active: false
  55. },
  56. PROFILE: {
  57. id: '#page-content--profile',
  58. index: 2,
  59. active: false
  60. },
  61. VERIFICATION: {
  62. id: '#page-content--verification',
  63. index: 3,
  64. active: false
  65. },
  66. COLLECTIONS_ADMIN: {
  67. id: '#page-content--collections-admin',
  68. index: 4,
  69. active: false
  70. }
  71. };
  72.  
  73. function _showWaitingSpinner(show) {
  74. utils.showSpinner('.waiting-spinner', show);
  75. }
  76.  
  77. function fbReportSignupSuccess(provider) {
  78. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  79. loginSrv.getProfile()
  80. .then(function (profile) {
  81. var isLead = profile.userType === 'lead';
  82. Backbone.trigger('fb:event', 'SIGNUP', {
  83. STATE: 'completed',
  84. USER_CATEGORY: isLead ? 'lead' : 'user', //TODO: adapt field name when is ready
  85. CREATION_DATE: profile.timeCreated, //TODO: adapt field name when is ready
  86. CONVERSION_DATE: isLead ? (new Date()).toJSON() : profile.timeCreated, //TODO: adapt field name when is ready
  87. INTERESTS: profile.speciality,
  88. PARTNER_RELATION: '',
  89. AUTHENTICATION_PROVIDER: provider
  90. });
  91. });
  92. }
  93.  
  94. function fbReportSigninSuccess(provider) {
  95. Backbone.trigger('fb:event', 'SIGNIN', {
  96. STATE: 'completed',
  97. AUTHENTICATION_PROVIDER: provider
  98. });
  99. }
  100.  
  101. function fbReportSignupFail(provider, error) {
  102. Backbone.trigger('fb:event', 'SIGNUP', {
  103. STATE: 'failed',
  104. ERROR: error,
  105. AUTHENTICATION_PROVIDER: provider
  106. });
  107. }
  108.  
  109. var DEFAULT_COLOR = 'rgb(181, 80, 222)';
  110.  
  111. function _colorize(articles, channelsConfig) {
  112. articles.map(function (article) {
  113. var config = channelsConfig[article.channel];
  114. article.color = config ? config.color : DEFAULT_COLOR;
  115. article.channelName = config ? config.name : article.channel;
  116. });
  117. return articles;
  118. }
  119.  
  120. return _.extend({}, {
  121. // Application Constructor
  122. initialize: function (config) {
  123. this.config = config;
  124. this.bindEvents();
  125. window.Connection = Connection;
  126. },
  127.  
  128. _createRouter: function () {
  129. var app = this;
  130. var Router = Backbone.Router.extend({
  131. routes: {
  132. "": "home",
  133. "logout": "logout",
  134. "login/:email": "login",
  135. "interest/:interest": "interest",
  136. "profile/settings": "settings",
  137. "profile/remove-interest/:interest": "settingsRemoveInterest",
  138. "editor/published/": "editorViewPublished",
  139. "editor/published/:interest": "editorViewPublished",
  140. "editor/not-published/": "editorViewNotPublished",
  141. "editor/not-published/:interest": "editorViewNotPublished",
  142. "editor/skipped/": "editorViewSkipped",
  143. "editor/skipped/:interest": "editorViewSkipped",
  144. "reset-password/": "resetPassword",
  145. "article/:articleId": "showArticle"
  146. },
  147.  
  148. home: function () {
  149. if (!app._loaded) { // handle during page load
  150. Backbone.trigger('keen:event', {
  151. type: 'page',
  152. action: 'show',
  153. name: 'home'
  154. });
  155. }
  156. },
  157.  
  158. interest: function (interest) {
  159. $(".mdl-layout__tab:eq(0) span").click();
  160. Backbone.trigger("interest-selected", {
  161. interest: interest
  162. });
  163. app._showArticles(interest);
  164. $(".mdl-layout__content").animate({
  165. scrollTop: 0
  166. }, 500, "swing");
  167. this.hideDrawer();
  168. Backbone.trigger('keen:event', {
  169. type: 'page',
  170. action: 'show',
  171. name: 'interest-articles'
  172. });
  173. Backbone.trigger('keen:event', {
  174. type: 'channel',
  175. action: 'open',
  176. channelId: interest,
  177. channelType: 'interest',
  178. mimeType: 'mixed'
  179. });
  180. },
  181.  
  182. settings: function () {
  183. $(".mdl-layout__tab:eq(1) span").click();
  184. this.hideDrawer();
  185. Backbone.trigger('keen:event', {
  186. type: 'page',
  187. action: 'show',
  188. name: 'interest-settings'
  189. });
  190. },
  191.  
  192. settingsRemoveInterest: function (interest) {
  193. app._removeInterest(interest);
  194. },
  195.  
  196. hideDrawer: function () {
  197. if ($('.mdl-layout__drawer.is-visible').length > 0) {
  198. var layout = document.querySelector('.mdl-layout');
  199. layout.MaterialLayout.toggleDrawer();
  200. }
  201. },
  202.  
  203. editorViewPublished: function (interest) {
  204. app._renderEditorPublished(interest);
  205. },
  206.  
  207. editorViewNotPublished: function (interest) {
  208. app._renderEditorNotPublished(interest);
  209. },
  210.  
  211. editorViewSkipped: function (interest) {
  212. app._renderEditorSkipped(interest);
  213. },
  214.  
  215. logout: function () {
  216. app._logout();
  217. },
  218.  
  219. login: function (email) {
  220. app._login(email);
  221. },
  222.  
  223. resetPassword: function () {
  224. app._resetPassword();
  225. },
  226.  
  227. showArticle: function (articleId) {
  228. app._showArticle(articleId);
  229. }
  230. });
  231.  
  232. this._router = new Router();
  233. Backbone.history.start({pushState: true});
  234.  
  235. return commonUtils.resolvedPromise();
  236. },
  237.  
  238. _navigate: function (path) {
  239. this._router.navigate(path, {
  240. trigger: true
  241. });
  242. },
  243.  
  244. _back: function () {
  245. window.history.back();
  246. },
  247.  
  248. // Bind Event Listeners
  249. //
  250. // Bind any events that are required on startup. Common events are:
  251. // 'load', 'deviceready', 'offline', and 'online'.
  252. bindEvents: function () {
  253. document.addEventListener('deviceready', _.bind(this.onDeviceReady, this), false);
  254. // this.onDeviceReady();
  255. },
  256.  
  257. // deviceready Event Handler
  258. //
  259. // The scope of 'this' is the event. In order to call the 'receivedEvent'
  260. // function, we must explicitly call 'app.receivedEvent(...);'
  261. onDeviceReady: function () {
  262. _showWaitingSpinner(true);
  263. $('.config-title').text(platformConfig.title);
  264. this._loadServices(this.config.services)
  265. .then(_.bind(this._initAnalytics, this))
  266. .then(_.bind(this._initSessionTracking, this))
  267. .then(_.bind(this._initInterests, this))
  268. .then(_.bind(this._initChannels, this))
  269. .then(_.bind(this._autoLogin, this))
  270. .then(_.bind(this._renderLayoutTabArticle, this))
  271. .then(_.bind(this._renderTabArticle, this))
  272. .then(_.bind(this._displayProfileSettings, this))
  273. .then(_.bind(this._displayMyCollections, this))
  274. .then(_.bind(this._displayUserVerification, this))
  275. .then(_.bind(this._displayCollectionsAdmin, this))
  276. .then(_.bind(this._createRouter, this))
  277. .then(_.bind(this._loadMainListeners, this))
  278. .then(_.bind(this._publisherModeConfiguration, this))
  279. .then(_.bind(this._hideLoadingSpinner, this))
  280. .then(_.bind(this._updateSignOutButtonState, this))
  281. .then(_.bind(function () {
  282. var loginService = window.miServices.get(SERVICE.LOGIN);
  283.  
  284. this.dialogAuth = new DialogAuthView({});
  285. this.dialogAuth.setElement('#dialogs').render();
  286.  
  287. Backbone.on('hide-auth-buttons', _.bind(function () {
  288. $('.login-signin-buttons-container').addClass('hidden');
  289. $('.responsive-login-register-buttons').addClass('hidden');
  290.  
  291. if (!this.checkIsItDevice()) {
  292. $('.sign-out-button-container').removeClass('hidden');
  293. } else {
  294. $('.responsive-sign-out-button').removeClass('hidden');
  295. }
  296.  
  297. }, this));
  298.  
  299. Backbone.on('show-auth-buttons', _.bind(function () {
  300. if (!this.checkIsItDevice()) {
  301. $('.login-signin-buttons-container').removeClass('hidden');
  302. $('.sign-out-button-container').addClass('hidden');
  303. } else {
  304. $('.responsive-login-register-buttons').removeClass('hidden');
  305. $('.responsive-sign-out-button').addClass('hidden');
  306. }
  307. }, this));
  308.  
  309. Backbone.on('editor:show-skipped', _.bind(function () {
  310. this._navigate('editor/skipped/');
  311. }, this));
  312.  
  313. Backbone.on('editor:show-published', _.bind(function () {
  314. this._navigate('editor/published/');
  315. }, this));
  316.  
  317. Backbone.on('editor:show-unpublished', _.bind(function () {
  318. this._navigate('editor/not-published/');
  319. }, this));
  320.  
  321. Backbone.on('show-article-excerpt', _.bind(function (articleId) {
  322. this._navigate('article/' + articleId);
  323. }, this));
  324.  
  325. loginService.on(loginService.loginStateChangedEventName, _.bind(function () {
  326. loginService.getProfile()
  327. .then(_.bind(function (profile) {
  328. if (profile) {
  329. this._displayMyCollections();
  330. this._displayProfileSettings();
  331. this._displayUserVerification();
  332. this._displayCollectionsAdmin();
  333. } else {
  334. if ($("#profile-page-link").is(":visible") ||
  335. $("#verification-page-link").is(":visible") ||
  336. $("#collections-page-link").is(":visible") ||
  337. $("#collections-admin-page-link").is(":visible")) {
  338.  
  339. window.location.reload();
  340. }
  341. }
  342. }, this))
  343. .fail(function (err) {
  344. console.error(err);
  345. });
  346. }, this));
  347.  
  348. this._loaded = true;
  349. _showWaitingSpinner(false);
  350.  
  351. if (this._showLogin) {
  352. this.dialogAuth._login = true;
  353. this.dialogAuth._email = this._email;
  354. delete this._showLogin;
  355. delete this._email;
  356. }
  357.  
  358. $('.signin-button').click(_.bind(function () {
  359. this.dialogAuth.showHideModalDialog("login", true);
  360. }, this));
  361.  
  362. $('.signup-button').click(_.bind(function () {
  363. this.dialogAuth.showHideModalDialog("register", true);
  364. }, this));
  365.  
  366. $('.signout-button').click(_.bind(function () {
  367. this._navigate('logout');
  368. }, this));
  369.  
  370. $('.responsive-signin-button').click(_.bind(function () {
  371. this.dialogAuth.showHideModalDialog("login", true);
  372. }, this));
  373.  
  374. $('.responsive-signup-button').click(_.bind(function () {
  375. this.dialogAuth.showHideModalDialog("register", true);
  376. }, this));
  377.  
  378. $('.responsive-signout-button').click(_.bind(function () {
  379. window.location.replace("#logout/");
  380. window.location.href = "#logout/";
  381. }, this));
  382.  
  383. loginService.getProfile().then(_.bind(function (user) {
  384. if (!user) {
  385. Backbone.trigger("show-auth-buttons");
  386.  
  387. _.delay(_.bind(function () {
  388. loginService.getProfile().then(_.bind(function (user) {
  389. if (!user) {
  390. if (!this.dialogAuth.isDialogCurrentlyShowing()) {
  391.  
  392. this.dialogAuth.showHideModalDialog("login", true);
  393. }
  394. } else {
  395. Backbone.trigger("hide-auth-buttons");
  396. }
  397. }, this));
  398. }, this), 5000);
  399. } else {
  400. Backbone.trigger("hide-auth-buttons");
  401. }
  402. }, this));
  403.  
  404. return commonUtils.resolvedPromise();
  405. }, this))
  406. .fail(_.bind(function (error) {
  407. this._hideLoadingSpinner();
  408. _showWaitingSpinner(false);
  409. $('.mdl-spinner').css('display', 'none');
  410. var errorView = new ErrorView();
  411. $(TABS.ARTICLES.id).append(errorView.el);
  412. console.error(error);
  413. return errorView.render().promise();
  414. }, this));
  415.  
  416. /* jshint ignore:start */
  417. $('.close-chip').click(_.bind(function (event) {
  418. sessionStorage.setItem('disableChip', true);
  419. $('#openInApp').addClass('hidden');
  420. event.stopPropagation();
  421. }, this));
  422.  
  423. $('#openInApp').click(function () {
  424. window.location.replace(platformConfig.appLink);
  425. });
  426.  
  427. if (!sessionStorage.getItem('disableChip') && this.checkIsItDevice()) {
  428. $('#openInApp').removeClass('hidden');
  429. }
  430. },
  431.  
  432. _initAnalytics: function () {
  433. var keenSrv = window.miServices.get(SERVICE.KEEN);
  434. if (keenSrv) {
  435. Backbone.on('keen:event', function (data, sendExtendedInfo) {
  436. keenSrv.trackEvent(data, sendExtendedInfo);
  437. });
  438. console.info('Keen analytics is initialized');
  439. } else {
  440. console.info('Keen analytics is not configured');
  441. }
  442. var fbSrv = window.miServices.get(SERVICE.FACEBOOK);
  443.  
  444. function _logFBEvent(eventName, params) {
  445. // for testing
  446. // console.log('FB event: ' + eventName + ': ' + JSON.stringify(params));
  447. fbSrv.logEvent(eventName, params);
  448. }
  449.  
  450. if (fbSrv) {
  451. Backbone.on('fb:event', function (eventName, params) {
  452. if ((eventName === "SIGNIN" || eventName === "SIGNUP") && params && params.STATE === 'completed') {
  453. Backbone.trigger('hide-auth-buttons');
  454. Backbone.trigger('hide-auth-dialogs');
  455. }
  456.  
  457. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  458. loginSrv.getProfile()
  459. .then(function (profile) {
  460. if (profile) {
  461. params.USER_ID = profile.id;
  462. }
  463. _logFBEvent(eventName, params);
  464. })
  465. .fail(function () {
  466. _logFBEvent(eventName, params);
  467. });
  468. });
  469. console.info('FB analytics is initialized');
  470. } else {
  471. console.info('FB analytics is not configured');
  472. }
  473. return commonUtils.resolvedPromise();
  474. },
  475.  
  476. _initSessionTracking: function () {
  477. var sessionSrv = window.miServices.get(SERVICE.SESSION_TRACKER);
  478. if (sessionSrv) {
  479. if (sessionSrv.isNewSession()) {
  480. Backbone.trigger('keen:event', {
  481. type: 'session',
  482. action: 'start'
  483. }, true);
  484. }
  485. }
  486. },
  487.  
  488. _initInterests: function () {
  489. var interestsService = window.miServices.get(SERVICE.INTERESTS);
  490. return interestsService.update();
  491. },
  492.  
  493. _initChannels: function () {
  494. var channelsMapService = window.miServices.get(SERVICE.CHANNELS_MAP);
  495. return channelsMapService.update()
  496. .then(function () {
  497. var srv = window.miServices.get(SERVICE.ME_CHANNELS_MAP);
  498. return srv.update();
  499. });
  500. },
  501.  
  502. _loadServices: function (servicesConfig) {
  503. var dfd = $.Deferred();
  504. servicesLoader.loadServices(servicesConfig)
  505. .always(function (servicesRegistry) {
  506. window.miServices = servicesRegistry;
  507. dfd.resolve();
  508. });
  509. return dfd.promise();
  510. },
  511.  
  512. _loadMainListeners: function () {
  513. Backbone.on('interest-changed', _.bind(function () {
  514. this._showArticles();
  515. }, this));
  516.  
  517. Backbone.on('show-progress', _.bind(_showWaitingSpinner, window, true));
  518. Backbone.on('hide-progress', _.bind(_showWaitingSpinner, window, false));
  519. Backbone.on('signup', _.bind(this._showSignupDialog, this));
  520.  
  521. Backbone.on('social-login', _.bind(function (socialProviderName) {
  522. _showWaitingSpinner(true);
  523. Backbone.trigger('fb:event', 'SIGNIN', {
  524. STATE: 'started',
  525. AUTHENTICATION_PROVIDER: socialProviderName
  526. });
  527. this._onSocialLogin(socialProviderName)
  528. .then(_.bind(function (signedUp) {
  529. _showWaitingSpinner(false);
  530. var navigationPath = signedUp === true ? 'profile/settings' : ''; // new/existing user
  531. if (signedUp) {
  532. fbReportSignupSuccess(socialProviderName);
  533. } else {
  534. fbReportSigninSuccess(socialProviderName);
  535. }
  536. this._navigate(navigationPath);
  537. }, this))
  538. .fail(function (err) {
  539. _showWaitingSpinner(false);
  540. Backbone.trigger('social-login-failed', err);
  541. Backbone.trigger('fb:event', 'SIGNIN', {
  542. STATE: 'failed',
  543. AUTHENTICATION_PROVIDER: socialProviderName
  544. });
  545. });
  546. }, this));
  547.  
  548. Backbone.on('personalize', _.bind(function () {
  549. this._navigate("profile/settings");
  550. }, this));
  551.  
  552. Backbone.on('create-collection', _.bind(function () {
  553. var view = new CreateCollectionView();
  554. $('body').append(view.render().$el);
  555. view.on('cancel', _.bind(function () {
  556. view.remove();
  557. }, this));
  558. }, this));
  559.  
  560. Backbone.on('add-article-to-collections', _.bind(function (articleId) {
  561. var articleSrv = window.miServices.get(SERVICE.ARTICLE);
  562. var collectionSrv = window.miServices.get(SERVICE.COLLECTIONS);
  563. articleSrv.getArticle(articleId)
  564. .then(_.bind(function (article) {
  565. collectionSrv.getCollections(null, 1000, null, null, null)
  566. .then(_.bind(function (collections) {
  567. var view = new AddArticleToCollectionsView({
  568. article: article,
  569. collections: collections
  570. });
  571. $('body').append(view.render().$el);
  572. view.on('done', _.bind(function () {
  573. view.remove();
  574. }, this));
  575. }, this))
  576. .fail(function (err) {
  577. //TODO: error handling
  578. });
  579. }, this))
  580. .fail(function (err) {
  581. //TODO: error handling
  582. });
  583. }, this));
  584.  
  585. $('#personalize-nav').hammer().on('tap', _.bind(function () {
  586. this._navigate("profile/settings");
  587. }, this));
  588.  
  589. $('#articles-page-link').hammer().on('tap', _.bind(function () {
  590. this._navigate("/");
  591. Backbone.trigger('keen:event', {
  592. type: 'page',
  593. action: 'show',
  594. name: 'home'
  595. });
  596. }, this));
  597.  
  598. //HACK: TODO: move when fixed into router.settings();
  599. $('#profile-page-link').hammer().on('tap', function () {
  600. Backbone.trigger('keen:event', {
  601. type: 'page',
  602. action: 'show',
  603. name: 'interest-settings'
  604. });
  605. });
  606.  
  607. var loginService = window.miServices.get(SERVICE.LOGIN);
  608. loginService.on(loginService.loginStateChangedEventName,
  609. _.bind(function () {
  610. this._updateSignOutButtonState();
  611. var srv = window.miServices.get(SERVICE.PERSONALIZED_ARTICLES);
  612. this._showArticles()
  613. .then(_.bind(srv.isEditor, srv))
  614. .then(_.bind(this._publisherModeConfiguration, this))
  615. .then(function () {
  616. _showWaitingSpinner(false);
  617. });
  618. }, this));
  619. },
  620.  
  621. _autoLogin: function () {
  622. var srv = window.miServices.get(SERVICE.LOGIN);
  623. var dfd = $.Deferred();
  624. srv.isLoggedIn()
  625. .then(function (isLoggedIn) {
  626. if (isLoggedIn) {
  627. return commonUtils.resolvedPromise();
  628. }
  629. var accessToken = utils.getParam('t');
  630. if (_.isUndefined(accessToken)) {
  631. return commonUtils.resolvedPromise();
  632. }
  633. var refreshToken = utils.getParam('r');
  634. if (_.isUndefined(accessToken)) {
  635. return commonUtils.resolvedPromise();
  636. }
  637. return srv.setAccessToken({'access_token': accessToken, 'refresh_token': refreshToken});
  638. })
  639. .always(_.bind(dfd.resolve, dfd));
  640. return dfd.promise();
  641. },
  642.  
  643. _renderEditorPublished: function (interest) {
  644. var srv = window.miServices.get(SERVICE.PERSONALIZED_ARTICLES);
  645. this._updateArticlesList(srv, srv.ARTICLE_STATE.PUBLISHED, interest);
  646. },
  647.  
  648. _renderEditorNotPublished: function (interest) {
  649. var srv = window.miServices.get(SERVICE.PERSONALIZED_ARTICLES);
  650. this._updateArticlesList(srv, srv.ARTICLE_STATE.NOT_PUBLISHED, interest);
  651. },
  652.  
  653. _renderEditorSkipped: function (interest) {
  654. var srv = window.miServices.get(SERVICE.PERSONALIZED_ARTICLES);
  655. this._updateArticlesList(srv, srv.ARTICLE_STATE.SKIPPED, interest);
  656. },
  657.  
  658. _updateSignOutButtonState: function () {
  659. var dfd = $.Deferred();
  660. var srv = window.miServices.get(SERVICE.LOGIN);
  661. srv.isLoggedIn()
  662. .then(function (loggedIn) {
  663. if (loggedIn) {
  664. $('body').addClass('logged-in');
  665.  
  666. } else {
  667. $('body').removeClass('logged-in');
  668. }
  669. dfd.resolve();
  670. })
  671. .fail(_.bind(dfd.resolve, dfd)); // always success
  672. return dfd.promise();
  673. },
  674.  
  675. _updateArticlesList: function (srv, forArticlesState, interest) {
  676. if (forArticlesState === this._lastArticlesState) {
  677. this._showArticles(interest);
  678. } else {
  679. _showWaitingSpinner(true);
  680. this._updateEditorModeLinks(forArticlesState);
  681. this._showArticles(interest, forArticlesState)
  682. .then(_.bind(function () {
  683. this._lastArticlesState = forArticlesState;
  684. }, this))
  685. .then(function () {
  686. _showWaitingSpinner(false);
  687. })
  688. .fail(function (err) {
  689. _showWaitingSpinner(false);
  690. console.error(err);
  691. });
  692. }
  693. },
  694.  
  695. _updateEditorModeLinks: function (forArticlesState) {
  696. var $editorMode = $('.editor-view-mode');
  697. $editorMode.removeClass('editor-view-mode-published editor-view-mode-not-published editor-view-mode-skipped');
  698. $editorMode.addClass('editor-view-mode-' + forArticlesState);
  699. },
  700.  
  701. generateTemplate: function (template, data) {
  702. return template({"interest": data});
  703. },
  704.  
  705. _getInterestsList: function (channelConfig) {
  706. var dfd = $.Deferred();
  707. var particlesSrv = window.miServices.get(SERVICE.PERSONALIZED_ARTICLES);
  708. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  709. var publishingMode = false;
  710. particlesSrv.isPublishingMode()
  711. .then(function (isPublishingMode) {
  712. publishingMode = isPublishingMode;
  713. if (!isPublishingMode) {
  714. return loginSrv.getProfile();
  715. } else {
  716. return commonUtils.resolvedPromise();
  717. }
  718. })
  719. .then(_.bind(function (profile) {
  720. var interestSrv = window.miServices.get(SERVICE.INTERESTS);
  721. var newsItems = [];
  722. _.each(channelConfig, function (config, key) {
  723. if (config.isNews) {
  724. newsItems.push({
  725. id: key,
  726. name: config.name,
  727. isNews: true
  728. });
  729. }
  730. });
  731. newsItems.sort(function (a, b) {
  732. return a.name < b.name;
  733. });
  734. if (profile) {
  735. return interestSrv.getItems()
  736. .then(function (interests) {
  737. var profileInterests = [];
  738. if (profile.speciality) {
  739. _.each(interests, function (interest) {
  740. if (profile.speciality.indexOf(interest.name) > -1) {
  741. profileInterests.push({
  742. id: interest.id,
  743. name: interest.name
  744. });
  745. }
  746. });
  747. }
  748. var allItems = newsItems.concat(profileInterests);
  749. return commonUtils.resolvedPromise(allItems);
  750. });
  751. } else {
  752. if (publishingMode) {
  753. return interestSrv.getItems()
  754. .then(function (interests) {
  755. var allItems = newsItems.concat(interests);
  756. return commonUtils.resolvedPromise(allItems);
  757. });
  758. } else {
  759. return commonUtils.resolvedPromise([]);
  760. }
  761. }
  762. }, this))
  763. .then(_.bind(dfd.resolve, dfd))
  764. .fail(_.bind(dfd.reject, dfd));
  765. return dfd.promise();
  766. },
  767.  
  768. _getPublishingViewMode: function () {
  769. if (!$('body').hasClass('editor-publishing')) {
  770. return '';
  771. }
  772. var $editorMode = $('.editor-view-mode');
  773. if ($editorMode.hasClass('editor-view-mode-published')) {
  774. return 'published';
  775. }
  776. if ($editorMode.hasClass('editor-view-mode-not-published')) {
  777. return 'not-published';
  778. }
  779. if ($editorMode.hasClass('editor-view-mode-skipped')) {
  780. return 'skipped';
  781. }
  782. return ''; // should not be reached
  783. },
  784.  
  785. _renderInterestLinks: function (chanelConfig) {
  786. var dfd = $.Deferred();
  787. var nav = $('#interests-nav');
  788. nav.children().remove();
  789.  
  790. this._getInterestsList(chanelConfig)
  791. .then(_.bind(function (interests) {
  792. var view = new InterestsLinksSidebarView({
  793. model: interests
  794. });
  795. view.render().promise()
  796. .done(function () {
  797. nav.append(view.el);
  798. dfd.resolve();
  799. });
  800. view.on('interest-selected', _.bind(function (interest) {
  801. var publishingViewMode = this._getPublishingViewMode();
  802. var path;
  803. if (publishingViewMode) {
  804. this._navigate(['/editor', publishingViewMode, interest].join('/'));
  805. } else {
  806. this._navigate("/interest/" + interest);
  807. }
  808. }, this));
  809. }, this))
  810. .fail(function (err) {
  811. console.error(err);
  812. dfd.reject(err);
  813. });
  814. return dfd.promise();
  815. },
  816.  
  817. _renderLayoutTabArticle: function () {
  818. var dfd = $.Deferred();
  819. var layout = new SimpleLayout({});
  820. this._layoutTabArticle = layout;
  821. $(TABS.ARTICLES.id).append(layout.el);
  822. return layout.render().promise();
  823. },
  824.  
  825. _renderTabArticle: function () {
  826. var view = new ReadingZoneMainView();
  827. this.mainView = view;
  828. view.on('command', _.bind(this._onChangeArticleState, this));
  829. view.on('load-more', _.bind(this._onLoadMore, this));
  830. var dfd = $.Deferred();
  831.  
  832. this._layoutTabArticle.pushView(view)
  833. .then(_.bind(this._showArticles, this))
  834. .then(_.bind(this._scrollToArticle, this))
  835. .then(_.bind(dfd.resolve, dfd))
  836. .fail(function (err) {
  837. console.error(err);
  838. dfd.resolve();
  839. });
  840. return dfd.promise();
  841. },
  842.  
  843. _onLoadMore: function (nextPartIndex, interestId) {
  844. console.log('loading part with index: ' + nextPartIndex);
  845. var srv = window.miServices.get(SERVICE.PERSONALIZED_ARTICLES);
  846. var promise,
  847. articlesState = this._getPublishingViewMode();
  848. if (interestId) {
  849. promise = srv.getInterestArticles(articlesState, interestId, nextPartIndex);
  850. } else {
  851. promise = srv.getInterestsArticles(articlesState, undefined, nextPartIndex);
  852. }
  853. promise.then(_.bind(function (data) {
  854. var rawArticles = data.result || data.articles || [];
  855. if (rawArticles.length > 0 && _.isArray(rawArticles[0].articles)) {
  856. rawArticles = _.chain(rawArticles).pluck("articles").flatten().sortBy("creationDate").value();
  857. } else {
  858. rawArticles = rawArticles;
  859. }
  860. var articles = _colorize(rawArticles, data.channelConfig);
  861. this.mainView.showArticles(articles, nextPartIndex, true);
  862. }, this));
  863. },
  864.  
  865. _displayProfileSettings: function () {
  866. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  867. loginSrv.getProfile()
  868. .then(_.bind(function (profile) {
  869. if (profile) {
  870. this._renderLayoutTabProfile();
  871. this._renderTabProfile();
  872. } else {
  873. $('#profile-page-link').hide();
  874. $('#personalize-nav').hide();
  875. }
  876. }, this))
  877. .fail(function (err) {
  878. console.error(err);
  879. });
  880. },
  881.  
  882. _renderLayoutTabProfile: function () {
  883. $('#profile-page-link').show();
  884. $('#personalize-nav').show();
  885. var layout = new SimpleLayout({});
  886. this._layoutTabProfile = layout;
  887. $(TABS.PROFILE.id).append(layout.el);
  888. return layout.render().promise();
  889. },
  890.  
  891. _renderTabProfile: function () {
  892. if (!this.profileView) {
  893. this.profileView = new InterestSettingsView();
  894. this._layoutTabProfile.pushView(this.profileView);
  895. }
  896. },
  897.  
  898. _displayMyCollections: function () {
  899. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  900. loginSrv.getProfile()
  901. .then(_.bind(function (profile) {
  902. if (profile) {
  903. this._renderLayoutTabCollections();
  904. this._renderTabCollections();
  905. } else {
  906. $('#collections-page-link').hide();
  907. }
  908. }, this))
  909. .fail(function (err) {
  910. console.error(err);
  911. });
  912. },
  913.  
  914. _renderLayoutTabCollections: function () {
  915. $('#collections-page-link').show();
  916. $('#personalize-nav').show();
  917. var layout = new SimpleLayout({});
  918. this._layoutTabProfile = layout;
  919. $(TABS.COLLECTIONS.id).append(layout.el);
  920. return layout.render().promise();
  921. },
  922.  
  923. _renderTabCollections: function () {
  924. if (!this.collectionsView) {
  925. this.collectionsView = new InterestSettingsView();
  926. this._layoutTabProfile.pushView(this.collectionsView);
  927. }
  928. },
  929.  
  930. _displayUserVerification: function () {
  931. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  932. var particlesSrv = window.miServices.get(SERVICE.PERSONALIZED_ARTICLES);
  933. loginSrv.getProfile()
  934. .then(_.bind(function (profile) {
  935. if (profile) {
  936. return particlesSrv.isEditor();
  937. }
  938. }, this))
  939. .then(_.bind(function (isEditor) {
  940. if (isEditor) {
  941. this._renderLayoutTabVerification();
  942. this._renderTabVerification();
  943. } else {
  944. $('#verification-page-link').hide();
  945. }
  946. }, this))
  947. .fail(function (err) {
  948. console.error(err);
  949. });
  950. },
  951.  
  952. _renderLayoutTabVerification: function () {
  953. $('#verification-page-link').show();
  954. var layout = new SimpleLayout({});
  955. this._layoutTabVerification = layout;
  956. $(TABS.VERIFICATION.id).append(layout.el);
  957. return layout.render().promise();
  958. },
  959.  
  960. _renderTabVerification: function () {
  961. if (!this.verificationView) {
  962. this.verificationView = new UserVerificationView();
  963. this._layoutTabVerification.pushView(this.verificationView);
  964. }
  965. },
  966.  
  967. _displayCollectionsAdmin: function () {
  968. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  969. var particlesSrv = window.miServices.get(SERVICE.PERSONALIZED_ARTICLES);
  970. loginSrv.getProfile()
  971. .then(_.bind(function (profile) {
  972. if (profile) {
  973. return particlesSrv.isEditor();
  974. }
  975. }, this))
  976. .then(_.bind(function (isEditor) {
  977. if (isEditor) {
  978. this._renderLayoutTabCollectionsAdmin();
  979. this._renderTabCollectionsAdmin();
  980. } else {
  981. $('#collections-admin-page-link').hide();
  982. }
  983. }, this))
  984. .fail(function (err) {
  985. console.error(err);
  986. });
  987. },
  988.  
  989. _renderLayoutTabCollectionsAdmin: function () {
  990. $('#collections-admin-page-link').show();
  991. var layout = new SimpleLayout({});
  992. this._layoutTabCollectionsAdmin = layout;
  993. $(TABS.COLLECTIONS_ADMIN.id).append(layout.el);
  994. return layout.render().promise();
  995. },
  996.  
  997. _renderTabCollectionsAdmin: function () {
  998. if (!this.collectionsAdminView) {
  999. this.collectionsAdminView = new CollectionsAdminView();
  1000. this._layoutTabCollectionsAdmin.pushView(this.collectionsAdminView);
  1001. }
  1002. },
  1003.  
  1004. _onChangeArticleState: function (action, articleId) {
  1005. console.log('change article state: ' + action + ', id: ' + articleId);
  1006. var srv = window.miServices.get(SERVICE.PERSONALIZED_ARTICLES);
  1007. srv.changePublishingStatus(articleId, action)
  1008. .then(function () {
  1009. utils.notifyInfo('Article state changed');
  1010. Backbone.trigger('article-state-chaged', articleId, action);
  1011. Backbone.trigger('keen:event', {
  1012. type: 'publishing',
  1013. action: action,
  1014. articleId: articleId
  1015. });
  1016. })
  1017. .fail(function (err) {
  1018. utils.notifyError('Fail to change article state');
  1019. console.error(err);
  1020. });
  1021. },
  1022.  
  1023. _showArticles: function (selectedInterest, forArticlesState) {
  1024. var srv = window.miServices.get(SERVICE.PERSONALIZED_ARTICLES);
  1025. var channelConfig, promise;
  1026. if (selectedInterest) {
  1027. var articlesState = this._getPublishingViewMode();
  1028. if (this.newslettersView) { // reload articles after newsletters view
  1029. delete this.newslettersView;
  1030. promise = srv.getInterestArticles(articlesState, selectedInterest)
  1031. .then(_.bind(function (data) {
  1032. var articles = _colorize(data.articles, data.channelConfig);
  1033. channelConfig = data.channelConfig;
  1034. return this.mainView.showArticles(articles, selectedInterest);
  1035. }, this));
  1036. } else {
  1037. promise = srv.getInterestArticles(articlesState, selectedInterest)
  1038. .then(_.bind(function (data) {
  1039. var articles = _colorize(data.articles, data.channelConfig);
  1040. channelConfig = data.channelConfig;
  1041. return this.mainView.showArticles(articles, selectedInterest);
  1042. }, this));
  1043. }
  1044. } else {
  1045. promise = srv.getInterestsArticles(forArticlesState)
  1046. .then(_.bind(function (interests) {
  1047. var result = interests.result;
  1048. var articles = [];
  1049. if (result.length > 0 && !_.isUndefined(result[0].articles)) {
  1050. articles = _.chain(result).pluck("articles").flatten().sortBy("creationDate").value();
  1051. } else {
  1052. articles = result;
  1053. }
  1054. articles = _colorize(articles, interests.channelConfig);
  1055. channelConfig = interests.channelConfig;
  1056. return this.mainView.showArticles(articles);
  1057. }, this));
  1058. }
  1059. return promise.then(_.bind(function () {
  1060. this._renderInterestLinks(channelConfig);
  1061. }, this));
  1062. },
  1063.  
  1064. _removeInterest: function (interest) {
  1065. console.log('NYI: remove interest: ' + interest);
  1066. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  1067. loginSrv.getProfile()
  1068. .then(function (profile) {
  1069. if (!profile) { // not loged in
  1070. return commonUtils.resolvedPromise();
  1071. }
  1072. var interests = [];
  1073. if (profile.speciality) {
  1074. interests = profile.speciality.split(';');
  1075. }
  1076. interests = _.filter(interests, function (item) {
  1077. return item !== interest;
  1078. });
  1079. profile.speciality = interests.join(';');
  1080. return loginSrv.updateProfile(profile);
  1081. })
  1082. .then(_.bind(function () {
  1083. return this._navigate('profile/settings');
  1084. }, this))
  1085. .fail(function (err) {
  1086. console.error(err);
  1087. });
  1088. },
  1089.  
  1090. _publisherModeConfiguration: function () {
  1091. var dfd = $.Deferred();
  1092. var srv = window.miServices.get(SERVICE.PERSONALIZED_ARTICLES);
  1093. srv.isPublishingMode()
  1094. .then(_.bind(function (isPublishingMode, isEditor) {
  1095. if (isEditor) {
  1096. $('body').addClass('editor');
  1097. if (!this._publishingModeInitialized) {
  1098. $('.editor-label input').change(_.bind(function (evt) {
  1099. var $switch = $('.editor-label .mdl-switch');
  1100. var checked = $switch.hasClass('is-checked');
  1101. this._switchViewMode(checked ? 'publisher' : 'user');
  1102. srv.setPublishingMode(checked);
  1103. _showWaitingSpinner(true);
  1104. this._showArticles()
  1105. .then(function () {
  1106. _showWaitingSpinner(false);
  1107. })
  1108. .fail(function (err) {
  1109. console.error(err);
  1110. _showWaitingSpinner(false);
  1111. });
  1112. }, this));
  1113. this._publishingModeInitialized = true;
  1114. }
  1115. } else {
  1116. $('body').removeClass('editor');
  1117. }
  1118. if (isPublishingMode) {
  1119. this._switchViewMode('publisher');
  1120. }
  1121. dfd.resolve();
  1122. }, this));
  1123. return dfd.promise();
  1124. },
  1125.  
  1126. _switchViewMode: function (mode) {
  1127. var mswitch = $('.editor-label input').parent()[0].MaterialSwitch;
  1128. switch (mode) {
  1129. case 'user':
  1130. $('body').removeClass('editor-publishing');
  1131. mswitch.off();
  1132. this._navigate('');
  1133. break;
  1134. case 'publisher':
  1135. $('body').addClass('editor-publishing');
  1136. mswitch.on();
  1137. this._navigate('editor/published/');
  1138. break;
  1139. }
  1140. },
  1141.  
  1142. _hideLoadingSpinner: function () {
  1143. $('#loading').css('display', 'none');
  1144. },
  1145.  
  1146. _scrollToArticle: function () {
  1147.  
  1148. function waitForElementToDisplay(selector, time) {
  1149. if (document.querySelector(selector) != null) {
  1150. var articleId = utils.getParam("articleId");
  1151. var articleCard = document.getElementById(articleId);
  1152. if (articleCard) {
  1153. var container = $('.mdl-layout__content');
  1154. var scrollTo = $(articleCard);
  1155. var DELTA = 10;
  1156. container.animate({
  1157. scrollTop: scrollTo.offset().top - (container.scrollTop() + scrollTo.height() + container.offset().top + DELTA)
  1158. });
  1159. }
  1160. } else {
  1161. setTimeout(function () {
  1162. waitForElementToDisplay(selector, time);
  1163. }, time);
  1164. }
  1165. }
  1166.  
  1167. waitForElementToDisplay('.reading-view', 300);
  1168. },
  1169.  
  1170. _updateDataAfterLogin: function () {
  1171. var srv = window.miServices.get(SERVICE.UPDATES);
  1172. if (srv) {
  1173. return srv.update('login');
  1174. }
  1175. return utils.resolvedPromise();
  1176. },
  1177.  
  1178. // _login: function(fieldValues) {
  1179. // var srv = window.miServices.get(SERVICE.LOGIN);
  1180. // return srv.login(fieldValues.email, fieldValues.password);
  1181. // },
  1182.  
  1183. _logout: function () {
  1184. _showWaitingSpinner(true);
  1185. var srv = window.miServices.get(SERVICE.LOGIN);
  1186. srv.logout().always(_.bind(function () {
  1187. _showWaitingSpinner(false);
  1188. Backbone.trigger('show-auth-buttons');
  1189. this._navigate("");
  1190. }, this));
  1191. },
  1192.  
  1193. _initUserProfile: function () {
  1194. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  1195. return loginSrv.getProfile(true)
  1196. .then(function (profile) {
  1197. return loginSrv.updateProfile(profile);
  1198. });
  1199. },
  1200.  
  1201. _showSignupDialog: function () {
  1202. var dfd = $.Deferred();
  1203. Backbone.trigger('fb:event', 'SIGNUP', {
  1204. STATE: 'started',
  1205. PARTNER_RELATION: ''
  1206. });
  1207. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  1208. var view = new SginupView({
  1209. title: 'Sign Up'
  1210. });
  1211. $('body').append(view.render().$el);
  1212.  
  1213. view.on('signup', _.bind(function (cmd, data) {
  1214. switch (cmd) {
  1215. case 'facebook':
  1216. this._onSocialLogin(cmd)
  1217. .then(_.bind(function (signedUp) {
  1218. view.remove();
  1219. var navigationPath = signedUp === true ? 'profile/settings' : ''; // new/existing user
  1220. if (signedUp) {
  1221. fbReportSignupSuccess('facebook');
  1222. } else {
  1223. fbReportSigninSuccess('facebook');
  1224. }
  1225. this._navigate(navigationPath);
  1226. }, this))
  1227. .fail(function (err) {
  1228. console.error(err);
  1229. view.showError(err);
  1230. fbReportSignupFail('facebook', err);
  1231. });
  1232. break;
  1233. case 'google':
  1234. this._onSocialLogin(cmd)
  1235. .then(_.bind(function (signedUp) {
  1236. view.remove();
  1237. var navigationPath = signedUp === true ? 'profile/settings' : ''; // new/existing user
  1238. if (signedUp) {
  1239. fbReportSignupSuccess('google');
  1240. } else {
  1241. fbReportSigninSuccess('google');
  1242. }
  1243. this._navigate(navigationPath);
  1244. }, this))
  1245. .fail(function (err) {
  1246. console.error(err);
  1247. view.showError(err);
  1248. fbReportSignupFail('google', err);
  1249. });
  1250. break;
  1251. case 'mi':
  1252. console.log(data);
  1253. loginSrv.signup(data)
  1254. .then(_.bind(function () {
  1255. console.log('signup successfull');
  1256. return this._initUserProfile();
  1257. }, this))
  1258. .then(function () {
  1259. Backbone.trigger('keen:event', {
  1260. type: 'signup',
  1261. action: 'create-account',
  1262. success: true
  1263. });
  1264. fbReportSignupSuccess('mi');
  1265. return commonUtils.resolvedPromise();
  1266. })
  1267. .then(_.bind(function () {
  1268. console.log('empty profile initialized');
  1269. view.remove();
  1270. this._navigate('profile/settings');
  1271. }, this))
  1272. .fail(function (err) {
  1273. console.error(err);
  1274. var errMsg = 'Signup Error';
  1275. if (!_.isUndefined(err.message)) {
  1276. errMsg = err.message;
  1277. }
  1278. view.showError(errMsg);
  1279. Backbone.trigger('keen:event', {
  1280. type: 'signup',
  1281. action: 'create-account',
  1282. success: false,
  1283. error: errMsg
  1284. });
  1285. fbReportSignupFail('mi', errMsg);
  1286. });
  1287. break;
  1288. case 'cancel':
  1289. Backbone.trigger('fb:event', 'SIGNUP', {
  1290. STATE: 'aborted',
  1291. PARTNER_RELATION: ''
  1292. });
  1293. break;
  1294. }
  1295. }, this));
  1296. return dfd.promise();
  1297. },
  1298.  
  1299. _onSocialLogin: function (socialProvider) {
  1300. var dfd = $.Deferred();
  1301. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  1302. switch (socialProvider) {
  1303. case 'facebook':
  1304. var fbSrv = window.miServices.get(SERVICE.FACEBOOK);
  1305. fbSrv.showAuthentication()
  1306. .then(_.bind(function (profileData, authData) {
  1307. console.log('FB logged in');
  1308. console.log(profileData);
  1309. console.log(authData);
  1310. loginSrv.loginSocial(socialProvider, profileData.id, fbSrv.getProviderData())
  1311. .then(function () {
  1312. Backbone.trigger('keen:event', {
  1313. type: 'login',
  1314. action: 'login-facebook',
  1315. success: true
  1316. });
  1317. return commonUtils.resolvedPromise();
  1318. })
  1319. .then(_.bind(dfd.resolve, dfd))
  1320. .fail(_.bind(function (err) {
  1321. if (err.todo === 'register-social-account') {
  1322. var accountData = {
  1323. email: profileData.original.email,
  1324. profile: profileData
  1325. };
  1326. loginSrv.signupSocial(accountData, fbSrv.getProviderData())
  1327. .then(_.bind(this._initUserProfile, this))
  1328. .then(function () {
  1329. Backbone.trigger('keen:event', {
  1330. type: 'signup',
  1331. action: 'signup-facebook',
  1332. success: true
  1333. });
  1334. return commonUtils.resolvedPromise();
  1335. })
  1336. .then(_.bind(dfd.resolve, dfd, true))
  1337. .fail(function (err) {
  1338. Backbone.trigger('keen:event', {
  1339. type: 'signup',
  1340. action: 'signup-facebook',
  1341. success: false,
  1342. error: err
  1343. });
  1344. dfd.reject(err);
  1345. });
  1346. } else {
  1347. dfd.reject(err);
  1348. }
  1349. }, this));
  1350. }, this))
  1351. .fail(function (err) {
  1352. console.error(err);
  1353. Backbone.trigger('keen:event', {
  1354. type: 'login',
  1355. action: 'login-facebook',
  1356. success: false,
  1357. error: err
  1358. });
  1359. dfd.reject(err);
  1360. });
  1361. break;
  1362. case 'google':
  1363. var gSrv = window.miServices.get(SERVICE.GOOGLE);
  1364. gSrv.showAuthentication()
  1365. .then(_.bind(function (profileData, authData) {
  1366. console.log('Google logged in');
  1367. console.log(profileData);
  1368. console.log(authData);
  1369. loginSrv.loginSocial(socialProvider, profileData.id, gSrv.getProviderData())
  1370. .then(function () {
  1371. Backbone.trigger('keen:event', {
  1372. type: 'login',
  1373. action: 'login-google',
  1374. success: true
  1375. });
  1376. return commonUtils.resolvedPromise();
  1377. })
  1378. .then(_.bind(dfd.resolve, dfd))
  1379. .fail(_.bind(function (err) {
  1380. if (err.todo === 'register-social-account') {
  1381. var accountData = {
  1382. email: profileData.original.email,
  1383. profile: profileData
  1384. };
  1385. loginSrv.signupSocial(accountData, gSrv.getProviderData())
  1386. .then(_.bind(this._initUserProfile, this))
  1387. .then(function () {
  1388. Backbone.trigger('keen:event', {
  1389. type: 'signup',
  1390. action: 'signup-google',
  1391. success: true
  1392. });
  1393. return commonUtils.resolvedPromise();
  1394. })
  1395. .then(_.bind(dfd.resolve, dfd, true))
  1396. .fail(function (err) {
  1397. Backbone.trigger('keen:event', {
  1398. type: 'signup',
  1399. action: 'signup-google',
  1400. success: false,
  1401. error: err
  1402. });
  1403. dfd.reject(err);
  1404. });
  1405. } else {
  1406. dfd.reject(err);
  1407. }
  1408. }, this));
  1409. }, this))
  1410. .fail(function (err) {
  1411. console.error(err);
  1412. Backbone.trigger('keen:event', {
  1413. type: 'login',
  1414. action: 'login-google',
  1415. success: false,
  1416. error: err
  1417. });
  1418. dfd.reject(err);
  1419. });
  1420. break;
  1421. default:
  1422. dfd.reject({
  1423. message: 'Not recognized social Provider: ' + socialProvider
  1424. });
  1425. }
  1426. return dfd.promise();
  1427. },
  1428.  
  1429. _resetPassword: function () {
  1430. var view = new ResetPasswordView();
  1431. $('body').append(view.render().$el);
  1432. view.on('reset-password', _.bind(function (email) {
  1433. var loginSrv = window.miServices.get(SERVICE.LOGIN);
  1434. loginSrv.resetPassword(email)
  1435. .then(function () {
  1436. _showWaitingSpinner(false);
  1437. view.showSuccess();
  1438. })
  1439. .fail(function (err) {
  1440. var msg = 'Fail to reset password';
  1441. if (err.message) {
  1442. msg = err.message;
  1443. } else {
  1444. if (err.errors && err.errors.length > 0) {
  1445. msg = err.errors[0];
  1446. }
  1447. }
  1448. _showWaitingSpinner(false);
  1449. view.showError(msg);
  1450. });
  1451. }, this));
  1452. view.on('closed', function () {
  1453. window.history.back();
  1454. });
  1455. },
  1456.  
  1457. _showArticle: function (articleId) {
  1458. var articleService = window.miServices.get(SERVICE.ARTICLE);
  1459. articleService.getArticle(articleId)
  1460. .then(_.bind(function (article) {
  1461. var view = new ArticleView({
  1462. article: article
  1463. });
  1464. this.mainView = view;
  1465. $('meta[property="og:title"]').attr('content', article.title);
  1466. $('meta[property="og:url"]').attr('content', platformConfig.readingZoneUrl + 'wrapper/article/' + article._id);
  1467. if (article.journal === 'youtube.com') {
  1468. $('meta[property="og:description"]').attr('content', article.content);
  1469. } else {
  1470. $('meta[property="og:description"]').attr('content', article.excerpt);
  1471. }
  1472. if (article.previewImageUrl) {
  1473. $('meta[property="og:image"]').attr('content', platformConfig.readingZoneUrl + 'wrapper/proxy?url=' + encodeURIComponent(article.previewImageUrl));
  1474. }
  1475.  
  1476. var dfd = $.Deferred();
  1477.  
  1478. this._layoutTabArticle.pushView(view)
  1479. .then(_.bind(dfd.resolve, dfd))
  1480. .fail(function (err) {
  1481. console.error(err);
  1482. dfd.resolve();
  1483. });
  1484.  
  1485. view.on('back', _.bind(function () {
  1486. this._renderTabArticle();
  1487. this._back();
  1488. }, this));
  1489. return dfd.promise();
  1490. }, this))
  1491. .fail(function (err) {
  1492. //TODO: error handling
  1493. });
  1494. },
  1495.  
  1496. _login: function (email) {
  1497. this._showLogin = true;
  1498. this._email = email;
  1499. },
  1500.  
  1501. checkIsItDevice: function () {
  1502. /* jshint ignore:start */
  1503. var ua = navigator.userAgent;
  1504. var checker = {
  1505. iphone: ua.match(/(iPhone|iPod|iPad)/),
  1506. blackberry: ua.match(/BlackBerry/),
  1507. android: ua.match(/Android/)
  1508. };
  1509.  
  1510. return (checker.iphone || checker.blackberry || checker.android);
  1511.  
  1512. /* jshint ignore:end */
  1513. }
  1514. });
  1515. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement