diff --git a/LICENSE.md b/LICENSE.md index 3a7b6c7..0de1993 100755 --- a/LICENSE.md +++ b/LICENSE.md @@ -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: diff --git a/README.md b/README.md index a3367a3..e2c80cb 100755 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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. @@ -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. @@ -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: diff --git a/package.json b/package.json index dc0a398..a1afee9 100755 --- a/package.json +++ b/package.json @@ -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": { diff --git a/providers/JekkoProvider.js b/providers/JekkoProvider.js index d8b560a..4f06be8 100755 --- a/providers/JekkoProvider.js +++ b/providers/JekkoProvider.js @@ -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 \ No newline at end of file diff --git a/src/Jekko/index.js b/src/Jekko/index.js index 62e58c5..fe8018a 100755 --- a/src/Jekko/index.js +++ b/src/Jekko/index.js @@ -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 \ No newline at end of file