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