|
214 | 214 |
|
215 | 215 | /* UTILITIES - FUNCTIONS */ |
216 | 216 |
|
217 | | - String.prototype.trunc = function (n, useWordBoundary) { |
218 | | - var toLong = this.length > n, s_ = toLong ? this.substr(0, n - 1) : this; |
| 217 | + var utils = {}; |
| 218 | + |
| 219 | + utils.trunc = function (string, n, useWordBoundary) { |
| 220 | + var toLong = string.length > n, s_ = toLong ? string.substr(0, n - 1) : string; |
219 | 221 | s_ = useWordBoundary && toLong ? s_.substr(0, s_.lastIndexOf(' ')) : s_; |
220 | 222 | return toLong ? s_ + '...' : s_; |
221 | 223 | }; |
|
225 | 227 | * |
226 | 228 | * @returns {Array} |
227 | 229 | */ |
228 | | - Array.prototype.getUnique = function () { |
| 230 | + utils.getUnique = function (array) { |
229 | 231 | var u = {}, a = []; |
230 | | - for (var i = 0, l = this.length; i < l; ++i) { |
231 | | - if (u.hasOwnProperty(this[i])) { |
| 232 | + for (var i = 0, l = array.length; i < l; ++i) { |
| 233 | + if (u.hasOwnProperty(array[i])) { |
232 | 234 | continue; |
233 | 235 | } |
234 | | - a.push(this[i]); |
235 | | - u[this[i]] = 1; |
| 236 | + a.push(array[i]); |
| 237 | + u[array[i]] = 1; |
236 | 238 | } |
237 | 239 | return a; |
238 | 240 | }; |
239 | 241 |
|
240 | | - String.prototype.toUrl = function () { |
| 242 | + utils.toUrl = function (string) { |
241 | 243 | var url; |
242 | | - if (this.indexOf('//') == -1) { |
243 | | - url = '//' + this; |
| 244 | + if (string.indexOf('//') == -1) { |
| 245 | + url = '//' + string; |
244 | 246 | } else { |
245 | | - url = this; |
| 247 | + url = string; |
246 | 248 | } |
247 | 249 | return url; |
248 | 250 | }; |
|
292 | 294 | return str.replace(urlRegex, function (match) { |
293 | 295 | var extension = match.split('.')[match.split('.').length - 1]; |
294 | 296 | if (($.inArray(extension, opts.linkExclude) === -1)) { |
295 | | - return '<a href="' + match.toUrl() + '" target="' + opts.linkTarget + '">' + match + '</a>'; |
| 297 | + return '<a href="' + utils.toUrl(match) + '" target="' + opts.linkTarget + '">' + match + '</a>'; |
296 | 298 | } |
297 | 299 | return match; |
298 | 300 | }); |
|
374 | 376 | video.host = 'youtube'; |
375 | 377 | video.title = ytData.snippet.title; |
376 | 378 | video.thumbnail = ytData.snippet.thumbnails.medium.url; |
377 | | - video.description = (ytData.snippet.description.trunc(150, true)).replace(/\n/g, ' ').replace(/ /g, ' '); |
| 379 | + video.description = (utils.trunc(ytData.snippet.description, 150, true)).replace(/\n/g, ' ').replace(/ /g, ' '); |
378 | 380 | video.rawDescription = ytData.snippet.description; |
379 | 381 | video.views = ytData.statistics.viewCount; |
380 | 382 | video.likes = ytData.statistics.likeCount; |
|
393 | 395 | video.host = 'vimeo'; |
394 | 396 | video.title = d[0].title; |
395 | 397 | video.rawDescription = (d[0].description).replace(/\n/g, '<br/>').replace(/ /g, '<br/>'); |
396 | | - video.description = (d[0].description).replace(/((<|<)br\s*\/*(>|>)\r\n)/g, ' ').trunc(150, true); |
| 398 | + video.description = utils.trunc((d[0].description).replace(/((<|<)br\s*\/*(>|>)\r\n)/g, ' '), 150, true); |
397 | 399 | video.thumbnail = d[0].thumbnail_medium; |
398 | 400 | video.views = d[0].stats_number_of_plays; |
399 | 401 | video.likes = d[0].stats_number_of_likes; |
|
443 | 445 |
|
444 | 446 | //Remove duplicate urls and save to the variable removedDuplicates |
445 | 447 |
|
446 | | - matchArray = matchArray.getUnique(); |
| 448 | + matchArray = utils.getUnique(matchArray); |
447 | 449 |
|
448 | 450 | var _this = this; |
449 | 451 |
|
|
572 | 574 | var matches; |
573 | 575 | while ((matches = docRegex.exec(rawStr)) !== null) { |
574 | 576 |
|
575 | | - var template = '<div class="ejs-doc"><div class="ejs-doc-preview"><div class="ejs-doc-icon"><i class="fa fa-file-o"></i></div><div class="ejs-doc-detail" ><div class="ejs-doc-title"> <a href="">' + matches[0].toUrl() + '</a></div> <div class="ejs-doc-view"> <a href="' + matches[0].toUrl() + '" target="_blank"><button>' + opts.docOptions.downloadText + '</button></a> <button class="ejs-doc-view-active">' + opts.docOptions.viewText + '</button></div> </div> </div></div>'; |
| 577 | + var template = '<div class="ejs-doc"><div class="ejs-doc-preview"><div class="ejs-doc-icon"><i class="fa fa-file-o"></i></div><div class="ejs-doc-detail" ><div class="ejs-doc-title"> <a href="">' + utils.toUrl(matches[0]) + '</a></div> <div class="ejs-doc-view"> <a href="' + utils.toUrl(matches[0]) + '" target="_blank"><button>' + opts.docOptions.downloadText + '</button></a> <button class="ejs-doc-view-active">' + opts.docOptions.viewText + '</button></div> </div> </div></div>'; |
576 | 578 | embedArray.push(createObject(matches.index, template)); |
577 | 579 |
|
578 | 580 | } |
|
588 | 590 |
|
589 | 591 | var docParent = $(self).closest('.ejs-doc'); |
590 | 592 | var docUrl = $(docParent).find('a')[1].href; |
591 | | - var docViewTemplate = ' <div class="ejs-doc-viewer"><iframe src="http://docs.google.com/viewer?embedded=true&url=' + docUrl.toUrl() + '" frameBorder="0" style="border: none;margin : 0 auto; display : block;"></iframe></div>'; |
| 593 | + var docViewTemplate = ' <div class="ejs-doc-viewer"><iframe src="http://docs.google.com/viewer?embedded=true&url=' + utils.toUrl(docUrl) + '" frameBorder="0" style="border: none;margin : 0 auto; display : block;"></iframe></div>'; |
592 | 594 | docParent.html(docViewTemplate); |
593 | 595 |
|
594 | 596 | //calling the function after the document is shown. |
|
693 | 695 | var matches; |
694 | 696 | while ((matches = flickrRegex.exec(rawStr)) !== null) { |
695 | 697 |
|
696 | | - var template = '<div class="ejs-embed"><div class="ne-image-wrapper"><iframe src="' + matches[0].toUrl() + '/player/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div></div>'; |
| 698 | + var template = '<div class="ejs-embed"><div class="ne-image-wrapper"><iframe src="' + utils.toUrl(matches[0]) + '/player/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div></div>'; |
697 | 699 | embedArray.push(createObject(matches.index, template)); |
698 | 700 |
|
699 | 701 | } |
|
705 | 707 | var matches; |
706 | 708 | while ((matches = instagramRegex.exec(rawStr)) !== null) { |
707 | 709 |
|
708 | | - var template = '<div class="ejs-embed"><iframe src="' + matches[0].toUrl() + '/embed/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div>'; |
| 710 | + var template = '<div class="ejs-embed"><iframe src="' + utils.toUrl(matches[0]) + '/embed/" width="' + dimensions.width + '" height="' + dimensions.height + '"></iframe></div>'; |
709 | 711 | embedArray.push(createObject(matches.index, template)); |
710 | 712 |
|
711 | 713 | } |
|
782 | 784 |
|
783 | 785 | getMatches: function (str) { |
784 | 786 | var tweetRegex = /https:\/\/twitter\.com\/\w+\/\w+\/\d+/gi; |
785 | | - var matches = str.match(tweetRegex) ? (str.match(tweetRegex)).getUnique() : null; |
| 787 | + var matches = str.match(tweetRegex) ? utils.getUnique(str.match(tweetRegex)) : null; |
786 | 788 | return matches; |
787 | 789 |
|
788 | 790 | }, |
|
883 | 885 | if (rawStr.match(ggRegex)) { |
884 | 886 | while ((matches = ggRegex.exec(rawStr)) !== null) { |
885 | 887 | var m = matches; |
886 | | - var match = 'https:' + matches[0].toUrl(); |
| 888 | + var match = 'https:' + utils.toUrl(matches[0]); |
887 | 889 | var url = 'https://noembed.com/embed?nowrap=on&url=' + match; |
888 | 890 | var template = '<div class="ejs-embed ejs-github-gist" data-url="' + url + '"></div>'; |
889 | 891 | embedArray.push(createObject(m.index, template)); |
|
972 | 974 | $.each(embedArray, function (index, value) { |
973 | 975 | embedCodeArray.push(value.embedCode); |
974 | 976 | }); |
975 | | - str = str + embedCodeArray.getUnique().join(' '); |
| 977 | + str = str + utils.getUnique(embedCodeArray).join(' '); |
976 | 978 | embedArray = []; |
977 | 979 | embedCodeArray = []; |
978 | 980 | return str; |
|
0 commit comments