lookup.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var _ = require('underscore');
  2. var $ = require('jquery');
  3. var AppState = require('../app_state');
  4. var Pubsub = require('../lib/pubsub');
  5. var BaseView = require('./base');
  6. var Topic = require('../models/topic');
  7. var Channel = require('../models/channel');
  8. var LookupView = BaseView.extend({
  9. className: 'lookup container-fluid',
  10. template: require('./spinner.hbs'),
  11. events: {
  12. 'click .hierarchy button': 'onCreateTopicChannel',
  13. 'click .delete-topic-link': 'onDeleteTopic',
  14. 'click .delete-channel-link': 'onDeleteChannel'
  15. },
  16. initialize: function() {
  17. BaseView.prototype.initialize.apply(this, arguments);
  18. var isAdmin = arguments[0]['isAdmin'];
  19. $.ajax(AppState.apiPath('/topics?inactive=true'))
  20. .done(function(data) {
  21. this.template = require('./lookup.hbs');
  22. this.render({
  23. 'topics': _.map(data['topics'], function(v, k) {
  24. return {'name': k, 'channels': v};
  25. }),
  26. 'message': data['message'],
  27. 'isAdmin': isAdmin
  28. });
  29. }.bind(this))
  30. .fail(this.handleViewError.bind(this))
  31. .always(Pubsub.trigger.bind(Pubsub, 'view:ready'));
  32. },
  33. onCreateTopicChannel: function(e) {
  34. e.preventDefault();
  35. e.stopPropagation();
  36. var topic = $(e.target.form.elements['topic']).val();
  37. var channel = $(e.target.form.elements['channel']).val();
  38. if (topic === '' && channel === '') {
  39. return;
  40. }
  41. $.post(AppState.apiPath('/topics'), JSON.stringify({
  42. 'topic': topic,
  43. 'channel': channel
  44. }))
  45. .done(function() { window.location.reload(true); })
  46. .fail(this.handleAJAXError.bind(this));
  47. },
  48. onDeleteTopic: function(e) {
  49. e.preventDefault();
  50. e.stopPropagation();
  51. var topic = new Topic({
  52. 'name': $(e.target).data('topic')
  53. });
  54. topic.destroy({'dataType': 'text'})
  55. .done(function() { window.location.reload(true); })
  56. .fail(this.handleAJAXError.bind(this));
  57. },
  58. onDeleteChannel: function(e) {
  59. e.preventDefault();
  60. e.stopPropagation();
  61. var channel = new Channel({
  62. 'topic': $(e.target).data('topic'),
  63. 'name': $(e.target).data('channel')
  64. });
  65. channel.destroy({'dataType': 'text'})
  66. .done(function() { window.location.reload(true); })
  67. .fail(this.handleAJAXError.bind(this));
  68. }
  69. });
  70. module.exports = LookupView;