From ad1a36c0ee32ebf8bf8414089387f953e7d55ea5 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Fri, 31 May 2013 04:28:26 -1000 Subject: [PATCH] Changed new directives over to user proper function declaration with code comments. Added new tree data resource, using promise api to return the data (handy!) --- .../common/directives/leftcolumn.directive.js | 2 +- .../common/directives/login.directive.js | 2 +- .../directives/notifications.directive.js | 2 +- .../common/resources/tree.resource.js | 37 ++++++++++++++ .../umbraco/js/umbraco.directives.js | 6 +-- .../umbraco/js/umbraco.resources.js | 33 +++++++++++++ .../umbraco/js/umbraco.services.js | 15 +++++- .../Trees/ApplicationTreeApiController.cs | 49 ++++++++++--------- 8 files changed, 116 insertions(+), 30 deletions(-) create mode 100644 src/Umbraco.Web.UI.Client/common/resources/tree.resource.js diff --git a/src/Umbraco.Web.UI.Client/common/directives/leftcolumn.directive.js b/src/Umbraco.Web.UI.Client/common/directives/leftcolumn.directive.js index 68a4da58ab..a48ae6e594 100644 --- a/src/Umbraco.Web.UI.Client/common/directives/leftcolumn.directive.js +++ b/src/Umbraco.Web.UI.Client/common/directives/leftcolumn.directive.js @@ -7,7 +7,7 @@ define(['angular'], function (angular) { * @name umbraco.directive:leftColumn * @restrict E **/ - var leftColumnDirective = function () { + function leftColumnDirective() { return { restrict: "E", // restrict to an element replace: true, // replace the html element with the template diff --git a/src/Umbraco.Web.UI.Client/common/directives/login.directive.js b/src/Umbraco.Web.UI.Client/common/directives/login.directive.js index 334d40d762..cb37dd9c54 100644 --- a/src/Umbraco.Web.UI.Client/common/directives/login.directive.js +++ b/src/Umbraco.Web.UI.Client/common/directives/login.directive.js @@ -7,7 +7,7 @@ define(['angular'], function (angular) { * @name umbraco.directive:login * @restrict E **/ - var loginDirective = function () { + function loginDirective() { return { restrict: "E", // restrict to an element replace: true, // replace the html element with the template diff --git a/src/Umbraco.Web.UI.Client/common/directives/notifications.directive.js b/src/Umbraco.Web.UI.Client/common/directives/notifications.directive.js index 04b762bcb6..78114f7a79 100644 --- a/src/Umbraco.Web.UI.Client/common/directives/notifications.directive.js +++ b/src/Umbraco.Web.UI.Client/common/directives/notifications.directive.js @@ -7,7 +7,7 @@ define(['angular'], function (angular) { * @name umbraco.directive:notifications * @restrict E **/ - var notificationDirective = function () { + function notificationDirective() { return { restrict: "E", // restrict to an element replace: true, // replace the html element with the template diff --git a/src/Umbraco.Web.UI.Client/common/resources/tree.resource.js b/src/Umbraco.Web.UI.Client/common/resources/tree.resource.js new file mode 100644 index 0000000000..672f052666 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/common/resources/tree.resource.js @@ -0,0 +1,37 @@ +'use strict'; + +define(['angular'], function(angular) { + + /** + * @ngdoc factory + * @name umbraco.resources.trees.umbTreeResource + **/ + function umbTreeResource($q, $http) { + + //internal method to get the tree app url + function getTreeAppUrl(section) { + return Umbraco.Sys.ServerVariables.treeApplicationApiBaseUrl + "GetApplicationTrees?application=" + section; + } + + //the factory object returned + return { + loadApplication: function (section) { + + var deferred = $q.defer(); + + //go and get the tree data + $http.get(getTreeAppUrl(section)). + success(function (data, status, headers, config) { + deferred.resolve(data); + }). + error(function (data, status, headers, config) { + deferred.reject('Failed to retreive data for application tree ' + section); + }); + + return deferred.promise; + } + }; + } + + angular.module('umbraco.resources.trees', []).factory('umbTreeResource', umbTreeResource); +}); \ No newline at end of file diff --git a/src/Umbraco.Web.UI/umbraco/js/umbraco.directives.js b/src/Umbraco.Web.UI/umbraco/js/umbraco.directives.js index d716e2c872..e09efde460 100644 --- a/src/Umbraco.Web.UI/umbraco/js/umbraco.directives.js +++ b/src/Umbraco.Web.UI/umbraco/js/umbraco.directives.js @@ -11,7 +11,7 @@ define(['angular'], function (angular) { * @name umbraco.directive:leftColumn * @restrict E **/ - var leftColumnDirective = function () { + function leftColumnDirective() { return { restrict: "E", // restrict to an element replace: true, // replace the html element with the template @@ -29,7 +29,7 @@ define(['angular'], function (angular) { * @name umbraco.directive:login * @restrict E **/ - var loginDirective = function () { + function loginDirective() { return { restrict: "E", // restrict to an element replace: true, // replace the html element with the template @@ -47,7 +47,7 @@ define(['angular'], function (angular) { * @name umbraco.directive:notifications * @restrict E **/ - var notificationDirective = function () { + function notificationDirective() { return { restrict: "E", // restrict to an element replace: true, // replace the html element with the template diff --git a/src/Umbraco.Web.UI/umbraco/js/umbraco.resources.js b/src/Umbraco.Web.UI/umbraco/js/umbraco.resources.js index 25eddcf48a..682ce8695e 100644 --- a/src/Umbraco.Web.UI/umbraco/js/umbraco.resources.js +++ b/src/Umbraco.Web.UI/umbraco/js/umbraco.resources.js @@ -5,6 +5,39 @@ */ 'use strict'; define(['angular'], function (angular) { + + /** + * @ngdoc factory + * @name umbraco.resources.trees.umbTreeResource +**/ + function umbTreeResource($q, $http) { + + //internal method to get the tree app url + function getTreeAppUrl(section) { + return Umbraco.Sys.ServerVariables.treeApplicationApiBaseUrl + "GetApplicationTrees?application=" + section; + } + + //the factory object returned + return { + loadApplication: function (section) { + + var deferred = $q.defer(); + + //go and get the tree data + $http.get(getTreeAppUrl(section)). + success(function (data, status, headers, config) { + deferred.resolve(data); + }). + error(function (data, status, headers, config) { + deferred.reject('Failed to retreive data for application tree ' + section); + }); + + return deferred.promise; + } + }; + } + angular.module('umbraco.resources.trees', []).factory('umbTreeResource', umbTreeResource); + angular.module('umbraco.resources.content', []) .factory('contentFactory', function () { diff --git a/src/Umbraco.Web.UI/umbraco/js/umbraco.services.js b/src/Umbraco.Web.UI/umbraco/js/umbraco.services.js index 2595095c98..e2ae42b175 100644 --- a/src/Umbraco.Web.UI/umbraco/js/umbraco.services.js +++ b/src/Umbraco.Web.UI/umbraco/js/umbraco.services.js @@ -244,8 +244,9 @@ angular.module('umbraco.services.section', []) }; }); -angular.module('umbraco.services.tree', []) -.factory('tree', function () { + +angular.module('umbraco.services.tree', ["umbraco.resources.trees"]) +.factory('tree', function ($http, umbTreeResource) { //implement this in local storage var treeArray = []; var currentSection = "content"; @@ -253,6 +254,16 @@ angular.module('umbraco.services.tree', []) return { getTree: function (section) { + umbTreeResource.loadApplication(section) + .then(function (data) { + + //this will be called once the tree app data has loaded + alert("woot!"); + + }, function (reason) { + alert('Failed: ' + reason); + }); + if (treeArray[section] !== undefined){ return treeArray[section]; } diff --git a/src/Umbraco.Web/Trees/ApplicationTreeApiController.cs b/src/Umbraco.Web/Trees/ApplicationTreeApiController.cs index 96991e6914..41c7742bcb 100644 --- a/src/Umbraco.Web/Trees/ApplicationTreeApiController.cs +++ b/src/Umbraco.Web/Trees/ApplicationTreeApiController.cs @@ -29,28 +29,34 @@ namespace Umbraco.Web.Trees controllerContext.Configuration.Formatters.Remove(controllerContext.Configuration.Formatters.XmlFormatter); } - ///// - ///// Returns the tree nodes for an application - ///// - ///// - ///// - ///// - //[HttpQueryStringFilter("queryStrings")] - //public TreeNodeCollection GetApplicationTrees(string application, FormDataCollection queryStrings) - //{ - // if (application == null) throw new ArgumentNullException("application"); - - // //find all tree definitions that have the current application alias - // var appTrees = ApplicationTree.getApplicationTree(application); - // if (appTrees.Count() == 1) - // { - // return GetNodeCollection(appTrees.Single(), "-1", queryStrings); - // } - // foreach (var tree in appTrees) - // { + /// + /// Returns the tree nodes for an application + /// + /// + /// + /// + [HttpQueryStringFilter("queryStrings")] + public TreeNodeCollection GetApplicationTrees(string application, FormDataCollection queryStrings) + { + if (application == null) throw new ArgumentNullException("application"); + + //find all tree definitions that have the current application alias + var appTrees = ApplicationTree.getApplicationTree(application).Where(x => x.Initialize).ToArray(); + if (appTrees.Count() == 1) + { + //return the nodes for the one tree assigned + return GetNodeCollection(appTrees.Single(), "-1", queryStrings); + } + + var collection = new TreeNodeCollection(); + foreach (var tree in appTrees) + { + //return the root nodes for each tree in the app + //collection.Add(); //GetNodeCollection(tree, "-1", queryStrings); - // } - //} + } + return null; + } /// /// Returns the tree data for a specific tree for the children of the id @@ -114,7 +120,6 @@ namespace Umbraco.Web.Trees var instance = (TreeApiController)DependencyResolver.Current.GetService(foundControllerTree); instance.ControllerContext = ControllerContext; instance.Request = Request; - //return it's data return new Attempt(true, instance.GetNodes(id, formCollection)); }