diff --git a/lms/static/coffee/src/discussion/views/new_post_view.coffee b/lms/static/coffee/src/discussion/views/new_post_view.coffee index 9bd3322657..67b28695a7 100644 --- a/lms/static/coffee/src/discussion/views/new_post_view.coffee +++ b/lms/static/coffee/src/discussion/views/new_post_view.coffee @@ -2,68 +2,155 @@ class @NewPostView extends Backbone.View initialize: () -> @dropdownButton = @$(".topic_dropdown_button") + @topicMenu = @$(".topic_menu_wrapper") - #events: - # "submit .new-post-form": "createPost" - # "click .topic_dropdown_button": "toggleTopicDropdown" - # "click .topic_menu": "setTopic" + @menuOpen = @dropdownButton.hasClass('dropped') - #toggleTopicDropdown: (event) -> - # if $target.hasClass('dropped') - # @showTopicDropdown() - # else - # @hideTopicDropdown() - # - #showTopicDropdown: () -> - # #$button = - # $button.addClass('dropped') + @topicId = @$(".topic").first().data("discussion_id") + @topicText = @getFullTopicName(@$(".topic").first()) - # $topicMenu = @$(".topic_menu") - # $topicMenu.show() + @setSelectedTopic() - #createPost: (event) -> - # event.preventDefault() + events: + "submit .new-post-form": "createPost" + "click .topic_dropdown_button": "toggleTopicDropdown" + "click .topic_menu_wrapper": "setTopic" + "click .topic_menu_search": "ignoreClick" - # title = @$(".new-post-title").val() - # body = @$(".new-post-body").val() - # tags = @$(".new-post-tags").val() + # Because we want the behavior that when the body is clicked the menu is + # closed, we need to ignore clicks in the search field and stop propagation. + # Without this, clicking the search field would also close the menu. + ignoreClick: (event) -> + event.stopPropagation() - # anonymous = false || @$("input.discussion-anonymous").is(":checked") - # follow = false || @$("input.discussion-follow").is(":checked") + toggleTopicDropdown: (event) -> + event.stopPropagation() + if @menuOpen + @hideTopicDropdown() + else + @showTopicDropdown() + + showTopicDropdown: () -> + console.log "showing" + @menuOpen = true + @dropdownButton.addClass('dropped') + @topicMenu.show() - # $formTopicDropBtn.bind('click', showFormTopicDrop); - # $formTopicDropMenu.bind('click', setFormTopic); + $('body').bind 'click', @hideTopicDropdown - # url = DiscussionUtil.urlFor('create_thread', @model.id) + # Set here because 1) the window might get resized and things could + # change and 2) can't set in initialize because the button is hidden + @maxNameWidth = @dropdownButton.width() * 0.9 - # DiscussionUtil.safeAjax - # $elem: $(event.target) - # $loading: $(event.target) if event - # url: url - # type: "POST" - # dataType: 'json' - # data: - # title: title - # body: body - # tags: tags - # anonymous: anonymous - # auto_subscribe: follow - # error: DiscussionUtil.formErrorHandler(@$(".new-post-form-errors")) - # success: (response, textStatus) => - # DiscussionUtil.clearFormErrors(@$(".new-post-form-errors")) - # $thread = $(response.html) - # @$el.children(".threads").prepend($thread) - # @$el.children(".blank").remove() + # Need a fat arrow because hideTopicDropdown is passed as a callback to bind + hideTopicDropdown: () => + @menuOpen = false + @dropdownButton.removeClass('dropped') + @topicMenu.hide() - # @$(".new-post-similar-posts").empty() - # @$(".new-post-similar-posts-wrapper").hide() - # @$(".new-post-title").val("").attr("prev-text", "") - # DiscussionUtil.setWmdContent @$el, $.proxy(@$, @), "new-post-body", "" - # @$(".new-post-tags").val("") - # @$(".new-post-tags").importTags("") + $('body').unbind 'click', @hideTopicDropdown - # thread = @model.addThread response.content - # threadView = new ThreadView el: $thread[0], model: thread - # thread.updateInfo response.annotated_content_info - # @cancelNewPost() + setTopic: (event) -> + $target = $(event.target) + if $target.data('discussion_id') + @topicText = $target.html() + @topicText = @getFullTopicName($target) + @topicId = $target.data('discussion_id') + @setSelectedTopic() + else + console.log "NOTHING IN " + console.log $target + + setSelectedTopic: -> + @dropdownButton.html(@fitName(@topicText) + ' ▾') + + getFullTopicName: (topicElement) -> + name = topicElement.html() + topicElement.parents('ul').not('.topic_menu').each -> + name = $(this).siblings('a').html() + ' / ' + name + return name + + getNameWidth: (name) -> + test = $("