diff --git a/common/static/coffee/spec/discussion/view/discussion_topic_menu_view_spec.js b/common/static/coffee/spec/discussion/view/discussion_topic_menu_view_spec.js index 2092a52854..752a4e3603 100644 --- a/common/static/coffee/spec/discussion/view/discussion_topic_menu_view_spec.js +++ b/common/static/coffee/spec/discussion/view/discussion_topic_menu_view_spec.js @@ -9,7 +9,7 @@ }, options); this.view = new DiscussionTopicMenuView(options); this.view.render().appendTo('#fixture-element'); - this.defaultTextWidth = this.view.getNameWidth(this.completeText); + this.defaultTextWidth = this.completeText.length; }; this.openMenu = function () { @@ -67,31 +67,10 @@ expect(this.completeText).toEqual(dropdownText); }); - it('completely show just sub-category', function() { - var dropdownText; - this.createTopicView(); - this.view.maxNameWidth = this.defaultTextWidth - 10; - this.view.$el.find('a.topic-title').first().click(); - dropdownText = this.view.$el.find('.js-selected-topic').text(); - expect(dropdownText.indexOf('…')).toEqual(0); - expect(dropdownText).toContain(this.selectedOptionText); - }); - - it('partially show sub-category', function() { - this.createTopicView(); - var parentWidth = this.view.getNameWidth(this.parentCategoryText), - dropdownText; - this.view.maxNameWidth = this.defaultTextWidth - parentWidth; - this.view.$el.find('a.topic-title').first().click(); - dropdownText = this.view.$el.find('.js-selected-topic').text(); - expect(dropdownText.indexOf('…')).toEqual(0); - expect(dropdownText.lastIndexOf('…')).toBeGreaterThan(0); - }); - it('broken span doesn\'t occur', function() { var dropdownText; this.createTopicView(); - this.view.maxNameWidth = this.view.getNameWidth(this.selectedOptionText) + 100; + this.view.maxNameWidth = this.selectedOptionText.length + 100; this.view.$el.find('a.topic-title').first().click(); dropdownText = this.view.$el.find('.js-selected-topic').text(); expect(dropdownText.indexOf('/ span>')).toEqual(-1); diff --git a/common/static/coffee/src/discussion/views/discussion_topic_menu_view.js b/common/static/coffee/src/discussion/views/discussion_topic_menu_view.js index b7350fb0f7..d08ff0a92e 100644 --- a/common/static/coffee/src/discussion/views/discussion_topic_menu_view.js +++ b/common/static/coffee/src/discussion/views/discussion_topic_menu_view.js @@ -16,7 +16,7 @@ initialize: function(options) { this.course_settings = options.course_settings; this.currentTopicId = options.topicId; - this.maxNameWidth = 100; + this.maxNameWidth = 26; _.bindAll(this, 'toggleTopicDropdown', 'handleTopicEvent', 'hideTopicDropdown', 'ignoreClick' ); @@ -88,9 +88,6 @@ this.dropdownButton.addClass('dropped'); this.topicMenu.show(); $(document.body).on('click.topicMenu', this.hideTopicDropdown); - // 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 - this.maxNameWidth = this.dropdownButton.width() - 40; return this; }, @@ -146,29 +143,12 @@ } }, - // @TODO move into utils.coffee - getNameWidth: function(name) { - var test = $('
'), - width; - - test.css({ - 'font-size': this.dropdownButton.css('font-size'), - 'opacity': 0, - 'position': 'absolute', - 'left': -1000, - 'top': -1000 - }).html(name).appendTo(document.body); - width = test.width(); - test.remove(); - return width; - }, - // @TODO move into utils.coffee fitName: function(name) { var ellipsisText = gettext('…'), partialName, path, rawName; - if (this.getNameWidth(name) < this.maxNameWidth) { + if (name.length < this.maxNameWidth) { return name; } else { path = _.map(name.split('/'), function(item){ @@ -176,15 +156,15 @@ }); while (path.length > 1) { path.shift(); - partialName = ellipsisText + ' / ' + path.join(' / '); - if (this.getNameWidth(partialName) < this.maxNameWidth) { - return partialName; + partialName = ellipsisText + '/' + path.join('/'); + if (partialName.length > this.maxNameWidth) { + return partialName; } } rawName = path[0]; name = ellipsisText + ' / ' + rawName; - while (this.getNameWidth(name) > this.maxNameWidth) { - rawName = rawName.slice(0, -1); + while (name.length > this.maxNameWidth) { + rawName = rawName.slice(0, this.maxNameWidth); name = ellipsisText + ' / ' + rawName + ' ' + ellipsisText; } }