From 3629e34a5ab3f75a0499446c5f492d11535643c5 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Mon, 6 Jun 2016 19:32:20 +0300 Subject: [PATCH] Continue migrating to TS --- build/tasks/build.js | 33 ++++++--- karma.conf.js | 6 +- lib/components/Redoc/redoc.ts | 7 +- .../RequestSamples/request-samples.ts | 4 +- lib/components/SchemaSample/schema-sample.js | 70 +++++++++++++++++++ lib/components/SchemaSample/schema-sample.ts | 8 ++- lib/index.js | 12 ++++ lib/index.ts | 7 -- lib/shared/components/DropDown/drop-down.ts | 3 +- lib/utils/pipes.ts | 4 -- package.json | 20 ++++-- system.config.js | 22 +++--- tsconfig.json | 7 +- typings/dropkick.d.ts | 7 -- typings/dropkickjs.d.ts | 4 ++ typings/index.d.ts | 1 - typings/json-schema-instantiator.d.ts | 6 -- typings/json-schema-ref-parser.d.ts | 4 +- typings/openapi-sampler.d.ts | 3 + 19 files changed, 156 insertions(+), 72 deletions(-) create mode 100644 lib/components/SchemaSample/schema-sample.js create mode 100644 lib/index.js delete mode 100644 lib/index.ts delete mode 100644 typings/dropkick.d.ts create mode 100644 typings/dropkickjs.d.ts delete mode 100644 typings/json-schema-instantiator.d.ts create mode 100644 typings/openapi-sampler.d.ts diff --git a/build/tasks/build.js b/build/tasks/build.js index 692041d9..4183af1d 100644 --- a/build/tasks/build.js +++ b/build/tasks/build.js @@ -21,8 +21,9 @@ gulp.task('build', function (callback) { return callback(); } return runSequence( - //'clean', - 'concatPrism', + 'clean', + 'tsc', + 'inlineTemplates', 'bundle', 'concatDeps', 'copyDebug', @@ -39,19 +40,32 @@ gulp.task('copyDebug', () => { gulp.task('rebuild', function(done) { return runSequence( + 'tsc', + 'inlineTemplates', 'bundle', 'concatDeps', + 'copyDebug', done ); }); +gulp.task('tsc', function() { + exec('tsc -p ./tsconfig.json'); +}); + gulp.task('inlineTemplates', ['sass'], function() { - return gulp.src(paths.source, { base: './' }) + return gulp.src('.tmp/lib/**/*.js', { base: './tmp' }) .pipe(replace(/'(.*?)\.css'/g, '\'$1.scss\'')) - .pipe(inlineNg2Template({ - base: '/', + .pipe(inlineNg2Template({ + base: './', useRelativePaths: true, - styleProcessor: compileSass + styleProcessor: compileSass, + customFilePath: function(ext, file) { + var cwd = process.cwd(); + var relative = path.relative(cwd, file); + relative = relative.substring('5'); + return path.join(cwd, relative); + } })) .pipe(gulp.dest(paths.tmp)); }); @@ -62,7 +76,7 @@ function compileSass(ext, file) { file = file.replace('../../shared/styles/share-link', 'lib/shared/styles/share-link'); file = file.replace('../JsonSchema/lib/components/JsonSchema/json-schema-common', 'lib/components/JsonSchema/json-schema-common'); file = file.replace('../../styles/variables', 'lib/shared/styles/variables'); - + return sassCopm.renderSync({data: file}).css; } @@ -97,7 +111,8 @@ gulp.task('concatDeps', ['concatPrism'], function() { }); gulp.task('bundle', function bundle(done) { - fs.existsSync('dist') || fs.mkdirSync('dist'); + mkdir('-p', 'dist'); + cp('lib/index.js', path.join(paths.tmp, paths.sourceEntryPoint)); var builder = new Builder('./', 'system.config.js'); builder @@ -141,7 +156,7 @@ gulp.task('concatPrism', function() { return gulp.src(prismFiles) .pipe(concat(path.join(paths.tmp, 'prismjs-bundle.js'))) - .pipe(gulp.dest('.')) + .pipe(gulp.dest('.')); }); // needs inlineTemplates run before to create .tmp/lib folder diff --git a/karma.conf.js b/karma.conf.js index a8dde48c..23e967df 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -8,7 +8,7 @@ module.exports = function (config) { regexPreprocessor: { rules: [ - [ /'\.(.*?)\.css'/g, '\'.tmp$1.css\''] + [ /'\.(.*?)\.scss'/g, '\'.tmp$1.css\''] ] }, babelPreprocessor: { @@ -58,13 +58,13 @@ module.exports = function (config) { config: 'system.config.js', loadFiles: ['tests/setup.js', 'tests/helpers.js', 'tests/unit/*.spec.js', 'lib/**/*.js'], serveFiles: ['tests/schemas/**/*.json','tests/schemas/**/*.yml', 'lib/**/*.html', - '.tmp/lib/**/*.json', '.tmp/*js', '.tmp/lib/**/*.css'], + '.tmp/lib/**/*.json', '.tmp/*js', '.tmp/lib/**/*.scss'], nocache: true }, proxies: { '/tests/': '/base/tests/', - '/lib/components/Redoc/redoc-initial-styles.css': '/base/.tmp/lib/components/Redoc/redoc-initial-styles.css', + '/lib/components/Redoc/redoc-initial-styles.scss': '/base/.tmp/lib/components/Redoc/redoc-initial-styles.scss', '/lib/version.json': '/base/.tmp/lib/version.json', '/lib/': '/base/lib/', '/jspm_packages/': '/base/jspm_packages/', diff --git a/lib/components/Redoc/redoc.ts b/lib/components/Redoc/redoc.ts index 9df657fe..7ceaeecd 100644 --- a/lib/components/Redoc/redoc.ts +++ b/lib/components/Redoc/redoc.ts @@ -17,9 +17,6 @@ import {SchemaManager} from '../../utils/SchemaManager'; import { OptionsService, RedocEventsService } from '../../services/index'; //import redocVersion from '../../version.js'; - -//import './redoc-initial-styles.css!css'; - var dom = new BrowserDomAdapter(); var _modeLocked = false; @@ -40,8 +37,8 @@ export class Redoc extends BaseComponent { private element: any; options: any; static appRef: ComponentRef; - - constructor(schemaMgr: SchemaManager, optionsMgr:OptionsService, elementRef:ElementRef, + + constructor(schemaMgr: SchemaManager, optionsMgr:OptionsService, elementRef:ElementRef, public events:RedocEventsService) { super(schemaMgr); this.element = elementRef.nativeElement; diff --git a/lib/components/RequestSamples/request-samples.ts b/lib/components/RequestSamples/request-samples.ts index 5efb66ad..c7631ace 100644 --- a/lib/components/RequestSamples/request-samples.ts +++ b/lib/components/RequestSamples/request-samples.ts @@ -27,9 +27,7 @@ export class RequestSamples extends BaseComponent { @ViewChildren(Tabs) childQuery:QueryList; constructor(schemaMgr:SchemaManager, public events:RedocEventsService) { super(schemaMgr); - this.childQuery.changes.subscribe(() => { - this.childTabs = this.childQuery.first; - }); + this.selectedLang = this.events.samplesLanguageChanged; } diff --git a/lib/components/SchemaSample/schema-sample.js b/lib/components/SchemaSample/schema-sample.js new file mode 100644 index 00000000..13c6c07a --- /dev/null +++ b/lib/components/SchemaSample/schema-sample.js @@ -0,0 +1,70 @@ +'use strict'; +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +var __metadata = (this && this.__metadata) || function (k, v) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); +}; +var core_1 = require('@angular/core'); +var OpenAPISampler = require('openapi-sampler'); +var base_1 = require('../base'); +var JsonFormatterPipe_1 = require('../../utils/JsonFormatterPipe'); +var SchemaSample = (function (_super) { + __extends(SchemaSample, _super); + function SchemaSample(schemaMgr, elementRef) { + _super.call(this, schemaMgr); + this.element = elementRef.nativeElement; + } + SchemaSample.prototype.init = function () { + this.data = {}; + var base = {}; + var sample; + if (this.componentSchema.schema) { + base = this.componentSchema; + this.componentSchema = this.componentSchema.schema; + } + if (base.examples && base.examples['application/json']) { + sample = base.examples['application/json']; + } + else { + this.dereference(this.componentSchema); + try { + sample = OpenAPISampler.sample(this.componentSchema); + } + catch (e) { + } + } + this.data.sample = sample; + this.element.addEventListener('click', function (event) { + var collapsed, target = event.target; + if (event.target.className === 'collapser') { + collapsed = target.parentNode.getElementsByClassName('collapsible')[0]; + if (collapsed.parentNode.classList.contains('collapsed')) { + collapsed.parentNode.classList.remove('collapsed'); + } + else { + collapsed.parentNode.classList.add('collapsed'); + } + } + }); + }; + SchemaSample = __decorate([ + base_1.RedocComponent({ + selector: 'schema-sample', + templateUrl: './schema-sample.html', + pipes: [JsonFormatterPipe_1.JsonFormatter], + styleUrls: ['./schema-sample.css'] + }), + __metadata('design:paramtypes', [base_1.SchemaManager, core_1.ElementRef]) + ], SchemaSample); + return SchemaSample; +}(base_1.BaseComponent)); +exports.SchemaSample = SchemaSample; diff --git a/lib/components/SchemaSample/schema-sample.ts b/lib/components/SchemaSample/schema-sample.ts index d9d6dedc..1910f350 100644 --- a/lib/components/SchemaSample/schema-sample.ts +++ b/lib/components/SchemaSample/schema-sample.ts @@ -2,7 +2,7 @@ import { ElementRef } from '@angular/core'; -import SchemaSampler from 'json-schema-instantiator'; +import * as OpenAPISampler from 'openapi-sampler'; import { RedocComponent, BaseComponent, SchemaManager } from '../base'; import { JsonFormatter } from '../../utils/JsonFormatterPipe'; @@ -37,7 +37,11 @@ export class SchemaSample extends BaseComponent { sample = base.examples['application/json']; } else { this.dereference(this.componentSchema); - sample = SchemaSampler.instantiate(this.componentSchema); + try { + sample = OpenAPISampler.sample(this.componentSchema); + } catch(e) { + // no sample available + } } this.data.sample = sample; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 00000000..88890747 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,12 @@ +'use strict'; +import 'dropkickjs/build/css/dropkick.css!css'; +import 'prismjs/themes/prism-dark.css!css'; +import 'hint.css/hint.base.css!css'; +import './components/redoc/redoc-initial-styles.css!css'; + +import { Redoc } from './components/index'; + +export var init = Redoc.init; + +window['Redoc'] = Redoc; +Redoc.autoInit(); diff --git a/lib/index.ts b/lib/index.ts deleted file mode 100644 index 47ca1293..00000000 --- a/lib/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; - -import {Redoc} from './components/index'; -export var init = Redoc.init; - -window['Redoc'] = Redoc; -Redoc.autoInit(); diff --git a/lib/shared/components/DropDown/drop-down.ts b/lib/shared/components/DropDown/drop-down.ts index 4798a3fe..ecac25b3 100644 --- a/lib/shared/components/DropDown/drop-down.ts +++ b/lib/shared/components/DropDown/drop-down.ts @@ -3,7 +3,6 @@ import {Component, EventEmitter, ElementRef, Output} from '@angular/core'; import {CORE_DIRECTIVES} from '@angular/common'; import DropKick from 'dropkickjs'; -//import 'Robdel12/DropKick/build/css/dropkick.css!css'; @Component({ selector: 'dropdown', @@ -24,7 +23,7 @@ export class DropDown { } ngAfterContentInit() { - //this.inst = new DropKick(this.elem.firstElementChild, {autoWidth: true}); + this.inst = new DropKick(this.elem.firstElementChild, {autoWidth: true}); } onChange(value) { diff --git a/lib/utils/pipes.ts b/lib/utils/pipes.ts index c5103c28..ef79be52 100644 --- a/lib/utils/pipes.ts +++ b/lib/utils/pipes.ts @@ -5,12 +5,8 @@ import {isString, stringify, isBlank} from '@angular/core/src/facade/lang'; import {BaseException} from '@angular/core/src/facade/exceptions'; import JsonPointer from './JsonPointer'; -//import '../../prismjs-bundle'; declare var Prism: any; -//import 'prismjs/themes/prism-dark.css!css'; -//import 'hint.css/hint.base.css!css'; - import marked from 'marked'; // in gfm mode marked doesn't parse #Heading (without space after #) as heading diff --git a/package.json b/package.json index f37422b1..19aeba93 100644 --- a/package.json +++ b/package.json @@ -38,14 +38,15 @@ "@angular/platform-browser": "npm:@angular/platform-browser@^2.0.0-rc.1", "@angular/platform-browser-dynamic": "npm:@angular/platform-browser-dynamic@^2.0.0-rc.1", "Robdel12/DropKick": "github:Robdel12/DropKick@^2.1.7", + "dropkickjs": "npm:dropkickjs@^2.1.8", "es6-shim": "github:es-shims/es6-shim@^0.33.6", "hint.css": "npm:hint.css@^2.2.1", "json": "github:systemjs/plugin-json@^0.1.0", "json-formatter-js": "npm:json-formatter-js@^0.2.0", "json-pointer": "npm:json-pointer@^0.3.0", - "json-schema-instantiator": "npm:json-schema-instantiator@^0.3.0", "json-schema-ref-parser": "npm:json-schema-ref-parser@^3.1.2", "marked": "npm:marked@^0.3.5", + "openapi-sampler": "npm:openapi-sampler@^0.1.2", "prismjs": "npm:prismjs@^1.3.0", "rxjs": "npm:rxjs@5.0.0-beta.6", "scrollparent": "npm:scrollparent@^0.1.0", @@ -55,15 +56,12 @@ "devDependencies": { "babel": "npm:babel-core@^5.8.34", "babel-runtime": "npm:babel-runtime@^5.8.24", - "clean-css": "npm:clean-css@^3.4.6", + "clean-css": "npm:clean-css@^3.4.17", "core-js": "npm:core-js@^1.2.6", "css": "github:systemjs/plugin-css@^0.1.18", "systemjs/plugin-json": "github:systemjs/plugin-json@^0.1.0" }, "overrides": { - "github:Robdel12/DropKick@2.1.7": { - "format": "global" - }, "npm:@angular/core@2.0.0-rc.1": { "main": "esm/index.js", "format": "esm" @@ -83,6 +81,13 @@ "npm:@angular/platform-browser@2.0.0-rc.1": { "main": "esm/index.js", "format": "esm" + }, + "npm:openapi-sampler@0.1.2": { + "main": "src/openapi-sampler", + "format": "esm" + }, + "npm:dropkickjs@2.1.8": { + "format": "global" } } }, @@ -100,7 +105,7 @@ "gulp-concat": "^2.6.0", "gulp-eslint": "^1.1.1", "gulp-if": "^2.0.1", - "gulp-inline-ng2-template": "^1.1.2", + "gulp-inline-ng2-template": "^1.1.5", "gulp-protractor": "^2.1.0", "gulp-rename": "^1.2.2", "gulp-replace": "^0.5.4", @@ -111,7 +116,7 @@ "jasmine-core": "^2.4.1", "jasmine-spec-reporter": "^2.4.0", "json-schema-instantiator": "^0.3.0", - "jspm": "^0.16.34", + "jspm": "^0.16.36", "karma": "^0.13.15", "karma-babel-preprocessor": "^5.2.2", "karma-chrome-launcher": "^0.2.2", @@ -126,6 +131,7 @@ "karma-should": "^1.0.0", "karma-sinon": "^1.0.4", "node-sass": "^3.7.0", + "openapi-sampler": "^0.1.2", "phantomjs-prebuilt": "^2.1.7", "protractor": "^3.0.0", "reflect-metadata": "^0.1.2", diff --git a/system.config.js b/system.config.js index 19d30682..8db71d8f 100644 --- a/system.config.js +++ b/system.config.js @@ -42,20 +42,20 @@ System.config({ "@angular/core": "npm:@angular/core@2.0.0-rc.1", "@angular/platform-browser": "npm:@angular/platform-browser@2.0.0-rc.1", "@angular/platform-browser-dynamic": "npm:@angular/platform-browser-dynamic@2.0.0-rc.1", - "Robdel12/DropKick": "github:Robdel12/DropKick@2.1.7", "babel": "npm:babel-core@5.8.34", "babel-runtime": "npm:babel-runtime@5.8.34", - "clean-css": "npm:clean-css@3.4.6", + "clean-css": "npm:clean-css@3.4.17", "core-js": "npm:core-js@1.2.6", "css": "github:systemjs/plugin-css@0.1.18", + "dropkickjs": "npm:dropkickjs@2.1.8", "es6-shim": "github:es-shims/es6-shim@0.33.6", "hint.css": "npm:hint.css@2.2.1", "json": "github:systemjs/plugin-json@0.1.2", "json-formatter-js": "npm:json-formatter-js@0.2.0", "json-pointer": "npm:json-pointer@0.3.0", - "json-schema-instantiator": "npm:json-schema-instantiator@0.3.0", "json-schema-ref-parser": "npm:json-schema-ref-parser@3.1.2", "marked": "npm:marked@0.3.5", + "openapi-sampler": "npm:openapi-sampler@0.1.2", "prismjs": "npm:prismjs@1.3.0", "rxjs": "npm:rxjs@5.0.0-beta.6", "scrollparent": "npm:scrollparent@0.1.0", @@ -63,7 +63,7 @@ System.config({ "systemjs/plugin-json": "github:systemjs/plugin-json@0.1.2", "zone.js": "npm:zone.js@0.6.12", "github:jspm/nodelibs-assert@0.1.0": { - "assert": "npm:assert@1.4.0" + "assert": "npm:assert@1.4.1" }, "github:jspm/nodelibs-buffer@0.1.0": { "buffer": "npm:buffer@3.6.0" @@ -201,10 +201,9 @@ System.config({ "stream": "github:jspm/nodelibs-stream@0.1.0", "util": "github:jspm/nodelibs-util@0.1.0" }, - "npm:assert@1.4.0": { + "npm:assert@1.4.1": { "assert": "github:jspm/nodelibs-assert@0.1.0", "buffer": "github:jspm/nodelibs-buffer@0.1.0", - "buffer-shims": "npm:buffer-shims@1.0.0", "process": "github:jspm/nodelibs-process@0.1.2", "util": "npm:util@0.10.3" }, @@ -292,9 +291,6 @@ System.config({ "readable-stream": "npm:readable-stream@2.0.6", "util": "github:jspm/nodelibs-util@0.1.0" }, - "npm:buffer-shims@1.0.0": { - "buffer": "github:jspm/nodelibs-buffer@0.1.0" - }, "npm:buffer-xor@1.0.3": { "buffer": "github:jspm/nodelibs-buffer@0.1.0", "systemjs-json": "github:systemjs/plugin-json@0.1.2" @@ -333,7 +329,7 @@ System.config({ "string_decoder": "github:jspm/nodelibs-string_decoder@0.1.0", "systemjs-json": "github:systemjs/plugin-json@0.1.2" }, - "npm:clean-css@3.4.6": { + "npm:clean-css@3.4.17": { "buffer": "github:jspm/nodelibs-buffer@0.1.0", "commander": "npm:commander@2.8.1", "fs": "github:jspm/nodelibs-fs@0.1.2", @@ -451,6 +447,9 @@ System.config({ "randombytes": "npm:randombytes@2.0.3", "systemjs-json": "github:systemjs/plugin-json@0.1.2" }, + "npm:dropkickjs@2.1.8": { + "process": "github:jspm/nodelibs-process@0.1.2" + }, "npm:ecc-jsbn@0.1.1": { "buffer": "github:jspm/nodelibs-buffer@0.1.0", "crypto": "github:jspm/nodelibs-crypto@0.1.0", @@ -667,6 +666,9 @@ System.config({ "process": "github:jspm/nodelibs-process@0.1.2", "util": "github:jspm/nodelibs-util@0.1.0" }, + "npm:openapi-sampler@0.1.2": { + "process": "github:jspm/nodelibs-process@0.1.2" + }, "npm:os-browserify@0.1.2": { "os": "github:jspm/nodelibs-os@0.1.0" }, diff --git a/tsconfig.json b/tsconfig.json index 5cd5269e..a74ad198 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,15 +6,16 @@ "target": "es6", "noImplicitAny": false, "sourceMap": false, - "outDir": ".tmp-es/", + "outDir": ".tmp/lib", "moduleResolution": "node" }, "exclude": [ "node_modules", - "lib", "jspm_packages", + ".tmp", "build", "docs", - "*.spec.js" + "*.spec.js", + "lib/index.js" ] } diff --git a/typings/dropkick.d.ts b/typings/dropkick.d.ts deleted file mode 100644 index 4b3ee972..00000000 --- a/typings/dropkick.d.ts +++ /dev/null @@ -1,7 +0,0 @@ - -// Generated by typings -// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/7de6c3dd94feaeb21f20054b9f30d5dabc5efabd/json-pointer/json-pointer.d.ts -declare module "dropkickjs" { - var x: any; - export default x; -} \ No newline at end of file diff --git a/typings/dropkickjs.d.ts b/typings/dropkickjs.d.ts new file mode 100644 index 00000000..eda2ba9a --- /dev/null +++ b/typings/dropkickjs.d.ts @@ -0,0 +1,4 @@ +declare module "dropkickjs" { + var x: any; + export default x; +} diff --git a/typings/index.d.ts b/typings/index.d.ts index ac6df80a..4253de60 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,4 +1,3 @@ /// /// /// - diff --git a/typings/json-schema-instantiator.d.ts b/typings/json-schema-instantiator.d.ts deleted file mode 100644 index 8a06186f..00000000 --- a/typings/json-schema-instantiator.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Generated by typings -// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/7de6c3dd94feaeb21f20054b9f30d5dabc5efabd/json-pointer/json-pointer.d.ts -declare module "json-schema-instantiator" { - var x: any; - export default x; -} \ No newline at end of file diff --git a/typings/json-schema-ref-parser.d.ts b/typings/json-schema-ref-parser.d.ts index be852cc7..b35393ac 100644 --- a/typings/json-schema-ref-parser.d.ts +++ b/typings/json-schema-ref-parser.d.ts @@ -1,6 +1,4 @@ -// Generated by typings -// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/7de6c3dd94feaeb21f20054b9f30d5dabc5efabd/json-pointer/json-pointer.d.ts declare module "json-schema-ref-parser/dist/ref-parser" { var x: any; export default x; -} \ No newline at end of file +} diff --git a/typings/openapi-sampler.d.ts b/typings/openapi-sampler.d.ts new file mode 100644 index 00000000..8c2101de --- /dev/null +++ b/typings/openapi-sampler.d.ts @@ -0,0 +1,3 @@ +declare module "openapi-sampler" { + export function sample(schema:any):any; +}