6
6
/*global grammar _ */
7
7
8
8
function Diagram ( ) {
9
- this . title = undefined ;
10
- this . actors = [ ] ;
11
- this . signals = [ ] ;
9
+ this . title = undefined ;
10
+ this . actors = [ ] ;
11
+ this . signals = [ ] ;
12
12
}
13
13
/*
14
14
* Return an existing actor with this alias, or creates a new one with alias and name.
15
15
*/
16
16
Diagram . prototype . getActor = function ( alias , name ) {
17
- alias = alias . trim ( ) ;
18
-
19
- var i , actors = this . actors ;
20
- for ( i in actors ) {
21
- if ( actors [ i ] . alias == alias )
22
- return actors [ i ] ;
23
- }
24
- i = actors . push ( new Diagram . Actor ( alias , ( name || alias ) , actors . length ) ) ;
25
- return actors [ i - 1 ] ;
17
+ alias = alias . trim ( ) ;
18
+
19
+ var i ;
20
+ var actors = this . actors ;
21
+ for ( i in actors ) {
22
+ if ( actors [ i ] . alias == alias ) {
23
+ return actors [ i ] ;
24
+ }
25
+ }
26
+ i = actors . push ( new Diagram . Actor ( alias , ( name || alias ) , actors . length ) ) ;
27
+ return actors [ i - 1 ] ;
26
28
} ;
27
29
28
30
/*
29
31
* Parses the input as either a alias, or a "name as alias", and returns the corresponding actor.
30
32
*/
31
33
Diagram . prototype . getActorWithAlias = function ( input ) {
32
- input = input . trim ( ) ;
33
-
34
- // We are lazy and do some of the parsing in javascript :(. TODO move into the .jison file.
35
- var s = / ( [ \s \S ] + ) a s ( \S + ) $ / im. exec ( input ) ;
36
- var alias , name ;
37
- if ( s ) {
38
- name = s [ 1 ] . trim ( ) ;
39
- alias = s [ 2 ] . trim ( ) ;
40
- } else {
41
- name = alias = input ;
42
- }
43
- return this . getActor ( alias , name ) ;
34
+ input = input . trim ( ) ;
35
+
36
+ // We are lazy and do some of the parsing in javascript :(. TODO move into the .jison file.
37
+ var s = / ( [ \s \S ] + ) a s ( \S + ) $ / im. exec ( input ) ;
38
+ var alias ;
39
+ var name ;
40
+ if ( s ) {
41
+ name = s [ 1 ] . trim ( ) ;
42
+ alias = s [ 2 ] . trim ( ) ;
43
+ } else {
44
+ name = alias = input ;
45
+ }
46
+ return this . getActor ( alias , name ) ;
44
47
} ;
45
48
46
49
Diagram . prototype . setTitle = function ( title ) {
47
- this . title = title ;
50
+ this . title = title ;
48
51
} ;
49
52
50
53
Diagram . prototype . addSignal = function ( signal ) {
51
- this . signals . push ( signal ) ;
54
+ this . signals . push ( signal ) ;
52
55
} ;
53
56
54
57
Diagram . Actor = function ( alias , name , index ) {
55
- this . alias = alias ;
56
- this . name = name ;
57
- this . index = index ;
58
+ this . alias = alias ;
59
+ this . name = name ;
60
+ this . index = index ;
58
61
} ;
59
62
60
63
Diagram . Signal = function ( actorA , signaltype , actorB , message ) {
61
- this . type = " Signal" ;
62
- this . actorA = actorA ;
63
- this . actorB = actorB ;
64
- this . linetype = signaltype & 3 ;
65
- this . arrowtype = ( signaltype >> 2 ) & 3 ;
66
- this . message = message ;
64
+ this . type = ' Signal' ;
65
+ this . actorA = actorA ;
66
+ this . actorB = actorB ;
67
+ this . linetype = signaltype & 3 ;
68
+ this . arrowtype = ( signaltype >> 2 ) & 3 ;
69
+ this . message = message ;
67
70
} ;
68
71
69
72
Diagram . Signal . prototype . isSelf = function ( ) {
70
- return this . actorA . index == this . actorB . index ;
73
+ return this . actorA . index == this . actorB . index ;
71
74
} ;
72
75
73
76
Diagram . Note = function ( actor , placement , message ) {
74
- this . type = " Note" ;
75
- this . actor = actor ;
76
- this . placement = placement ;
77
- this . message = message ;
78
-
79
- if ( this . hasManyActors ( ) && actor [ 0 ] == actor [ 1 ] ) {
80
- throw new Error ( " Note should be over two different actors" ) ;
81
- }
77
+ this . type = ' Note' ;
78
+ this . actor = actor ;
79
+ this . placement = placement ;
80
+ this . message = message ;
81
+
82
+ if ( this . hasManyActors ( ) && actor [ 0 ] == actor [ 1 ] ) {
83
+ throw new Error ( ' Note should be over two different actors' ) ;
84
+ }
82
85
} ;
83
86
84
87
Diagram . Note . prototype . hasManyActors = function ( ) {
85
- return _ . isArray ( this . actor ) ;
88
+ return _ . isArray ( this . actor ) ;
86
89
} ;
87
90
88
91
Diagram . unescape = function ( s ) {
89
- // Turn "\\n" into "\n"
90
- return s . trim ( ) . replace ( / ^ " ( .* ) " $ / m, "$1" ) . replace ( / \\ n / gm, "\n" ) ;
92
+ // Turn "\\n" into "\n"
93
+ return s . trim ( ) . replace ( / ^ " ( .* ) " $ / m, '$1' ) . replace ( / \\ n / gm, '\n' ) ;
91
94
} ;
92
95
93
96
Diagram . LINETYPE = {
94
- SOLID : 0 ,
95
- DOTTED : 1
97
+ SOLID : 0 ,
98
+ DOTTED : 1
96
99
} ;
97
100
98
101
Diagram . ARROWTYPE = {
99
- FILLED : 0 ,
100
- OPEN : 1
102
+ FILLED : 0 ,
103
+ OPEN : 1
101
104
} ;
102
105
103
106
Diagram . PLACEMENT = {
104
- LEFTOF : 0 ,
105
- RIGHTOF : 1 ,
106
- OVER : 2
107
+ LEFTOF : 0 ,
108
+ RIGHTOF : 1 ,
109
+ OVER : 2
107
110
} ;
108
111
109
-
110
112
// Some older browsers don't have getPrototypeOf, thus we polyfill it
111
113
// https://github.com/bramp/js-sequence-diagrams/issues/57
112
114
// https://github.com/zaach/jison/issues/194
113
115
// Taken from http://ejohn.org/blog/objectgetprototypeof/
114
- if ( typeof Object . getPrototypeOf !== " function" ) {
115
- /* jshint -W103 */
116
- if ( typeof " test" . __proto__ === " object" ) {
117
- Object . getPrototypeOf = function ( object ) {
118
- return object . __proto__ ;
119
- } ;
120
- } else {
121
- Object . getPrototypeOf = function ( object ) {
122
- // May break if the constructor has been tampered with
123
- return object . constructor . prototype ;
124
- } ;
125
- }
126
- /* jshint +W103 */
116
+ if ( typeof Object . getPrototypeOf !== ' function' ) {
117
+ /* jshint -W103 */
118
+ if ( typeof ' test' . __proto__ === ' object' ) {
119
+ Object . getPrototypeOf = function ( object ) {
120
+ return object . __proto__ ;
121
+ } ;
122
+ } else {
123
+ Object . getPrototypeOf = function ( object ) {
124
+ // May break if the constructor has been tampered with
125
+ return object . constructor . prototype ;
126
+ } ;
127
+ }
128
+ /* jshint +W103 */
127
129
}
128
130
129
131
/** The following is included by preprocessor */
@@ -134,30 +136,28 @@ if ( typeof Object.getPrototypeOf !== "function" ) {
134
136
* This is brittle as it depends on jison internals
135
137
*/
136
138
function ParseError ( message , hash ) {
137
- _ . extend ( this , hash ) ;
139
+ _ . extend ( this , hash ) ;
138
140
139
- this . name = " ParseError" ;
140
- this . message = ( message || "" ) ;
141
+ this . name = ' ParseError' ;
142
+ this . message = ( message || '' ) ;
141
143
}
142
144
ParseError . prototype = new Error ( ) ;
143
145
Diagram . ParseError = ParseError ;
144
146
145
147
Diagram . parse = function ( input ) {
146
- // TODO jison v0.4.17 changed their API slightly, so parser is no longer defined:
148
+ // TODO jison v0.4.17 changed their API slightly, so parser is no longer defined:
147
149
148
- // Create the object to track state and deal with errors
149
- parser . yy = new Diagram ( ) ;
150
- parser . yy . parseError = function ( message , hash ) {
151
- throw new ParseError ( message , hash ) ;
152
- } ;
150
+ // Create the object to track state and deal with errors
151
+ parser . yy = new Diagram ( ) ;
152
+ parser . yy . parseError = function ( message , hash ) {
153
+ throw new ParseError ( message , hash ) ;
154
+ } ;
153
155
154
- // Parse
155
- var diagram = parser . parse ( input ) ;
156
+ // Parse
157
+ var diagram = parser . parse ( input ) ;
156
158
157
- // Then clean up the parseError key that a user won't care about
158
- delete diagram . parseError ;
159
- return diagram ;
159
+ // Then clean up the parseError key that a user won't care about
160
+ delete diagram . parseError ;
161
+ return diagram ;
160
162
} ;
161
163
162
-
163
-
0 commit comments