Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
bower_components
*.iml
.idea/
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"version": "1.0.0",
"description": "An implementation of Bootstrap's scollspy and affix for AngularJS",
"dependencies": {
"angular": "~1.2.0"
"angular": "~1.4.0"
},
"main":"./dist/ngScrollSpy.js",
"devDependencies": {
"angular-mocks": "~1.2.0"
"angular-mocks": "~1.4.0"
}
}
131 changes: 74 additions & 57 deletions dist/ngScrollSpy.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,26 +313,30 @@ var state = {
this[k] = s[k];
}
this.state = true;
this.run();
//this.run();
},
builder: null,
setBuilder: function(builder) {
this.builder = builder;
this.run();
//this.run();
},
itemsLoader: null,
setItemsLoader: function(itemsLoader) {
this.itemsLoader = itemsLoader;
},
run: function() {
if (this.builder && this.state) {
this.builder();
this.builder= null;
this.state= null;
//this.builder = null;
this.state = null;
if(this.onRun) {
this.onRun();
this.onRun= null;
this.onRun = null;
}
}
}
};
mod.directive('pageitems', function(ScrollSpy) {
mod.directive('pageitems', function(ScrollSpy, $rootScope, $timeout) {
var linkfn = function(scope, elem, attrs) {
if (!angular.isDefined(scope.selector)) {
void 0;
Expand All @@ -347,20 +351,20 @@ mod.directive('pageitems', function(ScrollSpy) {
};

// Store my state that pagemenu will use to build the menu
getState().store({
topMargin: function() {
return scope.topmargin | 0; // so that pagemenu can correctly offset scrolling
},
addSpy: function(spyObj) {
scope.spies[spyObj.id] = spyObj; // each item in menu calls this function to register itself with pageitems
},
getSpy: function(id) {
return scope.spies[id]; // return the spy associated with id
},
items: function() {
return scope.spyElems; // return a list of dom items to be used to build menu
}
});
getState().store({
topMargin: function() {
return scope.topmargin | 0; // so that pagemenu can correctly offset scrolling
},
addSpy: function(spyObj) {
scope.spies[spyObj.id] = spyObj; // each item in menu calls this function to register itself with pageitems
},
getSpy: function(id) {
return scope.spies[id]; // return the spy associated with id
},
items: function() {
return scope.spyElems; // return a list of dom items to be used to build menu
}
});

var spyElems = scope.spyElems;
var topmargin = scope.topmargin | 0;
Expand All @@ -374,7 +378,7 @@ mod.directive('pageitems', function(ScrollSpy) {
var spy = spies[spyElem.id];
spy.clear();

if (spyElem.getBoundingClientRect().top === undefined) {
if (typeof spyElem.getBoundingClientRect().top === 'undefined') {
continue;
}

Expand Down Expand Up @@ -402,7 +406,7 @@ mod.directive('pageitems', function(ScrollSpy) {
highlightSpy.set();
scope.$on('destroy', function() {
ScrollSpy.removeHandler(scrollHandler);
});
});

});
};
Expand All @@ -413,7 +417,22 @@ mod.directive('pageitems', function(ScrollSpy) {
selector: '@',
topmargin: '@'
},
link: linkfn
link: function (scope, element, attrs) {
getState().setItemsLoader(function() {
linkfn(scope, element, attrs);
});

getState().itemsLoader();

scope.$watch(function () {
return attrs.reloadOn;
}, function () {
$timeout(function () { //wait for translate completion
getState().itemsLoader();
getState().run();
}, 0);
});
}
};
});
mod.directive('pagemenu', function($compile, $location, $anchorScroll) {
Expand Down Expand Up @@ -473,38 +492,34 @@ mod.directive('pagemenu', function($compile, $location, $anchorScroll) {
lastitem= item.link;
return item;
};

// dom items to build menu from
var items = getState().items();
var markup = '';
for (var i = 0; i < items.length; i++) {
var item = itemConstruct(items[i]);
if (item.push) {
// new submenu
markup += '<menu class="nav">';
} else if (item.pop) {
// closing submenu, maybe more than one
for (var j = 0; j < item.pop; j++) {
markup += '</li></menu>';
}
} else if (i !== 0) {
// sibling
markup += '</li>';
}

// basic markup
markup += '<li pagemenuspy="' + item.link + '" parent="' + item.parent + '">';
markup += '<a href="#' + item.link + '">';
markup += item.text;
markup += '</a>';
}
markup += '</li>';
element.append($compile(markup)(scope));

element.on('click', function(e) {
// menu item clicked, lets scroll to the associated dom item
var hash = e.target.hash.substring(1);
$location.hash(hash);
// dom items to build menu from
var items = getState().items();
var markup = '';
for (var i = 0; i < items.length; i++) {
var item = itemConstruct(items[i]);
if (item.push) {
// new submenu
markup += '<menu class="nav">';
} else if (item.pop) {
// closing submenu, maybe more than one
for (var j = 0; j < item.pop; j++) {
markup += '</li></menu>';
}
} else if (i !== 0) {
// sibling
markup += '</li>';
}

// basic markup
markup += '<li pagemenuspy="' + item.link + '" parent="' + item.parent + '">';
markup += '<a ng-click="scrollTo(\'' + item.link + '\')">';
markup += item.text;
markup += '</a>';
}
markup += '</li>';

scope.scrollTo = function (anchor) {
$location.hash(anchor);
$anchorScroll();
if(getState().topMargin() !== 0 ) {
setTimeout(function() {
Expand All @@ -515,7 +530,9 @@ mod.directive('pagemenu', function($compile, $location, $anchorScroll) {
);
}, 0);
}
});
};

element.html($compile(markup)(scope));
};

return {
Expand Down Expand Up @@ -559,4 +576,4 @@ mod.directive('pagemenuspy', function($location, $anchorScroll) {
});

function getState() { return state; }
})(angular);
})(angular);