From 835e2f8ed7b28991f2322c6d1ff13cd20a55a351 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sat, 24 May 2014 16:29:49 -0300 Subject: [PATCH 01/20] Refactored app.js to be easier to read and alter. --- applications/default/public/js/app.js | 33 +++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/applications/default/public/js/app.js b/applications/default/public/js/app.js index 2e7ae9e..4a93cf8 100644 --- a/applications/default/public/js/app.js +++ b/applications/default/public/js/app.js @@ -1,7 +1,30 @@ +/* + * Main choko application definition file. + */ + 'use strict'; -// Declare app level module which depends on services, directives and filters. -angular.module('choko', ['ngRoute', 'ngResource', 'ngSanitize', 'summernote', 'angularFileUpload', 'choko.services', 'choko.directives', 'choko.filters']) -.config(['$locationProvider', function($locationProvider) { - //$locationProvider.html5Mode(true); -}]); +// Define core choko dependencies. +var dependencies = [ + 'ngRoute', + 'ngResource', + 'ngSanitize', + 'summernote', + 'angularFileUpload', + 'choko.services', + 'choko.directives', + 'choko.filters' +]; + +// Declare main choko module. +angular.module('choko', dependencies) + + /** + * Main configuration. + */ + .config(['$locationProvider', function($locationProvider) { + + // Use HTML5 mode to remove "#" symbols from angular-routed pages. + // $locationProvider.html5Mode(true); + + }]); From d1dac37e6c76a5316c17788fa859fd3f91714b66 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sat, 24 May 2014 23:25:59 -0300 Subject: [PATCH 02/20] Refactored directives.js to comment out the code. --- applications/default/public/js/app.js | 8 +- applications/default/public/js/directives.js | 88 ++++++++++++++++---- 2 files changed, 77 insertions(+), 19 deletions(-) diff --git a/applications/default/public/js/app.js b/applications/default/public/js/app.js index 4a93cf8..ea5c064 100644 --- a/applications/default/public/js/app.js +++ b/applications/default/public/js/app.js @@ -1,5 +1,5 @@ -/* - * Main choko application definition file. +/** + * @file Main AngularJS module for the choko application. */ 'use strict'; @@ -12,7 +12,6 @@ var dependencies = [ 'summernote', 'angularFileUpload', 'choko.services', - 'choko.directives', 'choko.filters' ]; @@ -20,7 +19,8 @@ var dependencies = [ angular.module('choko', dependencies) /** - * Main configuration. + * Configures the location provider. + * @param {object} $locationProvider */ .config(['$locationProvider', function($locationProvider) { diff --git a/applications/default/public/js/directives.js b/applications/default/public/js/directives.js index bfde69b..9c71ddb 100644 --- a/applications/default/public/js/directives.js +++ b/applications/default/public/js/directives.js @@ -1,42 +1,100 @@ -'use strict'; +/** + * @file Choko directives. + */ -/* Directives */ +'use strict'; -angular.module('choko.directives', []) +// Append directives to main choko module. +angular.module('choko') + + /** + * Directive to return the application's version. + * @param {string} version + */ .directive('appVersion', ['version', function(version) { return function(scope, elm, attrs) { elm.text(version); }; }]) - .directive('ckReplace', function($http, $compile) { + + /** + * Replaces any tag with overridable templates from the server. + * @param {object} $http + * @param {object} $compile + */ + .directive('ckReplace', function($http, $compile, $injector) { return { restrict: 'E', scope: true, compile: function(element, attrs) { return function(scope, element, attrs) { + + // Allow for custom templates but fallback to default one based + // on element type. scope.element.template = scope.element.template || 'templates/' + scope.element.type + '.html'; - $http({method: 'GET', url: scope.element.template, cache: true}).then(function(result) { - var template = angular.element($compile(result.data)(scope)); - element.replaceWith(template); - }); + + // Retrive and compile element template. + compileAndReplace(scope, element, scope.element.template); }; } }; }) - .directive('ckButton', function($http, $compile) { + + /** + * [description] + * @param {[type]} $http + * @param {[type]} $compile + * @return {[type]} + */ + .directive('ckButton', function($http, $compile, $injector) { return { restrict: 'E', scope: true, compile: function(element, attrs) { return function(scope, element, attrs) { - var template = scope.item.items ? '/templates/btn-group-dropdown.html' : '/templates/btn-group-button.html'; + + // Add bootstrap button class. scope.item.classes = scope.item.classes || ['btn-default']; - $http({method: 'GET', url: template, cache: true}).then(function(result) { - var template = angular.element($compile(result.data)(scope)); - element.replaceWith(template); - }); + // @TODO: we should probably allow for custom templates, as we do in + // cdReplace directive above. + var template = scope.item.items ? '/templates/btn-group-dropdown.html' : '/templates/btn-group-button.html'; + + // Retrive and compile element template. + compileAndReplace(scope, element, template); }; } }; - }); \ No newline at end of file + }); + +/** + * Helper function to retrive a template for a element, compile it and + * replace the element with it's newly compiled one. + * @param {object} scope + * @param {object} element + * @param {string} templateUrl + */ +function compileAndReplace(scope, element, templateUrl) { + + var $injector = element.injector(); + var $http = $injector.get('$http'); + var $compile = $injector.get('$compile'); + + // Request the template content. + var loadTemplate = $http({ + method: 'GET', + url: templateUrl, + cache: true + }); + + // When ready, compile the retrieved template. + loadTemplate.then(function(result) { + + // Compile the returned template. + var compiled = $compile(result.data)(scope); + + // Replace old element with compiled one. + element.replaceWith(angular.element(compiled)); + + }); +} \ No newline at end of file From 53da962b985b9609351bec7cb1bae72c1dc53d13 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sat, 24 May 2014 23:37:10 -0300 Subject: [PATCH 03/20] Moved application version value definition from the services.js to the app.js. Even though value definitions are indeed services, the application's version definition should reside in it's main file. Better then that, they should be read from a JSON file, with even more properies available. --- applications/default/public/js/app.js | 3 +++ applications/default/public/js/services.js | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/applications/default/public/js/app.js b/applications/default/public/js/app.js index ea5c064..4069002 100644 --- a/applications/default/public/js/app.js +++ b/applications/default/public/js/app.js @@ -18,6 +18,9 @@ var dependencies = [ // Declare main choko module. angular.module('choko', dependencies) + // Define current choko version. + .value('version', '0.0.1') + /** * Configures the location provider. * @param {object} $locationProvider diff --git a/applications/default/public/js/services.js b/applications/default/public/js/services.js index c5a0941..5548194 100644 --- a/applications/default/public/js/services.js +++ b/applications/default/public/js/services.js @@ -2,8 +2,6 @@ /* Services */ angular.module('choko.services', []) - // Single value service for Choko version. - .value('version', '0.0.1') .factory('Choko', function($resource) { return $resource('/rest/:type/:key', { From 67c838ff19fcd66739a452a0c0eab1b35b5567f4 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sun, 25 May 2014 00:43:52 -0300 Subject: [PATCH 04/20] Updated application version to match the one found on package.json. --- applications/default/public/js/app.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/applications/default/public/js/app.js b/applications/default/public/js/app.js index 4069002..53cb814 100644 --- a/applications/default/public/js/app.js +++ b/applications/default/public/js/app.js @@ -18,8 +18,10 @@ var dependencies = [ // Declare main choko module. angular.module('choko', dependencies) - // Define current choko version. - .value('version', '0.0.1') + // Define current choko version. + // @TODO: we should read package.json and make available not only a version + // value but other metadata that might be used thoughout the application. + .value('version', '0.0.4') /** * Configures the location provider. From 8a89ebb8f52c8033eab348e93e29de926fd4c184 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sun, 25 May 2014 00:47:11 -0300 Subject: [PATCH 05/20] Fixed some comments. --- applications/default/public/js/app.js | 2 +- applications/default/public/js/directives.js | 17 +++++++++-------- applications/default/public/js/services.js | 4 ++++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/applications/default/public/js/app.js b/applications/default/public/js/app.js index 53cb814..b7144fc 100644 --- a/applications/default/public/js/app.js +++ b/applications/default/public/js/app.js @@ -19,7 +19,7 @@ var dependencies = [ angular.module('choko', dependencies) // Define current choko version. - // @TODO: we should read package.json and make available not only a version + // @todo: we should read package.json and make available not only a version // value but other metadata that might be used thoughout the application. .value('version', '0.0.4') diff --git a/applications/default/public/js/directives.js b/applications/default/public/js/directives.js index 9c71ddb..21ae347 100644 --- a/applications/default/public/js/directives.js +++ b/applications/default/public/js/directives.js @@ -1,5 +1,5 @@ /** - * @file Choko directives. + * @file Choko core directives. */ 'use strict'; @@ -22,7 +22,7 @@ angular.module('choko') * @param {object} $http * @param {object} $compile */ - .directive('ckReplace', function($http, $compile, $injector) { + .directive('ckReplace', function($http, $compile) { return { restrict: 'E', scope: true, @@ -41,12 +41,13 @@ angular.module('choko') }) /** - * [description] - * @param {[type]} $http - * @param {[type]} $compile - * @return {[type]} + * Handles button or button groups for navigation bars. + * @todo + * be moved to this extension's directory. + * @param {object} $http + * @param {object} $compile */ - .directive('ckButton', function($http, $compile, $injector) { + .directive('ckButton', function($http, $compile) { return { restrict: 'E', scope: true, @@ -56,7 +57,7 @@ angular.module('choko') // Add bootstrap button class. scope.item.classes = scope.item.classes || ['btn-default']; - // @TODO: we should probably allow for custom templates, as we do in + // @todo: we should probably allow for custom templates, as we do in // cdReplace directive above. var template = scope.item.items ? '/templates/btn-group-dropdown.html' : '/templates/btn-group-button.html'; diff --git a/applications/default/public/js/services.js b/applications/default/public/js/services.js index 5548194..a34a79d 100644 --- a/applications/default/public/js/services.js +++ b/applications/default/public/js/services.js @@ -1,3 +1,7 @@ +/** + * @file Choko core services. + */ + 'use strict'; /* Services */ From 5476a4c631293a80898971396560ec5dffeb3a8c Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sun, 25 May 2014 02:05:03 -0300 Subject: [PATCH 06/20] Wrapped directives into angular dependency injection array handlers. --- applications/default/public/js/directives.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/applications/default/public/js/directives.js b/applications/default/public/js/directives.js index 21ae347..7b0b447 100644 --- a/applications/default/public/js/directives.js +++ b/applications/default/public/js/directives.js @@ -22,7 +22,7 @@ angular.module('choko') * @param {object} $http * @param {object} $compile */ - .directive('ckReplace', function($http, $compile) { + .directive('ckReplace', ['$http', '$compile', function($http, $compile) { return { restrict: 'E', scope: true, @@ -38,7 +38,7 @@ angular.module('choko') }; } }; - }) + }]) /** * Handles button or button groups for navigation bars. @@ -47,7 +47,7 @@ angular.module('choko') * @param {object} $http * @param {object} $compile */ - .directive('ckButton', function($http, $compile) { + .directive('ckButton', ['$http', '$compile', function($http, $compile) { return { restrict: 'E', scope: true, @@ -66,7 +66,7 @@ angular.module('choko') }; } }; - }); + }]); /** * Helper function to retrive a template for a element, compile it and From 0d740671a2f4a411d5817ce16394772adbcf6280 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sun, 25 May 2014 02:13:38 -0300 Subject: [PATCH 07/20] Refactored services.js to comment out the code. --- applications/default/public/js/services.js | 38 ++++++++++++++++------ 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/applications/default/public/js/services.js b/applications/default/public/js/services.js index a34a79d..f6c4bbe 100644 --- a/applications/default/public/js/services.js +++ b/applications/default/public/js/services.js @@ -7,25 +7,43 @@ /* Services */ angular.module('choko.services', []) - .factory('Choko', function($resource) { - return $resource('/rest/:type/:key', { + /** + * Choko main REST factory. + * @param {object} $resource + * @return {object} A RESTful resource. + */ + .factory('Choko', ['$resource', function($resource) { + + var url = '/rest/:type/:key'; + var defaultParams = { type: '@type', key: '@key' - }, - { + }; + var actions = { 'get': { method: 'GET', + /** + * Modifies and parses the returned data. + * @param {object} data + * @return {object|object[]} data.data + */ transformResponse: function (data) { return angular.fromJson(data).data; }, - // Data is an Object, not an Array. + // Data is an object containing a property called data, which contains + // the actual retrieved data. isArray: false } - }); - }) + } + + return $resource(url, defaultParams, actions); + }]) - // Shared server with application state. - .factory('applicationState', function($rootScope) { + /** + * Application state wrapper, to be shared across controllers. + * P.s.: States are actual scopes. + */ + .factory('applicationState', function() { var state = {}; return { get: function() { @@ -33,6 +51,6 @@ angular.module('choko.services', []) }, set: function(newState) { return state = newState; - }, + } }; }); From c08c5f2716df2c35ea9f3ab6a80c3fa5cf8672e8 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sun, 25 May 2014 02:15:59 -0300 Subject: [PATCH 08/20] Appended core services to the main AngularJS app, not a sub-module. --- applications/default/public/js/app.js | 1 - applications/default/public/js/services.js | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/applications/default/public/js/app.js b/applications/default/public/js/app.js index b7144fc..20e697a 100644 --- a/applications/default/public/js/app.js +++ b/applications/default/public/js/app.js @@ -11,7 +11,6 @@ var dependencies = [ 'ngSanitize', 'summernote', 'angularFileUpload', - 'choko.services', 'choko.filters' ]; diff --git a/applications/default/public/js/services.js b/applications/default/public/js/services.js index f6c4bbe..04297b6 100644 --- a/applications/default/public/js/services.js +++ b/applications/default/public/js/services.js @@ -4,8 +4,8 @@ 'use strict'; -/* Services */ -angular.module('choko.services', []) +// Append services to main choko module. +angular.module('choko') /** * Choko main REST factory. From f9b9d57a9970e69a5b02d7937233ab81b292b599 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sun, 25 May 2014 02:21:16 -0300 Subject: [PATCH 09/20] Commented out filters.js file. --- applications/default/public/js/filters.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/applications/default/public/js/filters.js b/applications/default/public/js/filters.js index 71cd7a6..3d98ddc 100644 --- a/applications/default/public/js/filters.js +++ b/applications/default/public/js/filters.js @@ -1,6 +1,8 @@ -'use strict'; +/** + * @file Choko core filters. + */ -/* Filters */ +'use strict'; angular.module('choko.filters', []) .filter('interpolate', ['version', function(version) { From 94ddc2cd3f369acf7302f830dde8c99414e9e050 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sun, 25 May 2014 02:21:45 -0300 Subject: [PATCH 10/20] Appended core filters to the main AngularJS app, not a sub-module. --- applications/default/public/js/app.js | 3 +-- applications/default/public/js/filters.js | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/applications/default/public/js/app.js b/applications/default/public/js/app.js index 20e697a..43f1f65 100644 --- a/applications/default/public/js/app.js +++ b/applications/default/public/js/app.js @@ -10,8 +10,7 @@ var dependencies = [ 'ngResource', 'ngSanitize', 'summernote', - 'angularFileUpload', - 'choko.filters' + 'angularFileUpload' ]; // Declare main choko module. diff --git a/applications/default/public/js/filters.js b/applications/default/public/js/filters.js index 3d98ddc..8d942e7 100644 --- a/applications/default/public/js/filters.js +++ b/applications/default/public/js/filters.js @@ -4,7 +4,7 @@ 'use strict'; -angular.module('choko.filters', []) +angular.module('choko') .filter('interpolate', ['version', function(version) { return function(text) { return String(text).replace(/\%VERSION\%/mg, version); From baccddfac81fe02692fdff3d9982590c3c05a9ad Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sun, 25 May 2014 02:37:05 -0300 Subject: [PATCH 11/20] Improved 'keys' filter documentation and code. --- applications/default/public/js/filters.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/applications/default/public/js/filters.js b/applications/default/public/js/filters.js index 8d942e7..79c7463 100644 --- a/applications/default/public/js/filters.js +++ b/applications/default/public/js/filters.js @@ -10,11 +10,14 @@ angular.module('choko') return String(text).replace(/\%VERSION\%/mg, version); } }]) + + /** + * Returns the keys of a given acceptable value/object. + * @param {object|array|function} input + * @return {array} + */ .filter('keys', function() { return function(input) { - if (!input) { - return []; - } - return Object.keys(input); + return Boolean(['object', 'function'].indexOf(typeof input) + 1) ? Object.keys(input) : []; } }); From 4972f0540ce32e40d337aab0b4b613ea077af7b8 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sun, 25 May 2014 02:39:59 -0300 Subject: [PATCH 12/20] Removed deprecated filter 'interpolate', as it's goal seems to now be achieved by using the 'version' directive. --- applications/default/public/js/filters.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/applications/default/public/js/filters.js b/applications/default/public/js/filters.js index 79c7463..45c3d3e 100644 --- a/applications/default/public/js/filters.js +++ b/applications/default/public/js/filters.js @@ -5,11 +5,6 @@ 'use strict'; angular.module('choko') - .filter('interpolate', ['version', function(version) { - return function(text) { - return String(text).replace(/\%VERSION\%/mg, version); - } - }]) /** * Returns the keys of a given acceptable value/object. From 0b02b0df386787c1d6e2527375a7ed4789306d86 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Sun, 25 May 2014 18:08:57 -0300 Subject: [PATCH 13/20] Added heading JSDoc comment to controller's script. --- applications/default/public/js/controllers.js | 4 ++++ applications/default/public/js/directives.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/applications/default/public/js/controllers.js b/applications/default/public/js/controllers.js index ebd35df..56c7a33 100644 --- a/applications/default/public/js/controllers.js +++ b/applications/default/public/js/controllers.js @@ -1,3 +1,7 @@ +/** + * @file Choko core controllers. + */ + 'use strict'; function ApplicationController($scope, $location, $http, applicationState, Choko) { diff --git a/applications/default/public/js/directives.js b/applications/default/public/js/directives.js index 7b0b447..14acd72 100644 --- a/applications/default/public/js/directives.js +++ b/applications/default/public/js/directives.js @@ -6,7 +6,7 @@ // Append directives to main choko module. angular.module('choko') - + /** * Directive to return the application's version. * @param {string} version From aea75c21c556c97a9a7880e16d1d3f90d2bef00b Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Mon, 26 May 2014 15:13:12 -0300 Subject: [PATCH 14/20] Replaced block comments with inline ones for Angular service/filter declaration. --- applications/default/public/js/app.js | 5 +---- applications/default/public/js/directives.js | 22 ++++++-------------- applications/default/public/js/filters.js | 16 ++++++++------ applications/default/public/js/services.js | 16 +++++--------- 4 files changed, 22 insertions(+), 37 deletions(-) diff --git a/applications/default/public/js/app.js b/applications/default/public/js/app.js index 43f1f65..4ee608e 100644 --- a/applications/default/public/js/app.js +++ b/applications/default/public/js/app.js @@ -21,10 +21,7 @@ angular.module('choko', dependencies) // value but other metadata that might be used thoughout the application. .value('version', '0.0.4') - /** - * Configures the location provider. - * @param {object} $locationProvider - */ + // Location/routing configuration. .config(['$locationProvider', function($locationProvider) { // Use HTML5 mode to remove "#" symbols from angular-routed pages. diff --git a/applications/default/public/js/directives.js b/applications/default/public/js/directives.js index 14acd72..aa9faae 100644 --- a/applications/default/public/js/directives.js +++ b/applications/default/public/js/directives.js @@ -7,21 +7,14 @@ // Append directives to main choko module. angular.module('choko') - /** - * Directive to return the application's version. - * @param {string} version - */ + // Directive to return the application's version. .directive('appVersion', ['version', function(version) { return function(scope, elm, attrs) { elm.text(version); }; }]) - /** - * Replaces any tag with overridable templates from the server. - * @param {object} $http - * @param {object} $compile - */ + // Directive to replaces any tag with overridable templates from the server. .directive('ckReplace', ['$http', '$compile', function($http, $compile) { return { restrict: 'E', @@ -40,13 +33,9 @@ angular.module('choko') }; }]) - /** - * Handles button or button groups for navigation bars. - * @todo - * be moved to this extension's directory. - * @param {object} $http - * @param {object} $compile - */ + // Handles button or button groups for navigation bars. + // @todo This directive is specifically used by the navigation extension. + // Therefore it should be moved to this extension's directory. .directive('ckButton', ['$http', '$compile', function($http, $compile) { return { restrict: 'E', @@ -71,6 +60,7 @@ angular.module('choko') /** * Helper function to retrive a template for a element, compile it and * replace the element with it's newly compiled one. + * @todo Create a new directive to acomplish this behavior. * @param {object} scope * @param {object} element * @param {string} templateUrl diff --git a/applications/default/public/js/filters.js b/applications/default/public/js/filters.js index 45c3d3e..6c61200 100644 --- a/applications/default/public/js/filters.js +++ b/applications/default/public/js/filters.js @@ -6,13 +6,17 @@ angular.module('choko') - /** - * Returns the keys of a given acceptable value/object. - * @param {object|array|function} input - * @return {array} - */ + // Filter to get an array of keys for an object. .filter('keys', function() { - return function(input) { + + /** + * Returns the keys of a given acceptable value/object. + * @param {object|array|function} input + * @return {array} + */ + function objectKeysFilter(input) { return Boolean(['object', 'function'].indexOf(typeof input) + 1) ? Object.keys(input) : []; } + + return objectKeysFilter; }); diff --git a/applications/default/public/js/services.js b/applications/default/public/js/services.js index 04297b6..5d98136 100644 --- a/applications/default/public/js/services.js +++ b/applications/default/public/js/services.js @@ -7,11 +7,7 @@ // Append services to main choko module. angular.module('choko') - /** - * Choko main REST factory. - * @param {object} $resource - * @return {object} A RESTful resource. - */ + // Choko main REST factory. .factory('Choko', ['$resource', function($resource) { var url = '/rest/:type/:key'; @@ -30,8 +26,8 @@ angular.module('choko') transformResponse: function (data) { return angular.fromJson(data).data; }, - // Data is an object containing a property called data, which contains - // the actual retrieved data. + // Server will always return an object containing at least a 'data' + // property to hold the actual data and a status property. isArray: false } } @@ -39,10 +35,8 @@ angular.module('choko') return $resource(url, defaultParams, actions); }]) - /** - * Application state wrapper, to be shared across controllers. - * P.s.: States are actual scopes. - */ + // Application state wrapper, to be shared across controllers. + // P.s.: States are actual scope objects. .factory('applicationState', function() { var state = {}; return { From b59f3a6e6047c27652f4b627f03ab2cd0889535a Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Mon, 26 May 2014 17:24:45 -0300 Subject: [PATCH 15/20] Minor syntax adjustments. --- applications/default/public/js/filters.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/applications/default/public/js/filters.js b/applications/default/public/js/filters.js index 6c61200..0ac2557 100644 --- a/applications/default/public/js/filters.js +++ b/applications/default/public/js/filters.js @@ -8,7 +8,7 @@ angular.module('choko') // Filter to get an array of keys for an object. .filter('keys', function() { - + /** * Returns the keys of a given acceptable value/object. * @param {object|array|function} input @@ -20,3 +20,4 @@ angular.module('choko') return objectKeysFilter; }); + \ No newline at end of file From 0312275d000e2a11025fcd236985005ec0c2a9cb Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Mon, 26 May 2014 21:48:45 -0300 Subject: [PATCH 16/20] Pull #113: Removes block comment from a callback function, for this is no function declaration. --- applications/default/public/js/services.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/applications/default/public/js/services.js b/applications/default/public/js/services.js index 5d98136..a99254e 100644 --- a/applications/default/public/js/services.js +++ b/applications/default/public/js/services.js @@ -18,11 +18,6 @@ angular.module('choko') var actions = { 'get': { method: 'GET', - /** - * Modifies and parses the returned data. - * @param {object} data - * @return {object|object[]} data.data - */ transformResponse: function (data) { return angular.fromJson(data).data; }, From f3b160a407e0f6059265e3e89e922485a78e8b37 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Mon, 26 May 2014 21:55:04 -0300 Subject: [PATCH 17/20] Minor comment adjustments. --- applications/default/public/js/directives.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/default/public/js/directives.js b/applications/default/public/js/directives.js index aa9faae..e20def9 100644 --- a/applications/default/public/js/directives.js +++ b/applications/default/public/js/directives.js @@ -14,7 +14,7 @@ angular.module('choko') }; }]) - // Directive to replaces any tag with overridable templates from the server. + // Directive to replace any tag with overridable templates from the server. .directive('ckReplace', ['$http', '$compile', function($http, $compile) { return { restrict: 'E', From 11de1df0aff37bf248a7c1ed7f53c3fe3b2a6283 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Tue, 27 May 2014 00:43:23 -0300 Subject: [PATCH 18/20] Issue #113: Refactored directives to dynamically include other directives, making the final implementations simpler. --- .../form/public/templates/form.html | 2 +- .../form/public/templates/subform.html | 2 +- applications/default/public/js/directives.js | 143 ++++++++++++------ 3 files changed, 100 insertions(+), 47 deletions(-) diff --git a/applications/default/extensions/form/public/templates/form.html b/applications/default/extensions/form/public/templates/form.html index 7c17f68..c2b1330 100644 --- a/applications/default/extensions/form/public/templates/form.html +++ b/applications/default/extensions/form/public/templates/form.html @@ -4,5 +4,5 @@
  • {{error}}
  • - + diff --git a/applications/default/extensions/form/public/templates/subform.html b/applications/default/extensions/form/public/templates/subform.html index 5b367ec..0ef73b7 100644 --- a/applications/default/extensions/form/public/templates/subform.html +++ b/applications/default/extensions/form/public/templates/subform.html @@ -6,5 +6,5 @@

    Edit {{element.subform.title | lowercase}}

  • {{error}}
  • - + diff --git a/applications/default/public/js/directives.js b/applications/default/public/js/directives.js index e20def9..78c1fce 100644 --- a/applications/default/public/js/directives.js +++ b/applications/default/public/js/directives.js @@ -17,18 +17,93 @@ angular.module('choko') // Directive to replace any tag with overridable templates from the server. .directive('ckReplace', ['$http', '$compile', function($http, $compile) { return { - restrict: 'E', - scope: true, + restrict: 'EA', + + // As this directive will replace the existing markup, it's better that + // we run it previously to most other directives, to avoid dumb processing. + priority: 100, compile: function(element, attrs) { + return function(scope, element, attrs) { + + // Request the template content. + var loadTemplate = $http({ + method: 'GET', + // If the directive is an element, "src" should be available. + url: attrs.ckReplace || attrs.src, + cache: true + }); + + // When ready, compile the retrieved template. + loadTemplate.then(function(result) { + + // Compile the returned template. + var compiled = $compile(result.data)(scope); + + // Replace old element with compiled one. + element.replaceWith(angular.element(compiled)); + + }); + }; + } + }; + }]) + + // A helper service to handle re-compiling of directives. + .factory('ckReplaceAndRecompile', ['$compile', function ($compile) { + /** + * Creates a new element from the given, copying attributes but removing + * the old directive to avoid running it again. Recompiles the new + * element and replaces the old with it. + */ + return function (element, directiveToRemove, scope, newTag) { + + // Handle multiple removals using a array of removing directives. + var directives = directiveToRemove.length ? directiveToRemove : []; + var tagName = element.prop('localName'); + var replacement; + + // Replace the directive, be it a tag name or attribute. + directives.forEach(function (directive) { + if (tagName == directive) { + replacement = angular.element(document.createElement(newTag || 'div')); + angular.element.each(element[0].attributes, function (i, attr) { + replacement.attr(attr.name, attr.value); + }); + element.replaceWith(replacement); + element = replacement; + } else { + element.removeAttr(directive); + } + }); + + // Recompile the element. + $compile(element)(scope || {}); + } + }]) + + // Directive to replace form elements with overridable templates from + // the server. + .directive('ckReplaceElement', ['ckReplaceAndRecompile', function(ckReplaceAndRecompile) { + return { + restrict: 'EA', + // This directive will insert other directives dynamically. We set + // priority=999 (high number) to make it run first, but nor before core + // Angular directives, as we need their data. (ng-repeat runs at 1000 + // priority). + priority: 999, + terminal: true, + compile: function (tElement, attrs) { return function(scope, element, attrs) { // Allow for custom templates but fallback to default one based // on element type. scope.element.template = scope.element.template || 'templates/' + scope.element.type + '.html'; - - // Retrive and compile element template. - compileAndReplace(scope, element, scope.element.template); - }; + + // Append the ck-replace directive. + element.attr('ck-replace', scope.element.template); + + ckReplaceAndRecompile(element, ['ck-replace-element', 'ng-repeat'], scope); + } } }; }]) @@ -36,56 +111,34 @@ angular.module('choko') // Handles button or button groups for navigation bars. // @todo This directive is specifically used by the navigation extension. // Therefore it should be moved to this extension's directory. - .directive('ckButton', ['$http', '$compile', function($http, $compile) { + .directive('ckButton', ['ckReplaceAndRecompile', function(ckReplaceAndRecompile) { return { - restrict: 'E', + restrict: 'EA', scope: true, + // This directive will insert other directives dynamically. We set + // priority=999 (high number) to make it run first, but nor before core + // Angular directives, as we need their data. (ng-repeat runs at 1000 + // priority). + priority: 999, + terminal: true, compile: function(element, attrs) { return function(scope, element, attrs) { // Add bootstrap button class. - scope.item.classes = scope.item.classes || ['btn-default']; + scope.item.classes = scope.item.classes || []; + if (scope.item.classes.indexOf('btn-default')+1) { + scope.item.classes.push('btn-default'); + } // @todo: we should probably allow for custom templates, as we do in - // cdReplace directive above. + // ckReplaceElement directive above. var template = scope.item.items ? '/templates/btn-group-dropdown.html' : '/templates/btn-group-button.html'; - // Retrive and compile element template. - compileAndReplace(scope, element, template); + // Append the replacement directive. + element.attr('ck-replace', template); + + ckReplaceAndRecompile(element, ['ck-button', 'ng-repeat'], scope); }; } }; }]); - -/** - * Helper function to retrive a template for a element, compile it and - * replace the element with it's newly compiled one. - * @todo Create a new directive to acomplish this behavior. - * @param {object} scope - * @param {object} element - * @param {string} templateUrl - */ -function compileAndReplace(scope, element, templateUrl) { - - var $injector = element.injector(); - var $http = $injector.get('$http'); - var $compile = $injector.get('$compile'); - - // Request the template content. - var loadTemplate = $http({ - method: 'GET', - url: templateUrl, - cache: true - }); - - // When ready, compile the retrieved template. - loadTemplate.then(function(result) { - - // Compile the returned template. - var compiled = $compile(result.data)(scope); - - // Replace old element with compiled one. - element.replaceWith(angular.element(compiled)); - - }); -} \ No newline at end of file From 87a4a0c27d4d46c2d5d760acabe3cba7442f90b5 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Tue, 27 May 2014 02:39:31 -0300 Subject: [PATCH 19/20] Smart ass validation of array index. --- applications/default/public/js/filters.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/default/public/js/filters.js b/applications/default/public/js/filters.js index 0ac2557..28c33c9 100644 --- a/applications/default/public/js/filters.js +++ b/applications/default/public/js/filters.js @@ -15,7 +15,7 @@ angular.module('choko') * @return {array} */ function objectKeysFilter(input) { - return Boolean(['object', 'function'].indexOf(typeof input) + 1) ? Object.keys(input) : []; + return ~['object', 'function'].indexOf(typeof input) ? Object.keys(input) : []; } return objectKeysFilter; From aaa09086f5230d8b802dec16730d77cd0ccf0217 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Date: Mon, 2 Jun 2014 21:21:56 -0300 Subject: [PATCH 20/20] Avoid errors by setting a default for panels classes array. --- applications/default/public/js/controllers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/default/public/js/controllers.js b/applications/default/public/js/controllers.js index 56c7a33..06bc445 100644 --- a/applications/default/public/js/controllers.js +++ b/applications/default/public/js/controllers.js @@ -105,7 +105,7 @@ function RegionController($scope, $location, applicationState, Choko) { //RegionController.$inject = ['$scope', '$location', 'applicationState', 'Choko']; function NavigationController($scope, $location, $window, applicationState, Choko) { - $scope.panel.classes.unshift('nav'); + ($scope.panel.classes = $scope.panel.classes || []).unshift('nav'); $scope.isAbsolute = function(url) { return /^(?:[a-z]+:)?\/\//i.test(url);