From 0407bec67b66685d649cb1d78d31a961d6a8a5dd Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 10 Oct 2014 01:05:48 +1100 Subject: [PATCH] Moves more logic into directives --- .../directives/umbtreesearchbox.directive.js | 30 +++++++++++- .../common/dialogs/treepicker.controller.js | 47 +++++-------------- .../src/views/common/dialogs/treepicker.html | 5 +- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/directives/umbtreesearchbox.directive.js b/src/Umbraco.Web.UI.Client/src/common/directives/umbtreesearchbox.directive.js index 7729bdc285..2743cf136c 100644 --- a/src/Umbraco.Web.UI.Client/src/common/directives/umbtreesearchbox.directive.js +++ b/src/Umbraco.Web.UI.Client/src/common/directives/umbtreesearchbox.directive.js @@ -5,12 +5,13 @@ * @element ANY * @restrict E **/ -function treeSearchBox(localizationService) { +function treeSearchBox(localizationService, searchService) { return { scope: { searchFrom: "@", searchFromName: "@", showSearch: "@", + section: "@", hideSearchCallback: "=", searchCallback: "=" }, @@ -33,14 +34,39 @@ function treeSearchBox(localizationService) { scope.showSearch = "false"; } + function performSearch() { + if (scope.term) { + scope.results = []; + + var searchArgs = { + term: scope.term + }; + //append a start node context if there is one + if (scope.searchFrom) { + searchArgs["searchFrom"] = scope.searchFrom; + } + searcher(searchArgs).then(function (data) { + scope.searchCallback(data); + }); + } + } + scope.$watch("term", _.debounce(function(newVal, oldVal) { scope.$apply(function() { if (newVal !== null && newVal !== undefined && newVal !== oldVal) { - scope.searchCallback(newVal); + performSearch(); } }); }, 200)); + var searcher = searchService.searchContent; + //search + if (scope.section === "member") { + searcher = searchService.searchMembers; + } + else if (scope.section === "media") { + searcher = searchService.searchMedia; + } } }; } diff --git a/src/Umbraco.Web.UI.Client/src/views/common/dialogs/treepicker.controller.js b/src/Umbraco.Web.UI.Client/src/views/common/dialogs/treepicker.controller.js index 05bf0007a9..280ee0c132 100644 --- a/src/Umbraco.Web.UI.Client/src/views/common/dialogs/treepicker.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/common/dialogs/treepicker.controller.js @@ -10,7 +10,7 @@ angular.module("umbraco").controller("Umbraco.Dialogs.TreePickerController", $scope.multiPicker = dialogOptions.multiPicker; $scope.hideHeader = true; $scope.searchInfo = { - searchFrom: null, + searchFrom: dialogOptions.startNodeId, searchFromName: null, showSearch: false, results: [] @@ -20,8 +20,6 @@ angular.module("umbraco").controller("Umbraco.Dialogs.TreePickerController", $scope.customTreeParams = dialogOptions.startNodeId ? "startNodeId=" + dialogOptions.startNodeId : ""; $scope.customTreeParams += dialogOptions.customTreeParams ? "&" + dialogOptions.customTreeParams : ""; - //search defaults - var searcher = searchService.searchContent; var entityType = "Document"; @@ -33,13 +31,10 @@ angular.module("umbraco").controller("Umbraco.Dialogs.TreePickerController", dialogOptions.maxNumber = parseInt(dialogOptions.maxNumber, 10); } - //search if (dialogOptions.section === "member") { - searcher = searchService.searchMembers; entityType = "Member"; } - else if (dialogOptions.section === "media") { - searcher = searchService.searchMedia; + else if (dialogOptions.section === "media") { entityType = "Media"; } @@ -223,40 +218,24 @@ angular.module("umbraco").controller("Umbraco.Dialogs.TreePickerController", $scope.searchInfo.showSearch = false; - $scope.searchInfo.searchFromName = null; + $scope.searchInfo.searchFromName = dialogOptions.startNodeId; $scope.searchInfo.searchFrom = null; $scope.searchInfo.results = []; } - //handles the on key up for searching, but we don't want to over query so the result is debounced - $scope.performSearch = function(term) { - if (term) { - $scope.searchInfo.results = []; - - var searchArgs = { - term: term - }; - //append a start node id, whether it's a global one, or based on a selected list view - if ($scope.searchInfo.searchFrom || dialogOptions.startNodeId) { - searchArgs["searchFrom"] = $scope.searchInfo.searchFrom ? $scope.searchInfo.searchFrom : dialogOptions.startNodeId; - } - searcher(searchArgs).then(function (data) { - $scope.searchInfo.results = data; - //now we need to look in the already selected search results and - // toggle the check boxes for those ones - _.each($scope.searchInfo.results, function (result) { - var exists = _.find($scope.dialogData.selection, function (selectedId) { - return result.id == selectedId; - }); - if (exists) { - result.selected = true; - } - }); + $scope.onSearchResults = function(results) { + $scope.searchInfo.results = results; + _.each($scope.searchInfo.results, function (result) { + var exists = _.find($scope.dialogData.selection, function (selectedId) { + return result.id == selectedId; }); + if (exists) { + result.selected = true; + } + }); - $scope.searchInfo.showSearch = true; - } + $scope.searchInfo.showSearch = true; }; $scope.dialogTreeEventHandler.bind("treeLoaded", treeLoadedHandler); diff --git a/src/Umbraco.Web.UI.Client/src/views/common/dialogs/treepicker.html b/src/Umbraco.Web.UI.Client/src/views/common/dialogs/treepicker.html index 67becc362e..3d2772abcc 100644 --- a/src/Umbraco.Web.UI.Client/src/views/common/dialogs/treepicker.html +++ b/src/Umbraco.Web.UI.Client/src/views/common/dialogs/treepicker.html @@ -3,10 +3,11 @@
+ show-search="{{searchInfo.showSearch}}" + section="{{section}}">