From ccf4b9ec69c3a9d33cc02b5cf01f2ff0a866efdd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= <danxuliu@gmail.com>
Date: Fri, 9 Jun 2017 03:34:56 +0200
Subject: [PATCH] Add visibility related methods
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

SystemTagsInfoView now provides public methods related to its visibility
in preparation to be used by external objects.

Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
---
 apps/systemtags/js/systemtagsinfoview.js      | 30 +++++++++---
 .../tests/js/systemtagsinfoviewSpec.js        | 46 +++++++++++++++++++
 2 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/apps/systemtags/js/systemtagsinfoview.js b/apps/systemtags/js/systemtagsinfoview.js
index ce5de2048ab..d42bf18761b 100644
--- a/apps/systemtags/js/systemtagsinfoview.js
+++ b/apps/systemtags/js/systemtagsinfoview.js
@@ -65,9 +65,11 @@
 			this._toggleHandle.prepend($('<span>').addClass('icon icon-tag'));
 
 			this._toggleHandle.on('click', function () {
-				self.$el.toggleClass('hidden');
-				if (!self.$el.hasClass('hidden')) {
-					self.$el.find('.systemTagsInputField').select2('open');
+				if (self.isVisible()) {
+					self.hide();
+				} else {
+					self.show();
+					self.openDropdown();
 				}
 			});
 		},
@@ -144,15 +146,15 @@
 						self._inputView.setData(appliedTags);
 
 						if (appliedTags.length !== 0) {
-							self.$el.removeClass('hidden');
+							self.show();
 						} else {
-							self.$el.addClass('hidden');
+							self.hide();
 						}
 					}
 				});
 			}
 
-			this.$el.addClass('hidden');
+			this.hide();
 		},
 
 		/**
@@ -165,6 +167,22 @@
 			this._inputView.render();
 		},
 
+		isVisible: function() {
+			return !this.$el.hasClass('hidden');
+		},
+
+		show: function() {
+			this.$el.removeClass('hidden');
+		},
+
+		hide: function() {
+			this.$el.addClass('hidden');
+		},
+
+		openDropdown: function() {
+			this.$el.find('.systemTagsInputField').select2('open');
+		},
+
 		remove: function() {
 			this._inputView.remove();
 			this._toggleHandle.remove();
diff --git a/apps/systemtags/tests/js/systemtagsinfoviewSpec.js b/apps/systemtags/tests/js/systemtagsinfoviewSpec.js
index 449dfd859d7..2f874688112 100644
--- a/apps/systemtags/tests/js/systemtagsinfoviewSpec.js
+++ b/apps/systemtags/tests/js/systemtagsinfoviewSpec.js
@@ -201,4 +201,50 @@ describe('OCA.SystemTags.SystemTagsInfoView tests', function() {
 
 		});
 	});
+	describe('visibility', function() {
+		it('reports visibility based on the "hidden" class name', function() {
+			view.$el.addClass('hidden');
+
+			expect(view.isVisible()).toBeFalsy();
+
+			view.$el.removeClass('hidden');
+
+			expect(view.isVisible()).toBeTruthy();
+		});
+		it('is not visible after rendering', function() {
+			view.render();
+
+			expect(view.isVisible()).toBeFalsy();
+		});
+		it('shows and hides the element', function() {
+			view.show();
+
+			expect(view.isVisible()).toBeTruthy();
+
+			view.hide();
+
+			expect(view.isVisible()).toBeFalsy();
+
+			view.show();
+
+			expect(view.isVisible()).toBeTruthy();
+		});
+	});
+	describe('select2', function() {
+		var select2Stub;
+
+		beforeEach(function() {
+			select2Stub = sinon.stub($.fn, 'select2');
+		});
+		afterEach(function() {
+			select2Stub.restore();
+		});
+		it('opens dropdown', function() {
+			view.openDropdown();
+
+			expect(select2Stub.calledOnce).toBeTruthy();
+			expect(select2Stub.thisValues[0].selector).toEqual('.systemTagsInputField');
+			expect(select2Stub.withArgs('open')).toBeTruthy();
+		});
+	});
 });
-- 
GitLab