Skip to content

Commit

Permalink
distributives + demo updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilt committed Nov 4, 2016
1 parent a6a32e9 commit d9b3f9c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
6 changes: 5 additions & 1 deletion demo/insideDirective/insideDirective.html
Expand Up @@ -21,7 +21,11 @@ <h1 class="page-header page-header-exapmle">Scroller inside the directive</h1>
This sample demonstrates encapsulation of the ui-scroll directive inside another custom directive wich has it's own controller and wich uses "Controller As" syntax in it's template.
</div>

<my-dir></my-dir>
<div ng-controller="mainController">
<div ng-if="true">
<my-dir></my-dir>
</div>
</div>

</div>

Expand Down
3 changes: 3 additions & 0 deletions demo/insideDirective/insideDirective.js
@@ -1,4 +1,7 @@
angular.module('application', ['ui.scroll', 'ui.scroll.jqlite'])
.controller('mainController', ['$scope', function($scope) {
$scope.show = true;
}])
.directive('myDir', function() {
return {
restrict: 'E',
Expand Down
34 changes: 26 additions & 8 deletions dist/ui-scroll.js
@@ -1,7 +1,7 @@
/*!
* angular-ui-scroll
* https://github.com/angular-ui/ui-scroll.git
* Version: 1.5.1 -- 2016-10-30T12:41:33.695Z
* Version: 1.5.1 -- 2016-11-04T01:55:55.663Z
* License: MIT
*/

Expand Down Expand Up @@ -415,6 +415,7 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
}

function Adapter($attr, viewport, buffer, adjustBuffer, element) {
var hasViewport = !!viewport.scope();
var viewportScope = viewport.scope() || $rootScope;
var disabled = false;
var self = this;
Expand Down Expand Up @@ -524,34 +525,51 @@ angular.module('ui.scroll', []).directive('uiScrollViewport', function () {
var target = match[1];
var onControllerName = match[2];

var parseControllers = function parseControllers(controllerName) {
// ng-controller attr based DOM parsing
var parseNgCtrlAttrs = function parseNgCtrlAttrs(controllerName) {
var as = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];

var candidate = element;
while (candidate.length) {
var candidateScope = candidate.scope();
var candidateName = (candidate.attr('ng-controller') || '').match(/(\w(?:\w|\d)*)(?:\s+as\s+(\w(?:\w|\d)*))?/);
if (candidateName && candidateName[as ? 2 : 1] === controllerName) {
scope = candidate.scope();
break;
scope = candidateScope;
return true;
}
candidate = candidate.parent();
}
};

// scope based DOM pasrsing
var parseScopes = function parseScopes(controllerName) {
var candidate = element;
while (candidate.length) {
var candidateScope = candidate.scope();
if (candidateScope && candidateScope.hasOwnProperty(controllerName) && candidateScope[controllerName].constructor.name === 'controller') {
scope = candidateScope;
return true;
}
candidate = candidate.parent();
}
};

if (onControllerName) {
// 'on' syntax parsing
// 'on' syntax DOM parsing (adapter='adapter on ctrl')
scope = null;
parseControllers(onControllerName);
parseNgCtrlAttrs(onControllerName);
if (!scope) {
throw new Error('Failed to locate target controller \'' + onControllerName + '\' to inject \'' + target + '\'');
}
} else {
// try to parse with 'Controller As' syntax
// try to parse DOM with 'Controller As' syntax (adapter='ctrl.adapter')
var controllerAsName = undefined;
var dotIndex = target.indexOf('.');
if (dotIndex > 0) {
controllerAsName = target.substr(0, dotIndex);
parseControllers(controllerAsName, true);
if (!parseNgCtrlAttrs(controllerAsName, true) && !hasViewport) {
parseScopes(controllerAsName); // the case of custom Directive/Component
}
}
}

Expand Down

0 comments on commit d9b3f9c

Please sign in to comment.