123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- (function() {
-
- var oEmbed = {
- api: 'https://noembed.com/embed',
- width: 100,
- height: 75,
- cache: {},
- callbacks: {},
-
- lookup: function(url, callback)
- {
- if (typeof oEmbed.cache[url] == "object") {
-
- callback(oEmbed.cache[url]);
- } else if (typeof oEmbed.callbacks[url] == "undefined") {
-
- oEmbed.callbacks[url] = [callback];
- oEmbed.rawLookup(url, function(data) {
- oEmbed.cache[url] = data;
- var callbacks = oEmbed.callbacks[url];
- oEmbed.callbacks[url] = undefined;
- for (var i = 0; i < callbacks.length; i++) {
- callbacks[i](data);
- }
- });
- } else {
-
- oEmbed.callbacks[url].push(callback);
- }
- },
-
- rawLookup: function(url, callback)
- {
- var params = {
- url: url,
- format: 'json',
- maxwidth: oEmbed.width,
- maxheight: oEmbed.height,
- token: $('#token').val()
- };
- $.ajax({
- url: oEmbed.api,
- data: params,
- dataType: 'json',
- success: function(data, xhr) {
- callback(data);
- },
- error: function(xhr, textStatus, errorThrown) {
- callback(null);
- }
- });
- }
- };
- SN.Init.LinkPreview = function(params) {
- if (params.api) oEmbed.api = params.api;
- if (params.width) oEmbed.width = params.width;
- if (params.height) oEmbed.height = params.height;
- }
-
- var origCounter = SN.U.Counter;
- SN.U.Counter = function(form) {
- var preview = form.data('LinkPreview');
- if (preview) {
- preview.previewLinks(form.find('.notice_data-text:first').val());
- }
- return origCounter(form);
- }
-
- var origSetup = SN.Init.NoticeFormSetup;
- SN.Init.NoticeFormSetup = function(form) {
- origSetup(form);
- form
- .bind('reset', function() {
- LinkPreview.clear();
- });
- var LinkPreview = {
- links: [],
- state: [],
- refresh: [],
-
- findLinks: function (text)
- {
-
- var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
- var links = [];
- var matches;
- while ((matches = re.exec(text)) !== null) {
- links.push(matches[1]);
- }
- return links;
- },
- ensureArea: function() {
- if (form.find('.link-preview').length < 1) {
- form.append('<div class="notice-status link-preview thumbnails"></div>');
- }
- },
-
- prepLinkPreview: function(col)
- {
- var id = 'link-preview-' + col;
- var url = LinkPreview.links[col];
- LinkPreview.refresh[col] = false;
- LinkPreview.markLoading(col);
- oEmbed.lookup(url, function(data) {
- var thumb = null;
- var width = 100;
- if (data && typeof data.thumbnail_url == "string") {
- thumb = data.thumbnail_url;
- if (typeof data.thumbnail_width !== "undefined") {
- if (data.thumbnail_width < width) {
- width = data.thumbnail_width;
- }
- }
- } else if (data && data.type == 'photo' && typeof data.url == "string") {
- thumb = data.url;
- if (typeof data.width !== "undefined") {
- if (data.width < width) {
- width = data.width;
- }
- }
- }
- if (thumb) {
- LinkPreview.ensureArea();
- var link = $('<span class="inline-attachment"><a><img/></a></span>');
- link.find('a')
- .attr('href', url)
- .attr('target', '_blank')
- .last()
- .find('img')
- .attr('src', thumb)
- .attr('width', width)
- .attr('title', data.title || data.url || url);
- form.find('.' + id)
- .empty()
- .append(link);
- } else {
-
- LinkPreview.clearLink(col);
- }
- if (LinkPreview.refresh[col]) {
-
-
- LinkPreview.prepLinkPreview(col);
- } else {
- LinkPreview.markDone(col);
- }
- });
- },
-
- previewLinks: function(text)
- {
- var i;
- var old = LinkPreview.links;
- var links = LinkPreview.findLinks(text);
- LinkPreview.links = links;
-
- for (i = 0; i < old.length && i < links.length; i++) {
- if (links[i] != old[i]) {
- if (LinkPreview.state[i] == "loading") {
-
- LinkPreview.refresh[i] = true;
- } else {
-
- LinkPreview.prepLinkPreview(i);
- }
- }
- }
- if (links.length > old.length) {
-
- for (i = old.length; i < links.length; i++) {
- LinkPreview.addPreviewArea(i);
- LinkPreview.prepLinkPreview(i);
- }
- } else if (old.length > links.length) {
-
- for (i = links.length; i < old.length; i++) {
- LinkPreview.clearLink(i);
- }
- }
- if (links.length == 0) {
- LinkPreview.clear();
- }
- },
- addPreviewArea: function(col) {
- LinkPreview.ensureArea();
- var id = 'link-preview-' + col;
- if (form.find('.' + id).length < 1) {
- form.find('.link-preview').append('<span class="' + id + '"></span>');
- }
- },
- clearLink: function(col) {
- var id = 'link-preview-' + col;
- form.find('.' + id).html('');
- },
- markLoading: function(col) {
- LinkPreview.state[col] = "loading";
- var id = 'link-preview-' + col;
- form.find('.' + id).attr('style', 'opacity: 0.5');
- },
- markDone: function(col) {
- LinkPreview.state[col] = "done";
- var id = 'link-preview-' + col;
- form.find('.' + id).removeAttr('style');
- },
-
- clear: function() {
- LinkPreview.links = [];
- form.find('.link-preview').remove();
- }
- };
- form.data('LinkPreview', LinkPreview);
- }
- })();
|