Skip to content

Instantly share code, notes, and snippets.

@Blackening999
Last active November 19, 2015 18:25
Show Gist options
  • Save Blackening999/60f73f292f65f508a953 to your computer and use it in GitHub Desktop.
Save Blackening999/60f73f292f65f508a953 to your computer and use it in GitHub Desktop.
Tweets plugin for Imperavi Redactor 2

Tweets plugin for Imperavi Redactor II

Usage:

  • Add plugin's file to your assets folder or in the (bottom of the );
  • Put plugin name to plugins hash when configuring Imperavi Redactor instance:
$('.redactor').redactor({
        focus: true,
        plugins: ['tweets'],
    );
  • Enjoy :)
(function($)
{
$.Redactor.prototype.tweets = function() {
return {
init: function () {
var button = this.button.add('tweets', 'Tweets');
this.modal.addTemplate('tweets', this.tweets.getTemplate());
this.button.addCallback(button, this.tweets.show);
},
getTemplate: function()
{
return $(
'<div class="tweets-plugin">' +
' <section>' +
' <p>' +
' <label>Pass link to tweet </br>' +
' (e.g. https://twitter.com/DailyStolenBase/status/596855080806146049)' +
' </label>' +
' <input type="text" id="tweet-url" value="" placeholder="#" style="width: 100%" />' +
' </p>' +
' <p>' +
' <label>' +
' <input type="checkbox" id="tweet-show-media" value="1" checked /> Show image' +
' </label>' +
' </p>' +
' <p>' +
' <label>' +
' <input type="checkbox" id="tweet-show-thread" value="1" /> Show thread' +
' </label>' +
' </p>' +
' </section>' +
'<section>' +
'<button id="redactor-modal-button-action">Insert</button>' +
'<button id="redactor-modal-button-cancel">Cancel</button>' +
'</section>' +
'</div>');
},
show: function(e) {
if (typeof e != 'undefined' && e.preventDefault) { e.preventDefault(); }
this.modal.load('tweets', 'Insert Tweets', 600);
var button = this.modal.getActionButton();
button.on('click', this.tweets.insert);
this.selection.save();
this.modal.show();
},
insert: function () {
var tweetURL = $('#tweet-url').val();
var tweetHideMedia = ($('#tweet-show-media').prop('checked') == true) ? 0 : 1;
var tweetHideThread = ($('#tweet-show-thread').prop('checked') == true) ? 0 : 1;
$.get('https://api.twitter.com/1/statuses/oembed.json', {
url: tweetURL, omit_script: 1,
related: 'tjournalru', maxwidth: 550, 'lang': 'en', hide_media: tweetHideMedia,
hide_thread: tweetHideThread
}, $.proxy(function (data) {
if (data.html) {
this.selection.restore();
this.insert.raw(data.html);
this.modal.close();
this.observe.load();
} else {
alert('Error! Incorrect Twitter URL. Please, copy and paste twitter URL exactly from browser');
}
}, this), 'jsonp');
}
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment