Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 3eca7ef

Browse files
committed
- Refactoring: Minimize Node/browserify dependency and use browser EventTarget and ES6 classes for more modern, succincter syntax
1 parent 02f64da commit 3eca7ef

File tree

1 file changed

+78
-100
lines changed

1 file changed

+78
-100
lines changed

index.js

Lines changed: 78 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,128 +2,113 @@
22
(function($, prettyPrint) {
33
'use strict';
44
const jsdoctypeparser = require('jsdoctypeparser');
5-
const events = require('events');
6-
const util = require('util');
75

86
const INITIAL_TYPE_EXPR = '?TypeExpression=';
97

8+
class TypeExpressionModel extends EventTarget {
9+
constructor () {
10+
super();
11+
}
1012

13+
parse (typeExpr) {
14+
try {
15+
const ast = jsdoctypeparser.parse(typeExpr);
1116

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;
1623

24+
this.ast = null;
25+
this.hasSyntaxError = true;
26+
this.errorMessage = err.message;
27+
}
1728

18-
TypeExpressionModel.EventType = {
19-
CHANGE: 'change',
20-
};
29+
this.dispatchEvent(new CustomEvent('change'));
30+
}
31+
}
2132

33+
class ParseResultView {
34+
constructor (model, $jsonView, $stringView) {
35+
this.typeExprModel = model;
2236

23-
TypeExpressionModel.prototype.parse = function(typeExpr) {
24-
try {
25-
const ast = jsdoctypeparser.parse(typeExpr);
37+
this.$jsonView = $jsonView;
38+
this.$stringView = $stringView;
2639

27-
this.ast = ast;
28-
this.hasSyntaxError = false;
29-
this.errorMessage = '';
40+
this.typeExprModel.addEventListener('change', () => {
41+
this.render(this.typeExprModel);
42+
});
3043
}
31-
catch (err) {
32-
if (!(err instanceof jsdoctypeparser.JSDocTypeSyntaxError)) throw err;
3344

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();
3762
}
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-
});
5363
}
5464

65+
class ParseSuccessfulAlert {
66+
constructor (model, $alertElement) {
67+
this.typeExprModel = model;
5568

56-
ParseResultView.prototype.render = function(model) {
57-
const $allViews = $([
58-
this.$jsonView[0],
59-
this.$stringView[0],
60-
]);
69+
this.$alertElement = $alertElement;
6170

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+
});
6974
}
7075

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+
}
8579
}
8680

81+
class ParseErrorAlert {
82+
constructor (model, $alertElement, $messageElement, $closeButton) {
83+
this.typeExprModel = model;
8784

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;
9288

89+
this.typeExprModel.addEventListener('change', () => {
90+
this.render(this.typeExprModel);
91+
});
9392

94-
function ParseErrorAlert(model, $alertElement, $messageElement, $closeButton) {
95-
this.typeExprModel = model;
93+
this.$closeButton.on('click', () => {
94+
this.setVisibility(true);
95+
});
96+
}
9697

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+
}
100102

101-
this.typeExprModel.on(TypeExpressionModel.EventType.CHANGE, () => {
102-
this.render(this.typeExprModel);
103-
});
103+
setVisibility (isVisible) {
104+
this.$alertElement.toggle(isVisible);
105+
}
104106

105-
this.$closeButton.on('click', () => {
106-
this.setVisibility(true);
107-
});
107+
setMessage (msg) {
108+
this.$messageElement.text(msg);
109+
}
108110
}
109111

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-
127112
function bootstrap() {
128113
const typeExprModel = new TypeExpressionModel();
129114

@@ -146,30 +131,23 @@
146131
typeExprModel.parse(INITIAL_TYPE_EXPR, true);
147132
}
148133

149-
150-
151134
function createParseSuccessfulAlert(model) {
152135
const $alertElement = $('#success');
153136
return new ParseSuccessfulAlert(model, $alertElement);
154137
}
155138

156-
157-
158139
function createParseErrorAlert(model) {
159140
const $alertElement = $('#err');
160141
const $messageElement = $('#err-msg');
161142
const $closeButton = $('#err-close');
162143
return new ParseErrorAlert(model, $alertElement, $messageElement, $closeButton);
163144
}
164145

165-
166-
167146
function createParseResultView(model) {
168147
const $jsonView = $('#output-obj');
169148
const $stringView = $('#output-str');
170149
return new ParseResultView(model, $jsonView, $stringView);
171150
}
172151

173-
174152
bootstrap();
175153
})(jQuery, prettyPrint);

0 commit comments

Comments
 (0)