Debian Codegraph @

src/match/Mention.js

raw symbols SCIP
1 /*global Autolinker */
2 /**
3 * @class Autolinker.match.Mention
4 * @extends Autolinker.match.Match
5 *
6 * Represents a Mention match found in an input string which should be Autolinked.
7 *
8 * See this class's superclass ({@link Autolinker.match.Match}) for more details.
9 */
10 Autolinker.match.Mention = Autolinker.Util.extend( Autolinker.match.Match, {
11
12 /**
13 * @cfg {String} serviceName
14 *
15 * The service to point mention matches to. See {@link Autolinker#mention}
16 * for available values.
17 */
18
19 /**
20 * @cfg {String} mention (required)
21 *
22 * The Mention that was matched, without the '@' character.
23 */
24
25
26 /**
27 * @constructor
28 * @param {Object} cfg The configuration properties for the Match
29 * instance, specified in an Object (map).
30 */
31 constructor : function( cfg ) {
32 Autolinker.match.Match.prototype.constructor.call( this, cfg );
33
34 // @if DEBUG
35 if( !cfg.serviceName ) throw new Error( '`serviceName` cfg required' );
36 if( !cfg.mention ) throw new Error( '`mention` cfg required' );
37 // @endif
38
39 this.mention = cfg.mention;
40 this.serviceName = cfg.serviceName;
41 },
42
43
44 /**
45 * Returns the type of match that this class represents.
46 *
47 * @return {String}
48 */
49 getType : function() {
50 return 'mention';
51 },
52
53
54 /**
55 * Returns the mention, without the '@' character.
56 *
57 * @return {String}
58 */
59 getMention : function() {
60 return this.mention;
61 },
62
63
64 /**
65 * Returns the configured {@link #serviceName} to point the mention to.
66 * Ex: 'instagram', 'twitter', 'soundcloud'.
67 *
68 * @return {String}
69 */
70 getServiceName : function() {
71 return this.serviceName;
72 },
73
74
75 /**
76 * Returns the anchor href that should be generated for the match.
77 *
78 * @return {String}
79 */
80 getAnchorHref : function() {
81 switch( this.serviceName ) {
82 case 'twitter' :
83 return 'https://twitter.com/' + this.mention;
84 case 'instagram' :
85 return 'https://instagram.com/' + this.mention;
86 case 'soundcloud' :
87 return 'https://soundcloud.com/' + this.mention;
88
89 default : // Shouldn't happen because Autolinker's constructor should block any invalid values, but just in case.
90 throw new Error( 'Unknown service name to point mention to: ', this.serviceName );
91 }
92 },
93
94
95 /**
96 * Returns the anchor text that should be generated for the match.
97 *
98 * @return {String}
99 */
100 getAnchorText : function() {
101 return '@' + this.mention;
102 },
103
104
105 /**
106 * Returns the CSS class suffixes that should be used on a tag built with
107 * the match. See {@link Autolinker.match.Match#getCssClassSuffixes} for
108 * details.
109 *
110 * @return {String[]}
111 */
112 getCssClassSuffixes : function() {
113 var cssClassSuffixes = Autolinker.match.Match.prototype.getCssClassSuffixes.call( this ),
114 serviceName = this.getServiceName();
115
116 if( serviceName ) {
117 cssClassSuffixes.push( serviceName );
118 }
119 return cssClassSuffixes;
120 }
121
122 } );