From 9a0798b68f4ea4f4464b0687bc3bd6a001b4a767 Mon Sep 17 00:00:00 2001 From: Ibrahim Awwal Date: Wed, 12 Sep 2012 15:13:40 -0700 Subject: [PATCH] Remove old backbone views (the last place they were being used was the profile page). --- .../coffee/src/discussion/content.coffee | 366 +----------------- .../coffee/src/discussion/discussion.coffee | 169 -------- 2 files changed, 3 insertions(+), 532 deletions(-) diff --git a/lms/static/coffee/src/discussion/content.coffee b/lms/static/coffee/src/discussion/content.coffee index c803669316..d489782571 100644 --- a/lms/static/coffee/src/discussion/content.coffee +++ b/lms/static/coffee/src/discussion/content.coffee @@ -12,7 +12,7 @@ if Backbone? can_endorse: '.admin-endorse' can_delete: '.admin-delete' can_openclose: '.admin-openclose' - + urlMappers: {} urlFor: (name) -> @@ -59,7 +59,7 @@ if Backbone? @resetComments(@get('children')) remove: -> - + if @get('type') == 'comment' @get('thread').removeComment(@) @get('thread').trigger "comment:remove", @ @@ -69,7 +69,7 @@ if Backbone? @addContent: (id, content) -> @contents[id] = content @getContent: (id) -> @contents[id] - + @getInfo: (id) -> @contentInfos[id] @@ -79,362 +79,6 @@ if Backbone? @getContent(id).updateInfo(info) $.extend @contentInfos, infos - class @ContentView extends Backbone.View - - $: (selector) -> - @$local.find(selector) - - partial: - endorsed: (endorsed) -> - if endorsed - @$el.addClass("endorsed") - else - @$el.removeClass("endorsed") - - closed: (closed) -> # we should just re-render the whole thread, or update according to new abilities - if closed - @$el.addClass("closed") - @$(".admin-openclose").text "Re-open Thread" - else - @$el.removeClass("closed") - @$(".admin-openclose").text "Close Thread" - - voted: (voted) -> - @$(".discussion-vote-up").removeClass("voted") if voted != "up" - @$(".discussion-vote-down").removeClass("voted") if voted != "down" - @$(".discussion-vote-#{voted}").addClass("voted") if voted in ["up", "down"] - - votes_point: (votes_point) -> - @$(".discussion-votes-point").html(votes_point) - - comments_count: (comments_count) -> - @$(".comments-count").html(comments_count) - - subscribed: (subscribed) -> - if subscribed - @$(".discussion-follow-thread").addClass("discussion-unfollow-thread").html("Unfollow") - else - @$(".discussion-follow-thread").removeClass("discussion-unfollow-thread").html("Follow") - - ability: (ability) -> - for action, elemSelector of @model.actions - if not ability[action] - @$(elemSelector).parent().hide() - else - @$(elemSelector).parent().show() - - $discussionContent: -> - @_discussionContent ||= @$el.children(".discussion-content") - - $showComments: -> - @_showComments ||= @$(".discussion-show-comments") - - updateShowComments: -> - if @showed - @$showComments().html @$showComments().html().replace "Show", "Hide" - else - @$showComments().html @$showComments().html().replace "Hide", "Show" - - retrieved: -> - @$showComments().hasClass("retrieved") - - hideSingleThread: (event) -> - @$el.children(".comments").hide() - @showed = false - @updateShowComments() - - showSingleThread: (event) -> - if @retrieved() - @$el.children(".comments").show() - @showed = true - @updateShowComments() - else - $elem = $.merge @$(".thread-title"), @$showComments() - url = @model.urlFor('retrieve') - DiscussionUtil.safeAjax - $elem: $elem - $loading: @$(".discussion-show-comments") - type: "GET" - url: url - success: (response, textStatus) => - @showed = true - @updateShowComments() - @$showComments().addClass("retrieved") - @$el.children(".comments").replaceWith response.html - @model.resetComments response.content.children - @initCommentViews() - DiscussionUtil.bulkUpdateContentInfo response.annotated_content_info - - toggleSingleThread: (event) -> - if @showed - @hideSingleThread(event) - else - @showSingleThread(event) - - initCommentViews: -> - @$el.children(".comments").children(".comment").each (index, elem) => - model = @model.get('comments').find $(elem).attr("_id") - if not model.view - commentView = new CommentView el: elem, model: model - - reply: -> - if @model.get('type') == 'thread' - @showSingleThread() - $replyView = @$(".discussion-reply-new") - if $replyView.length - $replyView.show() - else - view = {} - view.id = @model.id - view.showWatchCheckbox = not @model.get('thread').get('subscribed') - html = Mustache.render DiscussionUtil.getTemplate('_reply'), view - @$discussionContent().append html - DiscussionUtil.makeWmdEditor @$el, $.proxy(@$, @), "reply-body" - @$(".discussion-submit-post").click $.proxy(@submitReply, @) - @$(".discussion-cancel-post").click $.proxy(@cancelReply, @) - @$(".discussion-reply").hide() - @$(".discussion-edit").hide() - - submitReply: (event) -> - url = @model.urlFor('reply') - - body = DiscussionUtil.getWmdContent @$el, $.proxy(@$, @), "reply-body" - - anonymous = false || @$(".discussion-post-anonymously").is(":checked") - autowatch = false || @$(".discussion-auto-watch").is(":checked") - - DiscussionUtil.safeAjax - $elem: $(event.target) - $loading: $(event.target) if event - url: url - type: "POST" - dataType: 'json' - data: - body: body - anonymous: anonymous - auto_subscribe: autowatch - error: DiscussionUtil.formErrorHandler @$(".discussion-errors") - success: (response, textStatus) => - DiscussionUtil.clearFormErrors @$(".discussion-errors") - $comment = $(response.html) - @$el.children(".comments").prepend $comment - DiscussionUtil.setWmdContent @$el, $.proxy(@$, @), "reply-body", "" - comment = @model.addComment response.content - commentView = new CommentView el: $comment[0], model: comment - comment.updateInfo response.annotated_content_info - if autowatch - @model.get('thread').set('subscribed', true) - @cancelReply() - - cancelReply: -> - $replyView = @$(".discussion-reply-new") - if $replyView.length - $replyView.hide() - @$(".discussion-reply").show() - @$(".discussion-edit").show() - - unvote: (event) -> - url = @model.urlFor('unvote') - $elem = @$(".discussion-vote") - DiscussionUtil.safeAjax - $elem: $elem - url: url - type: "POST" - success: (response, textStatus) => - @model.set('voted', '') - @model.set('votes_point', response.votes.point) - - vote: (event, value) -> - url = @model.urlFor("#{value}vote") - $elem = @$(".discussion-vote") - DiscussionUtil.safeAjax - $elem: $elem - url: url - type: "POST" - success: (response, textStatus) => - @model.set('voted', value) - @model.set('votes_point', response.votes.point) - - toggleVote: (event) -> - $elem = $(event.target) - value = $elem.attr("value") - $elem.toggleClass("is-cast") - return false - # if @model.get("voted") == value - # @unvote(event) - # else - # @vote(event, value) - - toggleEndorse: (event) -> - $elem = $(event.target) - url = @model.urlFor('endorse') - endorsed = @model.get('endorsed') - data = { endorsed: not endorsed } - DiscussionUtil.safeAjax - $elem: $elem - url: url - data: data - type: "POST" - success: (response, textStatus) => - @model.set('endorsed', not endorsed) - - toggleFollow: (event) -> - $elem = $(event.target) - subscribed = @model.get('subscribed') - if subscribed - url = @model.urlFor('unfollow') - else - url = @model.urlFor('follow') - DiscussionUtil.safeAjax - $elem: $elem - url: url - type: "POST" - success: (response, textStatus) => - @model.set('subscribed', not subscribed) - - toggleClosed: (event) -> - $elem = $(event.target) - url = @model.urlFor('close') - closed = @model.get('closed') - data = { closed: not closed } - DiscussionUtil.safeAjax - $elem: $elem - url: url - type: "POST" - data: data - success: (response, textStatus) => - @model.set('closed', not closed) - @model.set('ability', response.ability) - - edit: (event) -> - @$(".discussion-content-wrapper").hide() - $editView = @$(".discussion-content-edit") - if $editView.length - $editView.show() - else - view = {} - view.id = @model.id - if @model.get('type') == 'thread' - view.title = @model.get('title') - view.body = @model.get('body') - view.tags = @model.get('tags') - else - view.body = @model.get('body') - @$discussionContent().append Mustache.render DiscussionUtil.getTemplate("_edit_#{@model.get('type')}"), view - DiscussionUtil.makeWmdEditor @$el, $.proxy(@$, @), "#{@model.get('type')}-body-edit" - @$(".thread-tags-edit").tagsInput DiscussionUtil.tagsInputOptions() - @$(".discussion-submit-update").unbind("click").click $.proxy(@submitEdit, @) - @$(".discussion-cancel-update").unbind("click").click $.proxy(@cancelEdit, @) - - submitEdit: (event) -> - - url = @model.urlFor('update') - data = {} - if @model.get('type') == 'thread' - data.title = @$(".thread-title-edit").val() - data.body = DiscussionUtil.getWmdContent @$el, $.proxy(@$, @), "thread-body-edit" - data.tags = @$(".thread-tags-edit").val() - else - data.body = DiscussionUtil.getWmdContent @$el, $.proxy(@$, @), "comment-body-edit" - DiscussionUtil.safeAjax - $elem: $(event.target) - $loading: $(event.target) if event - url: url - type: "POST" - dataType: 'json' - data: data - error: DiscussionUtil.formErrorHandler @$(".discussion-update-errors") - success: (response, textStatus) => - DiscussionUtil.clearFormErrors @$(".discussion-update-errors") - @$discussionContent().replaceWith(response.html) - if @model.get('type') == 'thread' - @model = new Thread response.content - else - @model = new Comment $.extend {}, response.content, { thread: @model.get('thread') } - @reconstruct() - @model.updateInfo response.annotated_content_info, { forceUpdate: true } - - cancelEdit: (event) -> - @$(".discussion-content-edit").hide() - @$(".discussion-content-wrapper").show() - - delete: (event) -> - url = @model.urlFor('delete') - if @model.get('type') == 'thread' - c = confirm "Are you sure to delete thread \"#{@model.get('title')}\"?" - else - c = confirm "Are you sure to delete this comment? " - if not c - return - $elem = $(event.target) - DiscussionUtil.safeAjax - $elem: $elem - url: url - type: "POST" - success: (response, textStatus) => - @$el.remove() - if @model.get('type') == 'comment' - @model.get('thread').removeComment(@model) - - events: - "click .discussion-follow-thread": "toggleFollow" - "click .thread-title": "toggleSingleThread" - "click .discussion-show-comments": "toggleSingleThread" - "click .discussion-reply-thread": "reply" - "click .discussion-reply-comment": "reply" - "click .discussion-cancel-reply": "cancelReply" - "click .discussion-vote-up": "toggleVote" - "click .discussion-vote-down": "toggleVote" - "click .admin-endorse": "toggleEndorse" - "click .admin-openclose": "toggleClosed" - "click .admin-edit": "edit" - "click .admin-delete": "delete" - - initLocal: -> - @$local = @$el.children(".local") - @$delegateElement = @$local - - initTitle: -> - $contentTitle = @$(".thread-title") - if $contentTitle.length - $contentTitle.html DiscussionUtil.unescapeHighlightTag DiscussionUtil.stripLatexHighlight $contentTitle.html() - - initBody: -> - $contentBody = @$(".content-body") - $contentBody.html DiscussionUtil.postMathJaxProcessor DiscussionUtil.markdownWithHighlight $contentBody.html() - MathJax.Hub.Queue ["Typeset", MathJax.Hub, $contentBody.attr("id")] - - initTimeago: -> - @$("span.timeago").each (index, element) -> - elem = $(element) - elem.html("posted on #{$.timeago.parse(elem.html()).toLocaleString()}") - @$("span.timeago").timeago() - - renderPartial: -> - for attr, value of @model.changedAttributes() - if @partial[attr] - @partial[attr].apply(@, [value]) - - initBindings: -> - @model.view = @ - @model.bind('change', @renderPartial, @) - - initialize: -> - @initBindings() - @initLocal() - @initTimeago() - @initTitle() - @initBody() - @initCommentViews() - - reconstruct: -> - @initBindings() - @initLocal() - @initTimeago() - @initTitle() - @initBody() - @delegateEvents() - class @Thread extends @Content urlMappers: 'retrieve' : -> DiscussionUtil.urlFor('retrieve_single_thread', @discussion.id, @id) @@ -485,8 +129,6 @@ if Backbone? json_attributes = _.clone(@attributes) _.extend(json_attributes, { title: @display_title(), body: @display_body() }) - class @ThreadView extends @ContentView - class @Comment extends @Content urlMappers: 'reply': -> DiscussionUtil.urlFor('create_sub_comment', @id) @@ -503,8 +145,6 @@ if Backbone? count += comment.getCommentsCount() + 1 count - class @CommentView extends @ContentView - class @Comments extends Backbone.Collection model: Comment diff --git a/lms/static/coffee/src/discussion/discussion.coffee b/lms/static/coffee/src/discussion/discussion.coffee index e4db3c508c..bf51f354ff 100644 --- a/lms/static/coffee/src/discussion/discussion.coffee +++ b/lms/static/coffee/src/discussion/discussion.coffee @@ -37,172 +37,3 @@ if Backbone? thread1_count = parseInt(thread1.get("comments_count")) thread2_count = parseInt(thread2.get("comments_count")) thread2_count - thread1_count - - class @DiscussionView extends Backbone.View - - $: (selector) -> - @$local.find(selector) - - initLocal: -> - @$local = @$el.children(".local") - @$delegateElement = @$local - - initialize: -> - @initLocal() - @model.id = @$el.attr("_id") - @model.view = @ - @$el.children(".threads").children(".thread").each (index, elem) => - threadView = new ThreadView el: elem, model: @model.find $(elem).attr("_id") - if @$el.hasClass("forum-discussion") - $(".discussion-sidebar").find(".sidebar-new-post-button") - .unbind('click').click $.proxy @newPost, @ - else if @$el.hasClass("inline-discussion") - @newPost() - - reload: ($elem, url) -> - if not url then return - DiscussionUtil.safeAjax - $elem: $elem - $loading: $elem - loadingCallback: -> - $(this).parent().append("") - loadedCallback: -> - $(this).parent().children(".discussion-loading").remove() - url: url - type: "GET" - success: (response, textStatus) => - $parent = @$el.parent() - @$el.replaceWith(response.html) - $discussion = $parent.find("section.discussion") - @model.reset(response.discussion_data, { silent: false }) - view = new DiscussionView el: $discussion[0], model: @model - DiscussionUtil.bulkUpdateContentInfo(window.$$annotated_content_info) - $("html, body").animate({ scrollTop: 0 }, 0) - - loadSimilarPost: (event) -> - console.log "loading similar" - $title = @$(".new-post-title") - $wrapper = @$(".new-post-similar-posts-wrapper") - $similarPosts = @$(".new-post-similar-posts") - prevText = $title.attr("prev-text") - text = $title.val() - if text == prevText - if @$(".similar-post").length - $wrapper.show() - else if $.trim(text).length - $elem = $(event.target) - url = DiscussionUtil.urlFor 'search_similar_threads', @model.id - data = { text: @$(".new-post-title").val() } - DiscussionUtil.safeAjax - $elem: $elem - url: url - data: data - dataType: 'json' - success: (response, textStatus) => - $wrapper.html(response.html) - if $wrapper.find(".similar-post").length - $wrapper.show() - $wrapper.find(".hide-similar-posts").click => - $wrapper.hide() - else - $wrapper.hide() - $title.attr("prev-text", text) - - - newPost: -> - if not @$(".wmd-panel").length - view = { discussion_id: @model.id } - @$el.children(".discussion-non-content").append Mustache.render DiscussionUtil.getTemplate("_new_post"), view - $newPostBody = @$(".new-post-body") - DiscussionUtil.makeWmdEditor @$el, $.proxy(@$, @), "new-post-body" - - $input = DiscussionUtil.getWmdInput @$el, $.proxy(@$, @), "new-post-body" - $input.attr("placeholder", "post a new topic...") - if @$el.hasClass("inline-discussion") - $input.bind 'focus', (e) => - @$(".new-post-form").removeClass('collapsed') - else if @$el.hasClass("forum-discussion") - @$(".new-post-form").removeClass('collapsed') - - @$(".new-post-tags").tagsInput DiscussionUtil.tagsInputOptions() - - @$(".new-post-title").blur $.proxy(@loadSimilarPost, @) - - @$(".hide-similar-posts").click => - @$(".new-post-similar-posts-wrapper").hide() - - @$(".discussion-submit-post").click $.proxy(@submitNewPost, @) - @$(".discussion-cancel-post").click $.proxy(@cancelNewPost, @) - - - @$el.children(".blank").hide() - @$(".new-post-form").show() - - submitNewPost: (event) -> - title = @$(".new-post-title").val() - body = DiscussionUtil.getWmdContent @$el, $.proxy(@$, @), "new-post-body" - tags = @$(".new-post-tags").val() - anonymous = false || @$(".discussion-post-anonymously").is(":checked") - autowatch = false || @$(".discussion-auto-watch").is(":checked") - url = DiscussionUtil.urlFor('create_thread', @model.id) - 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: autowatch - 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() - - @$(".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("") - - thread = @model.addThread response.content - threadView = new ThreadView el: $thread[0], model: thread - thread.updateInfo response.annotated_content_info - @cancelNewPost() - - - cancelNewPost: (event) -> - if @$el.hasClass("inline-discussion") - @$(".new-post-form").addClass("collapsed") - else if @$el.hasClass("forum-discussion") - @$(".new-post-form").hide() - @$el.children(".blank").show() - - search: (event) -> - event.preventDefault() - $elem = $(event.target) - url = URI($elem.attr("action")).addSearch({text: @$(".search-input").val()}) - @reload($elem, url) - - sort: (event) -> - $elem = $(event.target) - url = $elem.attr("sort-url") - @reload($elem, url) - - page: (event) -> - $elem = $(event.target) - url = $elem.attr("page-url") - @reload($elem, url) - - events: - "submit .search-wrapper>.discussion-search-form": "search" - "click .discussion-search-link": "search" - "click .discussion-sort-link": "sort" - "click .discussion-page-link": "page"