Enable page navigation via URL fragment

The broadcasting of notifications didn't update properly and I couldn't
find a way to listen to $location updates. I replaced the three
intertwined directives with one controller and a bit more HTML code. Now
we have only one scope that listens to $location and all browser actions
like the back button is reflected in the page.

As nice side-effect, the menu links have now the right destination so
that you can copy the link and paste it into another browser window.

40 lines less code.
This commit is contained in:
Maikel Linke
2020-03-05 17:17:06 +11:00
parent bf26a26743
commit ea80ae3832
7 changed files with 21 additions and 56 deletions

View File

@@ -0,0 +1,10 @@
Darkswarm.controller "PagesetCtrl", ($scope, $location) ->
$scope.selected = ->
path = $location.path()?.match(/^\/\w+$/)?[0]
if path
path[1..]
else
$scope.defaultPage
$scope.selectDefault = (selection) ->
$scope.defaultPage = selection

View File

@@ -1,12 +0,0 @@
Darkswarm.directive "page", ->
restrict: "C"
require: "^^pagesetCtrl"
scope:
name: "@"
link: (scope, element, attrs, ctrl) ->
element.on "click", ->
scope.$apply ->
ctrl.toggle(scope.name)
ctrl.registerSelectionListener (prefix, selection) ->
element.toggleClass('selected', selection == scope.name)

View File

@@ -1,11 +0,0 @@
Darkswarm.directive "pageView", ->
restrict: "C"
require: "^^pagesetCtrl"
template: "<div ng-include='template'></div>"
scope:
templates: "="
link: (scope, element, attrs, ctrl) ->
scope.template = null
ctrl.registerSelectionListener (prefix, selection) ->
scope.template = "#{prefix}/#{selection}.html"

View File

@@ -1,26 +0,0 @@
Darkswarm.directive "pagesetCtrl", ($location) ->
restrict: "C"
scope:
id: "@"
selected: "@"
prefix: "@?"
controller: ($scope, $element) ->
path = $location.path()?.match(/^\/\w+$/)?[0]
$scope.selected = path[1..] if path
this.toggle = (name) ->
this.select(name)
this.select = (selection) ->
$scope.$broadcast("selection:changed", selection)
$element.toggleClass("expanded", selection?)
$location.path(selection)
this.registerSelectionListener = (callback) ->
$scope.$on "selection:changed", (event, selection) ->
callback($scope.prefix, selection)
this
link: (scope, element, attrs, ctrl) ->
ctrl.select(scope.selected) if scope.selected?