|
2 | 2 | (function($, prettyPrint) {
|
3 | 3 | 'use strict';
|
4 | 4 | const jsdoctypeparser = require('jsdoctypeparser');
|
5 |
| - const events = require('events'); |
6 |
| - const util = require('util'); |
7 | 5 |
|
8 | 6 | const INITIAL_TYPE_EXPR = '?TypeExpression=';
|
9 | 7 |
|
| 8 | + class TypeExpressionModel extends EventTarget { |
| 9 | + constructor () { |
| 10 | + super(); |
| 11 | + } |
10 | 12 |
|
| 13 | + parse (typeExpr) { |
| 14 | + try { |
| 15 | + const ast = jsdoctypeparser.parse(typeExpr); |
11 | 16 |
|
12 |
| - function TypeExpressionModel() { |
13 |
| - events.EventEmitter.call(this); |
14 |
| - } |
15 |
| - util.inherits(TypeExpressionModel, events.EventEmitter); |
| 17 | + this.ast = ast; |
| 18 | + this.hasSyntaxError = false; |
| 19 | + this.errorMessage = ''; |
| 20 | + } |
| 21 | + catch (err) { |
| 22 | + if (!(err instanceof jsdoctypeparser.JSDocTypeSyntaxError)) throw err; |
16 | 23 |
|
| 24 | + this.ast = null; |
| 25 | + this.hasSyntaxError = true; |
| 26 | + this.errorMessage = err.message; |
| 27 | + } |
17 | 28 |
|
18 |
| - TypeExpressionModel.EventType = { |
19 |
| - CHANGE: 'change', |
20 |
| - }; |
| 29 | + this.dispatchEvent(new CustomEvent('change')); |
| 30 | + } |
| 31 | + } |
21 | 32 |
|
| 33 | + class ParseResultView { |
| 34 | + constructor (model, $jsonView, $stringView) { |
| 35 | + this.typeExprModel = model; |
22 | 36 |
|
23 |
| - TypeExpressionModel.prototype.parse = function(typeExpr) { |
24 |
| - try { |
25 |
| - const ast = jsdoctypeparser.parse(typeExpr); |
| 37 | + this.$jsonView = $jsonView; |
| 38 | + this.$stringView = $stringView; |
26 | 39 |
|
27 |
| - this.ast = ast; |
28 |
| - this.hasSyntaxError = false; |
29 |
| - this.errorMessage = ''; |
| 40 | + this.typeExprModel.addEventListener('change', () => { |
| 41 | + this.render(this.typeExprModel); |
| 42 | + }); |
30 | 43 | }
|
31 |
| - catch (err) { |
32 |
| - if (!(err instanceof jsdoctypeparser.JSDocTypeSyntaxError)) throw err; |
33 | 44 |
|
34 |
| - this.ast = null; |
35 |
| - this.hasSyntaxError = true; |
36 |
| - this.errorMessage = err.message; |
| 45 | + render (model) { |
| 46 | + const $allViews = $([ |
| 47 | + this.$jsonView[0], |
| 48 | + this.$stringView[0], |
| 49 | + ]); |
| 50 | + |
| 51 | + if (model.hasSyntaxError) { |
| 52 | + $allViews.text('ERROR'); |
| 53 | + } |
| 54 | + else { |
| 55 | + const ast = model.ast; |
| 56 | + this.$jsonView.text(JSON.stringify(ast, null, 2)); |
| 57 | + this.$stringView.text(jsdoctypeparser.publish(ast)); |
| 58 | + } |
| 59 | + |
| 60 | + $allViews.removeClass('prettyprinted'); |
| 61 | + prettyPrint(); |
37 | 62 | }
|
38 |
| - |
39 |
| - this.emit(TypeExpressionModel.EventType.CHANGE); |
40 |
| - }; |
41 |
| - |
42 |
| - |
43 |
| - |
44 |
| - function ParseResultView(model, $jsonView, $stringView) { |
45 |
| - this.typeExprModel = model; |
46 |
| - |
47 |
| - this.$jsonView = $jsonView; |
48 |
| - this.$stringView = $stringView; |
49 |
| - |
50 |
| - this.typeExprModel.on(TypeExpressionModel.EventType.CHANGE, () => { |
51 |
| - this.render(this.typeExprModel); |
52 |
| - }); |
53 | 63 | }
|
54 | 64 |
|
| 65 | + class ParseSuccessfulAlert { |
| 66 | + constructor (model, $alertElement) { |
| 67 | + this.typeExprModel = model; |
55 | 68 |
|
56 |
| - ParseResultView.prototype.render = function(model) { |
57 |
| - const $allViews = $([ |
58 |
| - this.$jsonView[0], |
59 |
| - this.$stringView[0], |
60 |
| - ]); |
| 69 | + this.$alertElement = $alertElement; |
61 | 70 |
|
62 |
| - if (model.hasSyntaxError) { |
63 |
| - $allViews.text('ERROR'); |
64 |
| - } |
65 |
| - else { |
66 |
| - const ast = model.ast; |
67 |
| - this.$jsonView.text(JSON.stringify(ast, null, 2)); |
68 |
| - this.$stringView.text(jsdoctypeparser.publish(ast)); |
| 71 | + this.typeExprModel.addEventListener('change', () => { |
| 72 | + this.setVisibility(!this.typeExprModel.hasSyntaxError); |
| 73 | + }); |
69 | 74 | }
|
70 | 75 |
|
71 |
| - $allViews.removeClass('prettyprinted'); |
72 |
| - prettyPrint(); |
73 |
| - }; |
74 |
| - |
75 |
| - |
76 |
| - |
77 |
| - function ParseSuccessfulAlert(model, $alertElement) { |
78 |
| - this.typeExprModel = model; |
79 |
| - |
80 |
| - this.$alertElement = $alertElement; |
81 |
| - |
82 |
| - this.typeExprModel.on(TypeExpressionModel.EventType.CHANGE, () => { |
83 |
| - this.setVisibility(!this.typeExprModel.hasSyntaxError); |
84 |
| - }); |
| 76 | + setVisibility (isVisible) { |
| 77 | + this.$alertElement.toggle(isVisible); |
| 78 | + } |
85 | 79 | }
|
86 | 80 |
|
| 81 | + class ParseErrorAlert { |
| 82 | + constructor (model, $alertElement, $messageElement, $closeButton) { |
| 83 | + this.typeExprModel = model; |
87 | 84 |
|
88 |
| - ParseSuccessfulAlert.prototype.setVisibility = function(isVisible) { |
89 |
| - this.$alertElement.toggle(isVisible); |
90 |
| - }; |
91 |
| - |
| 85 | + this.$alertElement = $alertElement; |
| 86 | + this.$messageElement = $messageElement; |
| 87 | + this.$closeButton = $closeButton; |
92 | 88 |
|
| 89 | + this.typeExprModel.addEventListener('change', () => { |
| 90 | + this.render(this.typeExprModel); |
| 91 | + }); |
93 | 92 |
|
94 |
| - function ParseErrorAlert(model, $alertElement, $messageElement, $closeButton) { |
95 |
| - this.typeExprModel = model; |
| 93 | + this.$closeButton.on('click', () => { |
| 94 | + this.setVisibility(true); |
| 95 | + }); |
| 96 | + } |
96 | 97 |
|
97 |
| - this.$alertElement = $alertElement; |
98 |
| - this.$messageElement = $messageElement; |
99 |
| - this.$closeButton = $closeButton; |
| 98 | + render (model) { |
| 99 | + this.setVisibility(model.hasSyntaxError); |
| 100 | + this.setMessage(model.errorMessage); |
| 101 | + } |
100 | 102 |
|
101 |
| - this.typeExprModel.on(TypeExpressionModel.EventType.CHANGE, () => { |
102 |
| - this.render(this.typeExprModel); |
103 |
| - }); |
| 103 | + setVisibility (isVisible) { |
| 104 | + this.$alertElement.toggle(isVisible); |
| 105 | + } |
104 | 106 |
|
105 |
| - this.$closeButton.on('click', () => { |
106 |
| - this.setVisibility(true); |
107 |
| - }); |
| 107 | + setMessage (msg) { |
| 108 | + this.$messageElement.text(msg); |
| 109 | + } |
108 | 110 | }
|
109 | 111 |
|
110 |
| - |
111 |
| - ParseErrorAlert.prototype.render = function(model) { |
112 |
| - this.setVisibility(model.hasSyntaxError); |
113 |
| - this.setMessage(model.errorMessage); |
114 |
| - }; |
115 |
| - |
116 |
| - |
117 |
| - ParseErrorAlert.prototype.setVisibility = function(isVisible) { |
118 |
| - this.$alertElement.toggle(isVisible); |
119 |
| - }; |
120 |
| - |
121 |
| - |
122 |
| - ParseErrorAlert.prototype.setMessage = function(msg) { |
123 |
| - this.$messageElement.text(msg); |
124 |
| - }; |
125 |
| - |
126 |
| - |
127 | 112 | function bootstrap() {
|
128 | 113 | const typeExprModel = new TypeExpressionModel();
|
129 | 114 |
|
|
146 | 131 | typeExprModel.parse(INITIAL_TYPE_EXPR, true);
|
147 | 132 | }
|
148 | 133 |
|
149 |
| - |
150 |
| - |
151 | 134 | function createParseSuccessfulAlert(model) {
|
152 | 135 | const $alertElement = $('#success');
|
153 | 136 | return new ParseSuccessfulAlert(model, $alertElement);
|
154 | 137 | }
|
155 | 138 |
|
156 |
| - |
157 |
| - |
158 | 139 | function createParseErrorAlert(model) {
|
159 | 140 | const $alertElement = $('#err');
|
160 | 141 | const $messageElement = $('#err-msg');
|
161 | 142 | const $closeButton = $('#err-close');
|
162 | 143 | return new ParseErrorAlert(model, $alertElement, $messageElement, $closeButton);
|
163 | 144 | }
|
164 | 145 |
|
165 |
| - |
166 |
| - |
167 | 146 | function createParseResultView(model) {
|
168 | 147 | const $jsonView = $('#output-obj');
|
169 | 148 | const $stringView = $('#output-str');
|
170 | 149 | return new ParseResultView(model, $jsonView, $stringView);
|
171 | 150 | }
|
172 | 151 |
|
173 |
| - |
174 | 152 | bootstrap();
|
175 | 153 | })(jQuery, prettyPrint);
|
0 commit comments