UNPKG

api-console-assets

Version:

This repo only exists to publish api console components to npm

179 lines (166 loc) 4.68 kB
<!-- @license Copyright 2016 The Advanced REST client authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <link rel="import" href="../polymer/polymer.html"> <!-- The `<raml-main-entry-lookup>` is an element that lookup for a main RAML file in the list of passed files. List of files should be either result of reading multiple files from file imput or result of use of the `web-unzip` element. Directories from the file input will be ignored due the missing API to read it's contents. ### Example ```html <raml-main-entry-lookup files="[[files]]" entry="{{ramlEntry}}" on-entry="_entryPointFound"></raml-main-entry-lookup> ``` ```javascript this._entryPointFound: function(e) { var file = e.detail.entry; if (!file) { return this.notifyLackOfEntryPoint(); } if (file instanceof Array) { this.askUserAboutMainFile(file); } else { this.processRAML(file); } } ``` @group RAML Elements @element raml-main-entry-lookup @demo demo/index.html --> <script> Polymer({ is: 'raml-main-entry-lookup', /** * Fired when the entry was found. * * @event entry * @param {FileEntry} file Can be FileEntry, Array of file entries or null * if not found at all. */ properties: { candidates: { type: Array, value: function() { return []; }, readOnly: true }, // Array of File, or zip.js Entry objects files: Array, // Found entry. If this is an array more than one candidate was found. entry: { type: Object, notify: true } }, observers: [ '_filesChanged(files)' ], _filesChanged: function() { if (!this.files) { return; } this.async(function() { this.lookup(this.files); }, 1); }, /** * Lookup for main RAML file in a set of files. * * @param {Array} entries List of files. It can be either FileList or Array from the `web-unzip` * */ lookup: function(entries) { if (!entries) { return Promise.reject(new Error('Entries not set')); } if (!(entries instanceof Array) && !(entries instanceof FileList)) { this.fire('entry', {entry: entries}); return; } if (entries.length === 1) { this.fire('entry', {entry: entries[0]}); return; } var candidates = []; for (var i = 0; i < entries.length; i++) { var entry = entries[i]; var name = entry.name; if (name.substr(name.lastIndexOf('.') + 1) === 'raml') { var path = entry.filename || entry.name; if (!path) { path = '/' + name; } var obj = { name: name, entry: entry, path: entry.filename ? entry.filename.substr(0, entry.filename.lastIndexOf(name)) : '/' }; candidates.push(obj); } } if (candidates.length === 1) { candidates = candidates[0]; } else if (candidates.length === 0) { candidates = null; } else { candidates = this.processResults(candidates); } this.set('entry', candidates); this.fire('entry', {entry: candidates}); return candidates; }, /** * Lookup for a RAML files that are put on top of the directory structure. * @param {Array<File>} candidates A list of RAML files in the structure. * @return {File|Array<File>} A file if only one candidate was found and * list of files if more than one file was found. */ processResults: function(candidates) { if (!candidates || !candidates.length) { return null; } if (candidates.length === 1) { return candidates[0]; } // find a main file in the root dir var entryPoints = []; var minLen = -1; candidates.forEach(function(item) { var len = item.path.length; if (minLen === -1) { minLen = len; entryPoints = [item]; } else if (len < minLen) { minLen = len; entryPoints = [item]; } else if (len === minLen) { // multiple candidates in current path. entryPoints.push(item); } }); if (entryPoints.length === 0) { return null; } if (entryPoints.length === 1) { return entryPoints[0]; } return entryPoints; } }); </script>