service.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. define( [], function() {
  2. var view = Marionette.View.extend( {
  3. template: '#tmpl-nf-service',
  4. className: function(){
  5. return 'nf-extend nf-box ' + this.model.get( 'classes' );
  6. },
  7. ui: {
  8. install: '.js--install',
  9. learnMore: '.js--learn-more',
  10. enabled: '.nf-toggle.setting',
  11. toggleEnable: '.nf-toggle + label',
  12. },
  13. events: {
  14. 'click @ui.install': function() {
  15. nfRadio.channel( 'dashboard' ).request( 'install:service', this.model );
  16. },
  17. 'click @ui.learnMore': function() {
  18. this.showLearnMore();
  19. },
  20. 'click @ui.toggleEnable': function() {
  21. if( null == this.model.get( 'enabled' ) ){
  22. if( this.model.get( 'link' ) ){
  23. window.location = this.model.get( 'link' );
  24. return this.render();
  25. }
  26. }
  27. this.model.set( 'enabled', ! this.model.get( 'enabled' ) );
  28. this.model.save("enabled");
  29. this.render();
  30. },
  31. },
  32. initialize: function( oauthModel ) {
  33. this.updateOAuth();
  34. this.listenTo( this.model, 'change', this.render );
  35. // Show the "Learn More" modal.
  36. nfRadio.channel( 'dashboard' ).reply( 'more:service:' + this.model.get( 'slug' ), this.showLearnMore, this );
  37. // Update connected status when the OAuth model is synced with the server.
  38. this.listenTo( nfRadio.channel( 'dashboard' ), 'fetch:oauth', this.updateOAuth );
  39. // Re-render the view after syncing with the server.
  40. this.listenTo( nfRadio.channel( 'dashboard' ), 'save:service-' + this.model.get( 'slug' ), this.render );
  41. },
  42. /*
  43. * Open the modal with the "Learn More" content.
  44. */
  45. showLearnMore: function() {
  46. var that = this;
  47. var modal = new jBox( 'Modal', {
  48. width: 750,
  49. title: this.model.get( 'learnMoreTitle' ) || this.model.get( 'name' ),
  50. content: this.model.get( 'learnMore' ),
  51. closeButton: 'box',
  52. blockScroll: true
  53. } );
  54. modal.open();
  55. // Add a radio call for accessing the modal instance.
  56. nfRadio.channel( 'dashboard' ).reply( 'service:' + this.model.get( 'slug' ) + ':modal', function(){
  57. return modal;
  58. } );
  59. },
  60. /*
  61. * Update connected status when the OAuth model is synced with the server.
  62. */
  63. updateOAuth: function() {
  64. var oauth = nfRadio.channel( 'dashboard' ).request( 'get:oauth' );
  65. this.connected = oauth.get( 'connected' );
  66. this.render();
  67. },
  68. templateContext: function() {
  69. return {
  70. is_connected: this.connected,
  71. }
  72. }
  73. } );
  74. return view;
  75. } );