From c5e0762cb807082f6b84a6d1ed69cecccf5f73a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20=C5=98eho=C5=99ka?= Date: Sat, 22 Nov 2014 01:46:12 +0100 Subject: [PATCH 1/4] way how link was handled caused exceptions when not using html5 mode, better to use ng-click=scrollToAnchor(anchor) --- .gitignore | 2 ++ dist/ngScrollSpy.js | 13 ++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index a088b6f..7948123 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules bower_components +*.iml +.idea/ \ No newline at end of file diff --git a/dist/ngScrollSpy.js b/dist/ngScrollSpy.js index 5d8de0e..9e90af4 100755 --- a/dist/ngScrollSpy.js +++ b/dist/ngScrollSpy.js @@ -494,17 +494,14 @@ mod.directive('pagemenu', function($compile, $location, $anchorScroll) { // basic markup markup += '
  • '; - markup += ''; + markup += ''; markup += item.text; markup += ''; } markup += '
  • '; - 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); + scope.scrollTo = function (anchor) { + $location.hash(anchor); $anchorScroll(); if(getState().topMargin() !== 0 ) { setTimeout(function() { @@ -515,7 +512,9 @@ mod.directive('pagemenu', function($compile, $location, $anchorScroll) { ); }, 0); } - }); + }; + + element.append($compile(markup)(scope)); }; return { From 09f1e4f647bdba9e723fe3fe231cb37483fda73a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20=C5=98eho=C5=99ka?= Date: Tue, 16 Dec 2014 14:04:02 +0100 Subject: [PATCH 2/4] directive now supports onReload param - usefull with angular-translate --- dist/ngScrollSpy.js | 107 +++++++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 46 deletions(-) diff --git a/dist/ngScrollSpy.js b/dist/ngScrollSpy.js index 9e90af4..97aa27e 100755 --- a/dist/ngScrollSpy.js +++ b/dist/ngScrollSpy.js @@ -323,11 +323,11 @@ var state = { 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; } } } @@ -347,20 +347,36 @@ 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 - } - }); + var storeItems = function () { + 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 + } + }); + }; + + storeItems(); + + scope.$watch(function () { + return attrs.reloadOn; + }, function (newValue, oldValue) { + if(newValue === oldValue) { + return; + } + + scope.spyElems = elem[0].getElementsByClassName(scope.selector); + storeItems(); + getState().run(); + }); var spyElems = scope.spyElems; var topmargin = scope.topmargin | 0; @@ -374,7 +390,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; } @@ -473,32 +489,31 @@ 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 += ''; - } else if (item.pop) { - // closing submenu, maybe more than one - for (var j = 0; j < item.pop; j++) { - markup += ''; - } - } else if (i !== 0) { - // sibling - markup += ''; - } - - // basic markup - markup += '
  • '; - markup += ''; - markup += item.text; - markup += ''; - } - markup += '
  • '; + // 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 += ''; + } else if (item.pop) { + // closing submenu, maybe more than one + for (var j = 0; j < item.pop; j++) { + markup += ''; + } + } else if (i !== 0) { + // sibling + markup += ''; + } + + // basic markup + markup += '
  • '; + markup += ''; + markup += item.text; + markup += ''; + } + markup += '
  • '; scope.scrollTo = function (anchor) { $location.hash(anchor); @@ -514,7 +529,7 @@ mod.directive('pagemenu', function($compile, $location, $anchorScroll) { } }; - element.append($compile(markup)(scope)); + element.html($compile(markup)(scope)); }; return { @@ -558,4 +573,4 @@ mod.directive('pagemenuspy', function($location, $anchorScroll) { }); function getState() { return state; } -})(angular); \ No newline at end of file +})(angular); From 1eb2dfcd0fcc1f0e6a149d0cfc75c2916d11fe21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20=C5=98eho=C5=99ka?= Date: Tue, 16 Dec 2014 15:09:33 +0100 Subject: [PATCH 3/4] reload param async fix --- dist/ngScrollSpy.js | 73 +++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/dist/ngScrollSpy.js b/dist/ngScrollSpy.js index 97aa27e..0dcc907 100755 --- a/dist/ngScrollSpy.js +++ b/dist/ngScrollSpy.js @@ -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.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; @@ -347,35 +351,19 @@ mod.directive('pageitems', function(ScrollSpy) { }; // Store my state that pagemenu will use to build the menu - var storeItems = function () { - 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 - } - }); - }; - - storeItems(); - - scope.$watch(function () { - return attrs.reloadOn; - }, function (newValue, oldValue) { - if(newValue === oldValue) { - return; + 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 } - - scope.spyElems = elem[0].getElementsByClassName(scope.selector); - storeItems(); - getState().run(); }); var spyElems = scope.spyElems; @@ -418,7 +406,7 @@ mod.directive('pageitems', function(ScrollSpy) { highlightSpy.set(); scope.$on('destroy', function() { ScrollSpy.removeHandler(scrollHandler); - }); + }); }); }; @@ -429,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) { From f902de5ed063f5719aa92721c3a1936990ba111e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20=C5=98eho=C5=99ka?= Date: Mon, 12 Oct 2015 08:58:26 +0200 Subject: [PATCH 4/4] angular version updated --- bower.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bower.json b/bower.json index 83882dd..4f72d3d 100644 --- a/bower.json +++ b/bower.json @@ -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" } }