├── .babelrc ├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .npmignore ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── config ├── .eslintrc ├── banner.js ├── build.js ├── bundle.js ├── entry.js └── example.js ├── dist ├── README.md ├── vue-laroute.common.js ├── vue-laroute.esm.js ├── vue-laroute.js └── vue-laroute.min.js ├── example ├── example.js └── index.html ├── package-lock.json ├── package.json └── src ├── example.js ├── index.js └── laroute.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"], 3 | "plugins": ["babel-plugin-espower"], 4 | "env": { 5 | "test": { 6 | "plugins": ["istanbul"], 7 | "presets": ["power-assert"] 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | config/*.js 2 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "extends": [ 4 | "plugin:vue-libs/recommended" 5 | ], 6 | "rules": { 7 | "object-curly-spacing": ["error", "always"], 8 | "no-multiple-empty-lines": ["error", { "max": 2, "maxBOF": 1 }], 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 29 | 30 | 31 | ### vue & vue-laroute version 32 | 2.0.x, x.y.z 33 | 34 | ### Reproduction Link 35 | 36 | 37 | ### Steps to reproduce 38 | 39 | ### What is Expected? 40 | 41 | ### What is actually happening? 42 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samturrell/vue-laroute/2fded775457989b2b6ee98a216be9f7073550843/.github/PULL_REQUEST_TEMPLATE.md -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib 2 | coverage 3 | dist/*.gz 4 | docs/_book 5 | test/e2e/report 6 | test/e2e/screenshots 7 | node_modules 8 | .DS_Store 9 | *.log 10 | *.swp 11 | *~ 12 | /.idea/ 13 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .* 2 | *.log 3 | *.swp 4 | *.yml 5 | coverage 6 | docs/_book 7 | config 8 | dist/*.map 9 | lib 10 | test 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samturrell/vue-laroute/2fded775457989b2b6ee98a216be9f7073550843/CHANGELOG.md -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # vue-laroute Contributing Guide 2 | 3 | - [Issue Reporting Guidelines](#issue-reporting-guidelines) 4 | - [Pull Request Guidelines](#pull-request-guidelines) 5 | - [Development Setup](#development-setup) 6 | 7 | ## Issue Reporting Guidelines 8 | 9 | - The issue list of this repo is **exclusively** for bug reports and feature requests. Non-conforming issues will be closed immediately. 10 | 11 | - For simple beginner questions, you can get quick answers from [The Gitter chat room](https://gitter.im/vuejs/vue). 12 | 13 | - For more complicated questions, you can use [the official forum](http://forum.vuejs.org/) or StackOverflow. Make sure to provide enough information when asking your questions - this makes it easier for others to help you! 14 | 15 | - Try to search for your issue, it may have already been answered or even fixed in the development branch. 16 | 17 | - Check if the issue is reproducible with the latest stable version of Vue. If you are using a pre-release, please indicate the specific version you are using. 18 | 19 | - It is **required** that you clearly describe the steps necessary to reproduce the issue you are running into. Issues with no clear repro steps will not be triaged. If an issue labeled "need repro" receives no further input from the issue author for more than 5 days, it will be closed. 20 | 21 | - It is recommended that you make a JSFiddle/JSBin/Codepen to demonstrate your issue. You could start with [this template](http://jsfiddle.net/5sH6A/) that already includes the latest version of Vue. 22 | 23 | - For bugs that involves build setups, you can create a reproduction repository with steps in the README. 24 | 25 | - If your issue is resolved but still open, don’t hesitate to close it. In case you found a solution by yourself, it could be helpful to explain how you fixed it. 26 | 27 | ## Pull Request Guidelines 28 | 29 | - The `master` branch is basically just a snapshot of the latest stable release. All development should be done in dedicated branches. **Do not submit PRs against the `master` branch.** 30 | 31 | - Checkout a topic branch from the relevant branch, e.g. `master`, and merge back against that branch. 32 | 33 | - Work in the `src` folder and **DO NOT** checkin `dist` in the commits. 34 | 35 | - It's OK to have multiple small commits as you work on the PR - we will let GitHub automatically squash it before merging. 36 | 37 | - Make sure `npm test` passes. (see [development setup](#development-setup)) 38 | 39 | - If adding new feature: 40 | - Add accompanying test case. 41 | - Provide convincing reason to add this feature. Ideally you should open a suggestion issue first and have it greenlighted before working on it. 42 | 43 | - If fixing a bug: 44 | - Provide detailed description of the bug in the PR. Live demo preferred. 45 | - Add appropriate test coverage if applicable. 46 | 47 | ### Work Step Example 48 | - Fork the repository from [samturrell/vue-laroute](https://github.com/samturrell/vue-laroute) ! 49 | - Create your topic branch from `master`: `git branch my-new-topic origin/master` 50 | - Add codes and pass tests ! 51 | - Commit your changes: `git commit -am 'Add some topic'` 52 | - Push to the branch: `git push origin my-new-topic` 53 | - Submit a pull request to `master` branch of `samturrell/vue-laroute` repository ! 54 | 55 | ## Development Setup 56 | 57 | You will need [Node.js](http://nodejs.org). 58 | 59 | After cloning the repo, run: 60 | 61 | $ npm install 62 | 63 | ### Commonly used NPM scripts 64 | 65 | # watch and serve with hot reload unit test at localhost:8080 66 | $ npm run dev 67 | 68 | # lint source codes 69 | $ npm run lint 70 | 71 | # run unit tests in browser (firefox/safari/chrome) 72 | $ npm run test:unit 73 | 74 | # build all dist files, including npm packages 75 | $ npm run build 76 | 77 | # run the full test suite, include linting 78 | $ npm test 79 | 80 | There are some other scripts available in the `scripts` section of the `package.json` file. 81 | 82 | The default test script will do the following: lint with ESLint -> unit tests with coverage. **Please make sure to have this pass successfully before submitting a PR.** Although the same tests will be run against your PR on the CI server, it is better to have it working locally beforehand. 83 | 84 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Sam Turrell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vue-laroute 2 | 3 | [![npm](https://img.shields.io/npm/v/vue-laroute.svg)](https://www.npmjs.com/package/vue-laroute) 4 | [![vue2](https://img.shields.io/badge/vue-2.x-brightgreen.svg)](https://vuejs.org/) 5 | 6 | Inject Laravel routes into your Vue application via [aaronlord/laroute](https://github.com/aaronlord/laroute). I actually recommend the alternative and more slim-lined version of this package from [AXN-Informatique/laravel-laroute](https://github.com/AXN-Informatique/laravel-laroute). 7 | 8 | [DEMO](https://samturrell.github.io/vue-laroute/example) 9 | 10 | ## Using this plugin 11 | 12 | Adding vue-laroute to your application is as simple as any other plugin: 13 | 14 | ```js 15 | import Vue from 'vue'; 16 | 17 | import VueLaroute from 'vue-laroute'; 18 | import routes from '../path/to/laroute.js'; 19 | 20 | Vue.use(VueLaroute, { 21 | routes, 22 | accessor: '$routes', // Optional: the global variable for accessing the router 23 | }); 24 | 25 | new Vue({ 26 | el: '#app', 27 | }); 28 | ``` 29 | 30 | You can now access your global accessor (`$routes` by default) in any component via `this.$routes`, for example: 31 | 32 | ```js 33 | 57 | 58 | 70 | ``` 71 | 72 | 73 | 74 | ## :scroll: Changelog 75 | Details changes for each release are documented in the [CHANGELOG.md](https://github.com/samturrell/vue-laroute/blob/master/CHANGELOG.md). 76 | 77 | 78 | ## :exclamation: Issues 79 | Please make sure to read the [Issue Reporting Checklist](https://github.com/samturrell/vue-laroute/blob/master/CONTRIBUTING.md#issue-reporting-guidelines) before opening an issue. Issues not conforming to the guidelines may be closed immediately. 80 | 81 | 82 | ## :muscle: Contribution 83 | Please make sure to read the [Contributing Guide](https://github.com/samturrell/vue-laroute/blob/master/CONTRIBUTING.md) before making a pull request. 84 | 85 | ## :copyright: License 86 | 87 | [MIT](http://opensource.org/licenses/MIT) 88 | -------------------------------------------------------------------------------- /config/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "globals": { 3 | "process": true 4 | }, 5 | "extends": "vue", 6 | "rules": { 7 | "no-multiple-empty-lines": [2, {"max": 2}], 8 | "no-console": 0 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /config/banner.js: -------------------------------------------------------------------------------- 1 | const pack = require('../package.json') 2 | const version = process.env.VERSION || pack.version 3 | 4 | module.exports = 5 | '/*!\n' + 6 | ` * ${pack.name} v${version} \n` + 7 | ` * (c) ${new Date().getFullYear()} ${pack.author.name}\n` + 8 | ` * Released under the ${pack.license} License.\n` + 9 | ' */' 10 | -------------------------------------------------------------------------------- /config/build.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const exist = fs.existsSync 3 | const mkdir = fs.mkdirSync 4 | const getAllEntries = require('./entry').getAllEntries 5 | const build = require('./bundle') 6 | 7 | if (!exist('dist')) { 8 | mkdir('dist') 9 | } 10 | 11 | let entries = getAllEntries() 12 | 13 | // filter entries via command line arg 14 | if (process.argv[2]) { 15 | const filters = process.argv[2].split(',') 16 | entries = entries.filter(b => { 17 | return filters.some(f => b.dest.indexOf(f) > -1) 18 | }) 19 | } 20 | 21 | build(entries) 22 | -------------------------------------------------------------------------------- /config/bundle.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const readFile = fs.readFile 3 | const writeFile = fs.writeFile 4 | const relative = require('path').relative 5 | const gzip = require('zlib').gzip 6 | const rollup = require('rollup') 7 | const uglify = require('uglify-js') 8 | 9 | module.exports = build 10 | 11 | function build (entries) { 12 | let built = 0 13 | const total = entries.length 14 | const next = () => { 15 | buildEntry(entries[built]).then(() => { 16 | built++ 17 | if (built < total) { 18 | next() 19 | } 20 | }).catch(logError) 21 | } 22 | next() 23 | } 24 | 25 | function buildEntry (config) { 26 | const isProd = /min\.js$/.test(config.dest) 27 | return rollup.rollup(config).then(bundle => { 28 | const code = bundle.generate(config).code 29 | if (isProd) { 30 | var minified = (config.banner ? config.banner + '\n' : '') + uglify.minify(code, { 31 | fromString: true, 32 | output: { 33 | screw_ie8: true, 34 | ascii_only: true 35 | }, 36 | compress: { 37 | pure_funcs: ['makeMap'] 38 | } 39 | }).code 40 | return write(config.dest, minified).then(zip(config.dest)) 41 | } else { 42 | return write(config.dest, code) 43 | } 44 | }) 45 | } 46 | 47 | function write (dest, code) { 48 | return new Promise(function (resolve, reject) { 49 | writeFile(dest, code, function (err) { 50 | if (err) { return reject(err) } 51 | console.log(blue(relative(process.cwd(), dest)) + ' ' + getSize(code)) 52 | resolve() 53 | }) 54 | }) 55 | } 56 | 57 | function zip (file) { 58 | return function () { 59 | return new Promise(function (resolve, reject) { 60 | readFile(file, function (err, buf) { 61 | if (err) { return reject(err) } 62 | gzip(buf, function (err, buf) { 63 | if (err) { return reject(err) } 64 | write(file + '.gz', buf).then(resolve) 65 | }) 66 | }) 67 | }) 68 | } 69 | } 70 | 71 | function getSize (code) { 72 | return (code.length / 1024).toFixed(2) + 'kb' 73 | } 74 | 75 | function logError (e) { 76 | console.log(e) 77 | } 78 | 79 | function blue (str) { 80 | return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m' 81 | } 82 | -------------------------------------------------------------------------------- /config/entry.js: -------------------------------------------------------------------------------- 1 | const replace = require('rollup-plugin-replace') 2 | const buble = require('rollup-plugin-buble') 3 | const banner = require('./banner') 4 | const pack = require('../package.json') 5 | 6 | function toUpper (_, c) { 7 | return c ? c.toUpperCase() : '' 8 | } 9 | 10 | const classifyRE = /(?:^|[-_\/])(\w)/g 11 | function classify (str) { 12 | return str.replace(classifyRE, toUpper) 13 | } 14 | const moduleName = classify(pack.name) 15 | 16 | const entries = { 17 | commonjs: { 18 | entry: 'src/index.js', 19 | dest: `dist/${pack.name}.common.js`, 20 | format: 'cjs', 21 | banner 22 | }, 23 | esm: { 24 | entry: 'src/index.js', 25 | dest: `dist/${pack.name}.esm.js`, 26 | format: 'es', 27 | banner 28 | }, 29 | production: { 30 | entry: 'src/index.js', 31 | dest: `dist/${pack.name}.min.js`, 32 | format: 'umd', 33 | env: 'production', 34 | moduleName, 35 | banner 36 | }, 37 | development: { 38 | entry: 'src/index.js', 39 | dest: `dist/${pack.name}.js`, 40 | format: 'umd', 41 | env: 'development', 42 | moduleName, 43 | banner 44 | } 45 | } 46 | 47 | function genConfig (opts) { 48 | const config = { 49 | entry: opts.entry, 50 | dest: opts.dest, 51 | format: opts.format, 52 | banner: opts.banner, 53 | moduleName, 54 | plugins: [ 55 | buble() 56 | ] 57 | } 58 | 59 | const replacePluginOptions = { '__VERSION__': pack.version } 60 | if (opts.env) { 61 | replacePluginOptions['process.env.NODE_ENV'] = JSON.stringify(opts.env) 62 | } 63 | config.plugins.push(replace(replacePluginOptions)) 64 | 65 | return config 66 | } 67 | 68 | exports.getEntry = name => genConfig(entries[name]) 69 | exports.getAllEntries = () => Object.keys(entries).map(name => genConfig(entries[name])) 70 | -------------------------------------------------------------------------------- /config/example.js: -------------------------------------------------------------------------------- 1 | let path = require('path'); 2 | 3 | module.exports = { 4 | entry: path.resolve(__dirname, '../src/example.js'), 5 | output: { 6 | path: path.resolve(__dirname, '../example'), 7 | filename: 'example.js' 8 | }, 9 | resolve: { 10 | alias: { 11 | vue: 'vue/dist/vue.js' 12 | } 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /dist/README.md: -------------------------------------------------------------------------------- 1 | ## Explanation of Build Files 2 | 3 | - UMD: vue-laroute.js 4 | - CommonJS: vue-laroute.common.js 5 | - ES Module: vue-laroute.esm.js 6 | 7 | ### Terms 8 | 9 | - **[UMD](https://github.com/umdjs/umd)**: UMD builds can be used directly in the browser via a ` 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-laroute", 3 | "description": "Inject laravel routes into your vue application via laroute", 4 | "version": "0.2.0", 5 | "author": { 6 | "name": "Sam Turrell", 7 | "email": "sam.turrell@netsells.co.uk" 8 | }, 9 | "bugs": { 10 | "url": "https://github.com/samturrell/vue-laroute/issues" 11 | }, 12 | "devDependencies": { 13 | "babel-core": "^6.22.1", 14 | "babel-eslint": "^7.1.0", 15 | "babel-loader": "^6.2.10", 16 | "babel-plugin-istanbul": "^3.1.2", 17 | "babel-polyfill": "6.22.0", 18 | "babel-preset-es2015": "^6.22.0", 19 | "babel-preset-power-assert": "^1.0.0", 20 | "buble": "^0.14.0", 21 | "cross-env": "^5.0.5", 22 | "eslint": "^3.14.1", 23 | "eslint-plugin-vue-libs": "^1.2.0", 24 | "eslint-loader": "^1.6.1", 25 | "html-webpack-plugin": "^2.19.0", 26 | "mocha": "^3.2.0", 27 | "mocha-loader": "^1.1.1", 28 | "karma": "^1.4.1", 29 | "karma-chrome-launcher": "^2.1.1", 30 | "karma-coverage": "^1.1.1", 31 | "karma-firefox-launcher": "^1.0.0", 32 | "karma-mocha": "^1.3.0", 33 | "karma-mocha-reporter": "^2.2.2", 34 | "karma-safari-launcher": "^1.0.0", 35 | "karma-sourcemap-loader": "^0.3.7", 36 | "karma-webpack": "^2.0.2", 37 | "power-assert": "^1.4.2", 38 | "rollup": "^0.36.4", 39 | "rollup-plugin-buble": "^0.14.0", 40 | "rollup-plugin-replace": "^1.1.1", 41 | "uglify-js": "^2.7.5", 42 | "vue": "^2.1.10", 43 | "webpack": "^2.2.0", 44 | "webpack-dev-server": "^2.2.1" 45 | }, 46 | "files": [ 47 | "dist/vue-laroute.js", 48 | "dist/vue-laroute.min.js", 49 | "dist/vue-laroute.common.js", 50 | "src" 51 | ], 52 | "homepage": "https://github.com/samturrell/vue-laroute#readme", 53 | "main": "dist/vue-laroute.common.js", 54 | "module": "dist/vue-laroute.esm.js", 55 | "unpkg": "dist/vue-laroute.js", 56 | "keywords": [ 57 | "plugin", 58 | "vue", 59 | "vuejs" 60 | ], 61 | "license": "MIT", 62 | "engines": { 63 | "node": ">= 6.0" 64 | }, 65 | "repository": { 66 | "type": "git", 67 | "url": "git+https://github.com/samturrell/vue-laroute.git" 68 | }, 69 | "scripts": { 70 | "build": "node config/build.js", 71 | "clean": "rm -rf coverage && rm -rf dist/*.js* && rm ./*.log", 72 | "dev": "cross-env BABEL_ENV=test webpack-dev-server --inline --hot --open --content-base ./test/unit/ --config config/webpack.dev.conf.js", 73 | "lint": "eslint src test config", 74 | "example": "npm run build && webpack --config=./config/example.js" 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/example.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import VueLaroute from './index'; 3 | import routes from './laroute'; 4 | 5 | Vue.use(VueLaroute, { 6 | routes, 7 | }) 8 | 9 | new Vue({ 10 | el: '#app' 11 | }) 12 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | function plugin (Vue, options = {}) { 2 | options = Object.assign({ 3 | accessor: '$routes', 4 | routes: (typeof window !== 'undefined' && window.laroute) 5 | ? window.laroute 6 | : {} 7 | }, options) 8 | 9 | Vue.prototype[options.accessor] = options.routes 10 | } 11 | 12 | plugin.version = '__VERSION__' 13 | 14 | export default plugin 15 | 16 | if (typeof window !== 'undefined' && window.Vue && window.laroute) { 17 | window.Vue.use(plugin) 18 | } 19 | -------------------------------------------------------------------------------- /src/laroute.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 3 | var laroute = (function () { 4 | 5 | var routes = { 6 | 7 | absolute: false, 8 | rootUrl: 'http://localhost', 9 | routes : [ 10 | { 11 | "uri": "api\/user", 12 | "name": null 13 | }, 14 | { 15 | "uri": "\/", 16 | "name": "home" 17 | }, 18 | { 19 | "uri": "products", 20 | "name": "products.index" 21 | }, 22 | { 23 | "uri": "products\/{id}", 24 | "name": "products.show" 25 | }, 26 | { 27 | "uri": "products\/{id}\/purchase", 28 | "name": "products.purchase" 29 | }, 30 | { 31 | "uri": "contact", 32 | "name": "contact" 33 | } 34 | ], 35 | prefix: '', 36 | 37 | route : function (name, parameters, route) { 38 | route = route || this.getByName(name); 39 | 40 | if ( ! route ) { 41 | return undefined; 42 | } 43 | 44 | return this.toRoute(route, parameters); 45 | }, 46 | 47 | url: function (url, parameters) { 48 | parameters = parameters || []; 49 | 50 | var uri = url + '/' + parameters.join('/'); 51 | 52 | return this.getCorrectUrl(uri); 53 | }, 54 | 55 | toRoute : function (route, parameters) { 56 | var uri = this.replaceNamedParameters(route.uri, parameters); 57 | var qs = this.getRouteQueryString(parameters); 58 | 59 | if (this.absolute && this.isOtherHost(route)){ 60 | return "//" + route.host + "/" + uri + qs; 61 | } 62 | 63 | return this.getCorrectUrl(uri + qs); 64 | }, 65 | 66 | isOtherHost: function (route){ 67 | return route.host && route.host != window.location.hostname; 68 | }, 69 | 70 | replaceNamedParameters : function (uri, parameters) { 71 | uri = uri.replace(/\{(.*?)\??\}/g, function(match, key) { 72 | if (parameters.hasOwnProperty(key)) { 73 | var value = parameters[key]; 74 | delete parameters[key]; 75 | return value; 76 | } else { 77 | return match; 78 | } 79 | }); 80 | 81 | // Strip out any optional parameters that were not given 82 | uri = uri.replace(/\/\{.*?\?\}/g, ''); 83 | 84 | return uri; 85 | }, 86 | 87 | getRouteQueryString : function (parameters) { 88 | var qs = []; 89 | for (var key in parameters) { 90 | if (parameters.hasOwnProperty(key)) { 91 | qs.push(key + '=' + parameters[key]); 92 | } 93 | } 94 | 95 | if (qs.length < 1) { 96 | return ''; 97 | } 98 | 99 | return '?' + qs.join('&'); 100 | }, 101 | 102 | getByName : function (name) { 103 | for (var key in this.routes) { 104 | if (this.routes.hasOwnProperty(key) && this.routes[key].name === name) { 105 | return this.routes[key]; 106 | } 107 | } 108 | }, 109 | 110 | getByAction : function(action) { 111 | for (var key in this.routes) { 112 | if (this.routes.hasOwnProperty(key) && this.routes[key].action === action) { 113 | return this.routes[key]; 114 | } 115 | } 116 | }, 117 | 118 | getCorrectUrl: function (uri) { 119 | var url = this.prefix + '/' + uri.replace(/^\/?/, ''); 120 | 121 | if ( ! this.absolute) { 122 | return url; 123 | } 124 | 125 | return this.rootUrl.replace('/\/?$/', '') + url; 126 | } 127 | }; 128 | 129 | var getLinkAttributes = function(attributes) { 130 | if ( ! attributes) { 131 | return ''; 132 | } 133 | 134 | var attrs = []; 135 | for (var key in attributes) { 136 | if (attributes.hasOwnProperty(key)) { 137 | attrs.push(key + '="' + attributes[key] + '"'); 138 | } 139 | } 140 | 141 | return attrs.join(' '); 142 | }; 143 | 144 | var getHtmlLink = function (url, title, attributes) { 145 | title = title || url; 146 | attributes = getLinkAttributes(attributes); 147 | 148 | return '' + title + ''; 149 | }; 150 | 151 | return { 152 | // Generate a url for a given controller action. 153 | // laroute.action('HomeController@getIndex', [params = {}]) 154 | action : function (name, parameters) { 155 | parameters = parameters || {}; 156 | 157 | return routes.route(name, parameters, routes.getByAction(name)); 158 | }, 159 | 160 | // Generate a url for a given named route. 161 | // laroute.route('routeName', [params = {}]) 162 | route : function (route, parameters) { 163 | parameters = parameters || {}; 164 | 165 | return routes.route(route, parameters); 166 | }, 167 | 168 | // Generate a fully qualified URL to the given path. 169 | // laroute.route('url', [params = {}]) 170 | url : function (route, parameters) { 171 | parameters = parameters || {}; 172 | 173 | return routes.url(route, parameters); 174 | }, 175 | 176 | // Generate a html link to the given url. 177 | // laroute.link_to('foo/bar', [title = url], [attributes = {}]) 178 | link_to : function (url, title, attributes) { 179 | url = this.url(url); 180 | 181 | return getHtmlLink(url, title, attributes); 182 | }, 183 | 184 | // Generate a html link to the given route. 185 | // laroute.link_to_route('route.name', [title=url], [parameters = {}], [attributes = {}]) 186 | link_to_route : function (route, title, parameters, attributes) { 187 | var url = this.route(route, parameters); 188 | 189 | return getHtmlLink(url, title, attributes); 190 | }, 191 | 192 | // Generate a html link to the given controller action. 193 | // laroute.link_to_action('HomeController@getIndex', [title=url], [parameters = {}], [attributes = {}]) 194 | link_to_action : function(action, title, parameters, attributes) { 195 | var url = this.action(action, parameters); 196 | 197 | return getHtmlLink(url, title, attributes); 198 | } 199 | 200 | }; 201 | 202 | }).call(this); 203 | 204 | /** 205 | * Expose the class either via AMD, CommonJS or the global object 206 | */ 207 | if (typeof define === 'function' && define.amd) { 208 | define(function () { 209 | return laroute; 210 | }); 211 | } 212 | else if (typeof module === 'object' && module.exports){ 213 | module.exports = laroute; 214 | } 215 | else { 216 | window.laroute = laroute; 217 | } 218 | 219 | }).call(this); 220 | 221 | --------------------------------------------------------------------------------