|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 The Advanced REST client authors <[email protected]> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | + * License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * The following script will handle API console routing when using the console as |
| 17 | + * a standalone application. |
| 18 | + * |
| 19 | + * It uses native JavaScript APIs so it can be used outside Polymer scope. |
| 20 | + * |
| 21 | + * @author Pawel Psztyc <[email protected]> |
| 22 | + */ |
| 23 | +(function() { |
| 24 | + 'use strict'; |
| 25 | + // API Console namespace. |
| 26 | + var apiconsole = {}; |
| 27 | + // Namespace for standalone application. |
| 28 | + apiconsole.app = {}; |
| 29 | + /** |
| 30 | + * Initialize event listeners for console's path and page properties and observers |
| 31 | + * router data change. |
| 32 | + */ |
| 33 | + apiconsole.app.init = function() { |
| 34 | + apiconsole.app.setInitialRouteData(); |
| 35 | + apiconsole.app.addParserListeners(); |
| 36 | + apiconsole.app.observeRouteEvents(); |
| 37 | + apiconsole.app.loadDefault(); |
| 38 | + }; |
| 39 | + |
| 40 | + apiconsole.app.setInitialRouteData = function() { |
| 41 | + // sets the initial path for routing from external source. |
| 42 | + // The API console sets default path to `summary` after RAML change. |
| 43 | + var location = document.querySelector('app-location'); |
| 44 | + var locationPath = location.path; |
| 45 | + if (!locationPath) { |
| 46 | + return; |
| 47 | + } |
| 48 | + var parsedPath = locationPath.replace(/\-/g, '.'); |
| 49 | + if (parsedPath[0] === '/') { |
| 50 | + parsedPath = parsedPath.substr(1); |
| 51 | + } |
| 52 | + var _route = parsedPath.split('/'); |
| 53 | + var page = _route[0]; |
| 54 | + var path = _route[1]; |
| 55 | + |
| 56 | + apiconsole.app.__initialPage = page; |
| 57 | + apiconsole.app.__initialPath = path; |
| 58 | + }; |
| 59 | + /** |
| 60 | + * Adds event listeres to elements that are related to RAML dataq parsing. |
| 61 | + */ |
| 62 | + apiconsole.app.addParserListeners = function() { |
| 63 | + document.querySelector('raml-js-parser') |
| 64 | + .addEventListener('api-parse-ready', function(e) { |
| 65 | + document.querySelector('raml-json-enhance') |
| 66 | + .json = e.detail.json.specification; |
| 67 | + }); |
| 68 | + |
| 69 | + document.getElementById('versionSelector') |
| 70 | + .addEventListener('change', function() { |
| 71 | + document.querySelector('raml-js-parser').loadApi(this.value); |
| 72 | + document.getElementById('loadingApi').active = true; |
| 73 | + }); |
| 74 | + |
| 75 | + window.addEventListener('raml-json-enhance-ready', function(e) { |
| 76 | + var apiConsole = document.querySelector('api-console'); |
| 77 | + apiConsole.raml = e.detail.json; |
| 78 | + document.getElementById('loadingApi').active = false; |
| 79 | + if (apiconsole.app.__initialPage && apiconsole.app.__initialPage !== apiConsole.page) { |
| 80 | + apiconsole.app.pageChanged(apiconsole.app.__initialPage); |
| 81 | + apiconsole.app.__initialPage = undefined; |
| 82 | + } |
| 83 | + if (apiconsole.app.__initialPath && apiconsole.app.__initialPath !== apiConsole.path) { |
| 84 | + apiconsole.app.pathChanged(apiconsole.app.__initialPath); |
| 85 | + apiconsole.app.__initialPath = undefined; |
| 86 | + } |
| 87 | + }); |
| 88 | + }; |
| 89 | + /** |
| 90 | + * Adds event listeres to elements that are related to the routing: |
| 91 | + * app-location, app-route and api-console. |
| 92 | + */ |
| 93 | + apiconsole.app.observeRouteEvents = function() { |
| 94 | + var apiConsole = document.querySelector('api-console'); |
| 95 | + var location = document.querySelector('app-location'); |
| 96 | + |
| 97 | + apiConsole.addEventListener('path-changed', apiconsole.app._pathChanged); |
| 98 | + apiConsole.addEventListener('page-changed', apiconsole.app._pageChanged); |
| 99 | + location.addEventListener('route-changed', apiconsole.app._routeChanged); |
| 100 | + }; |
| 101 | + // Event handler for the path change. |
| 102 | + apiconsole.app._pathChanged = function(e) { |
| 103 | + apiconsole.app.pathChanged(e.detail.value); |
| 104 | + }; |
| 105 | + // Called when path changed from the api-console. |
| 106 | + apiconsole.app.pathChanged = function(path) { |
| 107 | + if (!path) { |
| 108 | + return; |
| 109 | + } |
| 110 | + var location = document.querySelector('app-location'); |
| 111 | + var parsedPath = path.replace(/\./g, '-'); |
| 112 | + var newPath = '/docs/' + parsedPath; |
| 113 | + if (newPath !== location.path) { |
| 114 | + location.set('path', newPath); |
| 115 | + } |
| 116 | + }; |
| 117 | + // Event handler for the page change. |
| 118 | + apiconsole.app._pageChanged = function(e) { |
| 119 | + apiconsole.app.pageChanged(e.detail.value); |
| 120 | + }; |
| 121 | + // Called when page change. |
| 122 | + apiconsole.app.pageChanged = function(page) { |
| 123 | + var apiConsole = document.querySelector('api-console'); |
| 124 | + if (apiConsole.page !== page) { |
| 125 | + apiConsole.page = page; |
| 126 | + } |
| 127 | + }; |
| 128 | + // Event handler for the route change. |
| 129 | + apiconsole.app._routeChanged = function(e) { |
| 130 | + apiconsole.app.routeChanged(e.detail.value); |
| 131 | + }; |
| 132 | + // Updates api console path if different than curent URL |
| 133 | + apiconsole.app.routeChanged = function(route) { |
| 134 | + var locationPath = route.path; |
| 135 | + if (!locationPath || locationPath === '/') { |
| 136 | + document.querySelector('app-location').set('path', '/docs'); |
| 137 | + return; |
| 138 | + } |
| 139 | + var parsedPath = locationPath.replace(/\-/g, '.'); |
| 140 | + if (parsedPath[0] === '/') { |
| 141 | + parsedPath = parsedPath.substr(1); |
| 142 | + } |
| 143 | + var _route = parsedPath.split('/'); |
| 144 | + var page = _route[0]; |
| 145 | + var path = _route[1]; |
| 146 | + var apiConsole = document.querySelector('api-console'); |
| 147 | + if (apiConsole.page !== page) { |
| 148 | + apiConsole.page = page; |
| 149 | + } |
| 150 | + if (apiConsole.path !== path) { |
| 151 | + apiConsole.path = path; |
| 152 | + } |
| 153 | + }; |
| 154 | + /** |
| 155 | + * Reads page name and the path from location path. |
| 156 | + * |
| 157 | + * @param {String} locationPath Current path read from path change event or read fomr the |
| 158 | + * `app-location` element. |
| 159 | + */ |
| 160 | + apiconsole.app._readPagePath = function(locationPath) { |
| 161 | + var parsedPath = locationPath.replace(/\-/g, '.'); |
| 162 | + if (parsedPath[0] === '/') { |
| 163 | + parsedPath = parsedPath.substr(1); |
| 164 | + } |
| 165 | + var _route = parsedPath.split('/'); |
| 166 | + var page = _route[0]; |
| 167 | + var path = _route[1]; |
| 168 | + return { |
| 169 | + page: page, |
| 170 | + path: path |
| 171 | + }; |
| 172 | + }; |
| 173 | + |
| 174 | + apiconsole.app.loadDefault = function() { |
| 175 | + var url = document.getElementById('versionSelector').value; |
| 176 | + document.querySelector('raml-js-parser').loadApi(url); |
| 177 | + document.getElementById('loadingApi').active = true; |
| 178 | + }; |
| 179 | + |
| 180 | + // Notifys user when something went wrong... |
| 181 | + apiconsole.app.notifyInitError = function(message) { |
| 182 | + window.alert('Cannot initialize API console. ' + message); |
| 183 | + }; |
| 184 | + apiconsole.app.init(); |
| 185 | +})(); |
0 commit comments