From eed9956ff59d30265f7bbb087df48a5a14558839 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 10 Sep 2016 22:34:17 -0400 Subject: [PATCH] initListSectionItemListeners --- src/popup/app/vault/vaultAddSiteController.js | 9 +--- src/popup/index.html | 1 + src/popup/scripts/popup.js | 54 +++++++++++++++++++ 3 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 src/popup/scripts/popup.js diff --git a/src/popup/app/vault/vaultAddSiteController.js b/src/popup/app/vault/vaultAddSiteController.js index daf061e008..b1a98219c3 100644 --- a/src/popup/app/vault/vaultAddSiteController.js +++ b/src/popup/app/vault/vaultAddSiteController.js @@ -7,14 +7,7 @@ }; $('#name').focus(); - $('.list-section-item').click(function (e) { - e.preventDefault(); - $(this).find('input[type="text"], textarea, select').focus(); - var checkbox = $(this).find('input[type="checkbox"]'); - if (checkbox.length > 0) { - checkbox.prop('checked', !checkbox.is(':checked')); - } - }); + popupUtils.initListSectionItemListeners(); $scope.savePromise = null; $scope.save = function (model) { diff --git a/src/popup/index.html b/src/popup/index.html index 2d1d7113e8..77d8ffaf26 100644 --- a/src/popup/index.html +++ b/src/popup/index.html @@ -11,6 +11,7 @@ + diff --git a/src/popup/scripts/popup.js b/src/popup/scripts/popup.js new file mode 100644 index 0000000000..b22303e5b1 --- /dev/null +++ b/src/popup/scripts/popup.js @@ -0,0 +1,54 @@ +var popupUtils = function () { + var self = this; + + self.initListSectionItemListeners = function () { + $('.list-section-item').click(function (e) { + var text = $(this).find('input, textarea').not('input[type="checkbox"], input[type="radio"], input[type="hidden"]'); + var checkbox = $(this).find('input[type="checkbox"]'); + var select = $(this).find('select'); + + if (text.length > 0 && e.target === text[0]) { + return; + } + if (checkbox.length > 0 && e.target === checkbox[0]) { + return; + } + if (select.length > 0 && e.target === select[0]) { + return; + } + + e.preventDefault(); + + if (text.length > 0) { + text.focus(); + } + else if (checkbox.length > 0) { + checkbox.prop('checked', !checkbox.is(':checked')); + } + else if (select.length > 0) { + select.openSelect(); + } + }); + } + + return self; +}(); + + +// ref: http://stackoverflow.com/questions/19432610/jquery-open-select-by-button +(function ($) { + "use strict"; + $.fn.openSelect = function () { + return this.each(function (index, el) { + $(el).focus(); + if (document.createEvent) { + var event = document.createEvent("MouseEvents"); + event.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); + el.dispatchEvent(event); + } + else if (element.fireEvent) { + el.fireEvent("onmousedown"); + } + }); + } +}(jQuery));