From df3c3b0883df78d7116a5c7b38d002c3f1552b3e Mon Sep 17 00:00:00 2001 From: Andres Barrios Date: Thu, 30 Jun 2016 15:38:36 -0300 Subject: [PATCH 1/3] Fix sub-directives compilation. --- lib/molecules/select-ul/example.html | 15 +- lib/molecules/select-ul/metadata.json | 2 +- .../select-ul/select-ul-item.directive.js | 14 -- .../select-ul/select-ul.directive.js | 133 +++++++++--------- lib/molecules/select-ul/template.html | 14 +- 5 files changed, 85 insertions(+), 93 deletions(-) delete mode 100644 lib/molecules/select-ul/select-ul-item.directive.js diff --git a/lib/molecules/select-ul/example.html b/lib/molecules/select-ul/example.html index bd13202..bdb49b9 100644 --- a/lib/molecules/select-ul/example.html +++ b/lib/molecules/select-ul/example.html @@ -1,8 +1,9 @@ - + diff --git a/lib/molecules/select-ul/metadata.json b/lib/molecules/select-ul/metadata.json index 23f566b..42428dc 100644 --- a/lib/molecules/select-ul/metadata.json +++ b/lib/molecules/select-ul/metadata.json @@ -2,7 +2,7 @@ "name": "ln-m-select-ul", "description": "Molecule - Select control which converts to a ul for styling on desktop.", "params": { - "ngModel": "VAR", + "lnModel": "VAR", "lnOptions": [{ "value": "STRING", "label": "STRING" diff --git a/lib/molecules/select-ul/select-ul-item.directive.js b/lib/molecules/select-ul/select-ul-item.directive.js deleted file mode 100644 index 0989e4d..0000000 --- a/lib/molecules/select-ul/select-ul-item.directive.js +++ /dev/null @@ -1,14 +0,0 @@ -angular - .module('lnPatterns') - .directive('lnMSelectUlItem', lnMSelectUlItem); - -function lnMSelectUlItem() { - - return { - restrict: 'A', - template: '', - scope: { - lnLabel: '@' - } - }; -} diff --git a/lib/molecules/select-ul/select-ul.directive.js b/lib/molecules/select-ul/select-ul.directive.js index b6d12ba..72c64aa 100644 --- a/lib/molecules/select-ul/select-ul.directive.js +++ b/lib/molecules/select-ul/select-ul.directive.js @@ -1,29 +1,23 @@ -// require('@iamadamjowett/angular-click-outside'); -require( './select-ul-item.directive'); - angular .module('lnPatterns') - .directive('lnMSelectUl', lnMSelectUl); - + .constant('DEFAULT_DIRECTIVE', 'ln-m-select-ul-item') + .directive('lnMSelectUl', lnMSelectUl) + .directive('lnMSelectUlWrapper', lnMSelectUlWrapper) + .directive('lnMSelectUlItem', lnMSelectUlItem); -// angular -// .module('lnSelectToUl', [ -// 'angular-click-outside' -// ]) -// .directive('selectToUl', selectToUl); +///////////// -lnMSelectUl.$inject = ['$timeout', '$sanitize', '$compile', '$sce']; - -function lnMSelectUl( $timeout, $sanitize, $compile, $sce ) { +lnMSelectUl.$inject = ['$timeout', 'DEFAULT_DIRECTIVE']; +function lnMSelectUl( $timeout, DEFAULT_DIRECTIVE ) { return { restrict: 'E', link: link, templateUrl: 'lnPatterns/molecules/select-ul/template.html', - controller: controller, + controller: lnMSelectUlController, controllerAs: 'vm', scope: { - ngModel: '=', + lnModel: '=', lnOptions: '=', lnValueField: '@', lnLabelField: '@', @@ -36,27 +30,15 @@ function lnMSelectUl( $timeout, $sanitize, $compile, $sce ) { }; function link( scope, elem ) { - - var DEFAULT_DIRECTIVE = 'ln-m-select-ul-item'; - scope.valueField = scope.lnValueField ? scope.lnValueField : 'value'; - scope.labelField = scope.lnLabelField ? scope.lnLabelField : 'label'; - scope.required = scope.lnRequired ? scope.lnRequired : false; - scope.placholder = scope.lnPlaceholder ? scope.lnPlaceholder : '-- Select --'; - scope.none = scope.lnNone ? scope.lnNone : false; - scope.mobileNone = scope.lnMobileNone ? scope.lnMobileNone : false; - scope.itemDirective = scope.lnItemDirective ? scope.lnItemDirective : DEFAULT_DIRECTIVE; - scope.displayOptions = false; - scope.$watch( 'lnOptions', compile ); - scope.getSelected = function( field ) { var selected = {}; @@ -64,20 +46,20 @@ function lnMSelectUl( $timeout, $sanitize, $compile, $sce ) { if ( angular.equals({}, selected) ) { selected[scope.valueField] = null; - selected[scope.labelField] = scope.lnPlaceholder; + selected[scope.labelField] = scope.placholder; } return angular.isDefined( field ) ? selected[ field ] : selected; function checkOption( option ) { - if ( scope.ngModel === option[scope.lnValueField] ) { + if ( scope.lnModel === option[scope.lnValueField] ) { selected = option; } } }; scope.setSelected = function( index ) { - scope.ngModel = -1 === index ? null : scope.lnOptions[index][scope.valueField]; + scope.lnModel = -1 === index ? null : scope.lnOptions[index][scope.valueField]; scope.hideOptions(); }; @@ -96,45 +78,14 @@ function lnMSelectUl( $timeout, $sanitize, $compile, $sce ) { scope.showOptions = function() { scope.displayOptions = true; }; - - scope.getOptionHtml = function( option ) { - var props = ''; - - if ( DEFAULT_DIRECTIVE === scope.itemDirective ) { - props = ' ln-label="' + option[scope.labelField] + '"'; - } else { - for ( var key in option ) { - if ( option.hasOwnProperty( key ) ) { - var parAttr = 'mx-' + normalize( key ); - - var value = angular.isObject(option[key]) ? $sanitize(angular.toJson(option[key])) : option[key]; - - props += ' ' + parAttr + '="' + value + '"'; - } - } - } - - return $sce.trustAsHtml( '' ); - }; - - function normalize( string ) { - return string.replace( /_/g, '-' ).toLowerCase(); - } - - function compile() { - $timeout( compileElem, 2000 ); - - function compileElem() { - $compile( elem[0].querySelector('.desktop li') )( scope ); - } - } } } +///////////// -controller.$inject = ['$document']; +lnMSelectUlController.$inject = ['$document']; -function controller($document) { +function lnMSelectUlController($document) { var vm = this; vm.hasTouchSupport = isTouchSupported() && isMobile(); @@ -152,3 +103,57 @@ function controller($document) { return devices.test(userAgent); } } + +///////////// + +lnMSelectUlWrapper.$inject = ['DEFAULT_DIRECTIVE', '$sanitize', '$compile']; + +function lnMSelectUlWrapper( DEFAULT_DIRECTIVE, $sanitize, $compile ) { + return { + restrict: 'A', + link: link, + scope: { + lnOption: '=' + } + }; + + function link( scope, elem ) { + var props = ''; + + if ( DEFAULT_DIRECTIVE === scope.$parent.itemDirective ) { + props = ' ln-label="' + scope.lnOption[scope.$parent.labelField] + '"'; + } else { + for ( var key in scope.lnOption ) { + if ( scope.lnOption.hasOwnProperty( key ) ) { + var parAttr = 'mx-' + normalize( key ); + + var value = + angular.isObject(scope.lnOption[key]) + ? $sanitize(angular.toJson(scope.lnOption[key])) + : scope.lnOption[key]; + + props += ' ' + parAttr + '="' + value + '"'; + } + } + } + + var span = $compile( '' )( scope ); + elem.append( span ); + } + + function normalize( string ) { + return string.replace( /_/g, '-' ).toLowerCase(); + } +} + +///////////// + +function lnMSelectUlItem() { + return { + restrict: 'A', + template: '', + scope: { + lnLabel: '@' + } + }; +} \ No newline at end of file diff --git a/lib/molecules/select-ul/template.html b/lib/molecules/select-ul/template.html index 1a6c608..38a3e5a 100644 --- a/lib/molecules/select-ul/template.html +++ b/lib/molecules/select-ul/template.html @@ -1,10 +1,9 @@
- + ng-bind-html="getSelected(labelField)"> +
  • + ln-m-select-ul-wrapper + ln-option="option">
-
-