From 80ceb1a46da54359cfc18eb46a744dad5c8d481c Mon Sep 17 00:00:00 2001 From: Yashashvi Dave Date: Wed, 8 May 2019 12:56:27 +0530 Subject: [PATCH] components: Add `disable_tab` attribute. --- frontend_tests/node_tests/components.js | 15 +++++++++++++++ static/js/components.js | 14 +++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/frontend_tests/node_tests/components.js b/frontend_tests/node_tests/components.js index 76c7b32616..ee96497e06 100644 --- a/frontend_tests/node_tests/components.js +++ b/frontend_tests/node_tests/components.js @@ -34,6 +34,11 @@ run_test('basics', () => { self.class = _.without(tokens, c).join(' '); }; + self.hasClass = function (c) { + var tokens = self.class.trim().split(/ +/); + return tokens.includes(c); + }; + self.data = function (name) { assert.equal(name, 'tab-id'); return i; @@ -197,4 +202,14 @@ run_test('basics', () => { click_f.call(tabs[1]); assert.equal(widget.value(), 'translated: Message formatting'); + + callback_args = undefined; + widget.disable_tab("search-operators"); + assert.equal(tabs[2].hasClass('disabled'), true); + assert.equal(tabs[2].class, "last disabled"); + + widget.goto('keyboard-shortcuts'); + assert.equal(focused_tab, 0); + widget.goto("search-operators"); + assert.equal(focused_tab, 0); }); diff --git a/static/js/components.js b/static/js/components.js index aeef10f27c..64e9a3d340 100644 --- a/static/js/components.js +++ b/static/js/components.js @@ -42,9 +42,12 @@ exports.toggle = function (opts) { }; function select_tab(idx) { + var elem = meta.$ind_tab.eq(idx); + if (elem.hasClass('disabled')) { + return; + } meta.$ind_tab.removeClass("selected"); - var elem = meta.$ind_tab.eq(idx); elem.addClass("selected"); meta.idx = idx; @@ -98,6 +101,15 @@ exports.toggle = function (opts) { maybe_go_left: maybe_go_left, maybe_go_right: maybe_go_right, + disable_tab: function (name) { + var value = _.find(opts.values, function (o) { + return o.key === name; + }); + + var idx = opts.values.indexOf(value); + meta.$ind_tab.eq(idx).addClass('disabled'); + }, + value: function () { if (meta.idx >= 0) { return opts.values[meta.idx].label;