Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Commit

Permalink
Added Wildcard for Routes
Browse files Browse the repository at this point in the history
  • Loading branch information
zanechua committed Jun 24, 2018
1 parent 0dd4f0c commit e5617fa
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 70 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 Poowf
Copyright (c) 2018 Poowf

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Jekko is an AdonisJS Helper Package which helps you mark currently active items in your navigation.

It takes inspiration from the [Ekko](https://github.com/laravelista/Ekko) packaage for Laravel hence Jekko.
It takes inspiration from the [Ekko](https://github.com/laravelista/Ekko) package for Laravel hence Jekko.

## Installation

Expand All @@ -16,7 +16,6 @@ From the command line:
npm install --save poowf/jekko
```


You will need to include the service provider in start/app.js

```javascript
Expand Down Expand Up @@ -86,6 +85,10 @@ isActiveRoute($routeName, $output = "active")

If the current route is `home`, function `isActiveRoute('home')` would return *string* `active`.

_The `*` wildcard can be used for resource routes_

Function `isActiveRoute('user.*')` would return string `active` for any current route which begins with `user`.

### isActiveURL

Compares given URL path with current URL path.
Expand Down Expand Up @@ -122,6 +125,10 @@ areActiveRoutes(array $routeNames, $output = "active")

If the current route is `product.index` or `product.show`, function `areActiveRoutes(['product.index', 'product.show'])` would return *string* `active`.

_The `*` wildcard can be used for resource routes_

Function `areActiveRoutes(['user.*', 'product.*'])` would return string `active` for any current route which begins with `user` or `product`.

### areActiveURLs

Compares given array of URL paths with current URL path.
Expand All @@ -138,7 +145,7 @@ If the current URL path is `/product` or `/product/create`, function `areActiveU

The MIT License (MIT)

Copyright (c) 2017 Poowf
Copyright (c) 2018 Poowf

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poowf/jekko",
"version": "1.0.3",
"version": "1.0.4",
"description": "Jekko is an AdonisJS Helper Package which helps you mark currently active items in your navigation",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions providers/JekkoProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
const { ServiceProvider } = require('@adonisjs/fold')

class JekkoProvider extends ServiceProvider {
register () {
this.app.bind('Poowf/Middleware/Jekko', (app) => {
const Jekko = require('../src/Jekko')
return new Jekko()
})
}
register () {
this.app.bind('Poowf/Middleware/Jekko', (app) => {
const Jekko = require('../src/Jekko')
return new Jekko()
})
}
}

module.exports = JekkoProvider
153 changes: 94 additions & 59 deletions src/Jekko/index.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,117 @@
'use strict'

const Route = use('Route')
const Logger = use('Logger')

class Jekko {
constructor () {
}

constructor () {
}
shareIsActiveRouteViewGlobal (request, view) {
view.share({
request,
isActiveRoute: function (route, output = 'active') {
if(route.includes("*"))
{
let filtered = Jekko.getFilteredRoutes(route)
let urls = []
for(let route of filtered)
{
urls.push(Route.url(route.name))
}

shareIsActiveRouteViewGlobal (request, view) {
view.share({
request,
isActiveRoute: function (route, output = 'active') {
return (Route.url(route) == request.url()) ? output : this.safe(``)
}
})
}
return (urls.includes(request.url())) ? this.safe(`${output}`) : this.safe(``)
}
else
{
return (Route.url(route) == request.url()) ? this.safe(`${output}`) : this.safe(``)
}
}
})
}

shareIsActiveURLViewGlobal (request, view) {
view.share({
request,
isActiveURL: function (url, output = 'active') {
return (url == request.url()) ? this.safe(`${output}`) : this.safe(``)
}
})
}
shareIsActiveURLViewGlobal (request, view) {
view.share({
request,
isActiveURL: function (url, output = 'active') {
return (url == request.url()) ? this.safe(`${output}`) : this.safe(``)
}
})
}

shareIsActiveMatchViewGlobal (request, view) {
view.share({
request,
isActiveMatch: function (string, output = 'active') {
return ((request.url()).includes(string)) ? this.safe(`${output}`) : this.safe(``)
}
})
}
shareIsActiveMatchViewGlobal (request, view) {
view.share({
request,
isActiveMatch: function (string, output = 'active') {
return ((request.url()).includes(string)) ? this.safe(`${output}`) : this.safe(``)
}
})
}

shareAreActiveRoutesViewGlobal (request, view) {
view.share({
request,
areActiveRoutes: function (routes, output = 'active') {
for (let route of routes)
{
if(Route.url(route) == request.url())
shareAreActiveRoutesViewGlobal (request, view) {
view.share({
request,
areActiveRoutes: function (routes, output = 'active') {
for (let route of routes)
{
return this.safe(`${output}`)
if(route.includes("*"))
{
let filtered = Jekko.getFilteredRoutes(route)
let urls = []
for(let route of filtered)
{
urls.push(Route.url(route.name))
}

if(urls.includes(request.url()))
{
return this.safe(`${output}`)
}
}
else if(Route.url(route) == request.url())
{
return this.safe(`${output}`)
}
}
return this.safe(``)
}
})
}

return this.safe(``)
}
})
}

shareAreActiveURLsViewGlobal (request, view) {
view.share({
request,
areActiveURLs: function (urls, output = 'active') {
for (let url of urls)
{
if(url == request.url())
shareAreActiveURLsViewGlobal (request, view) {
view.share({
request,
areActiveURLs: function (urls, output = 'active') {
for (let url of urls)
{
return this.safe(`${output}`)
if(url == request.url())
{
return this.safe(`${output}`)
}
}

return this.safe(``)
}
})
}

static getFilteredRoutes(route) {
let regex = "^" + route.replace(".*", "\\.[^.]*?")
let routes = Route.list()
let filtered = routes.filter(({name}) => name.match(regex))

return this.safe(``)
}
})
}
return filtered
}

async handle({ request, view }, next) {
this.shareIsActiveRouteViewGlobal(request, view)
this.shareIsActiveURLViewGlobal(request, view)
this.shareIsActiveMatchViewGlobal(request, view)
this.shareAreActiveRoutesViewGlobal(request, view)
this.shareAreActiveURLsViewGlobal(request, view)
async handle({ request, view }, next) {
this.shareIsActiveRouteViewGlobal(request, view)
this.shareIsActiveURLViewGlobal(request, view)
this.shareIsActiveMatchViewGlobal(request, view)
this.shareAreActiveRoutesViewGlobal(request, view)
this.shareAreActiveURLsViewGlobal(request, view)

await next()
}
await next()
}
}

module.exports = Jekko

0 comments on commit e5617fa

Please sign in to comment.