2016-12-25 15:41:57 +01:00
|
|
|
/**
|
|
|
|
* Instagram plugin
|
|
|
|
* @version 1.0.0
|
|
|
|
*/
|
2017-02-13 18:54:18 +01:00
|
|
|
var instagram = (function(instagram_plugin) {
|
2017-01-18 16:12:50 +01:00
|
|
|
|
|
|
|
var methods = {
|
|
|
|
|
|
|
|
render : function(content) {
|
|
|
|
|
2017-02-26 11:22:21 +01:00
|
|
|
codex.editor.content.switchBlock(codex.editor.content.currentNode, content);
|
2017-01-30 14:18:54 +01:00
|
|
|
|
2017-01-26 15:43:49 +01:00
|
|
|
setTimeout(function() {
|
|
|
|
window.instgrm.Embeds.process();
|
|
|
|
}, 200);
|
2017-01-18 16:12:50 +01:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Drawing html content.
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* @returns {Element} blockquote - HTML template for Instagram Embed JS
|
|
|
|
*/
|
|
|
|
instagramBlock : function(url) {
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
var blockquote = codex.editor.draw.node('BLOCKQUOTE', 'instagram-media instagram', {}),
|
|
|
|
div = codex.editor.draw.node('DIV', '', {}),
|
|
|
|
paragraph = codex.editor.draw.node('P', 'ce-paste__instagram--p', {}),
|
|
|
|
anchor = codex.editor.draw.node('A', '', { href : url });
|
2017-01-18 16:12:50 +01:00
|
|
|
|
|
|
|
blockquote.dataset.instgrmVersion = 4;
|
|
|
|
|
|
|
|
paragraph.appendChild(anchor);
|
|
|
|
div.appendChild(paragraph);
|
|
|
|
blockquote.appendChild(div);
|
|
|
|
|
|
|
|
return blockquote;
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare before usage
|
|
|
|
* Load important scripts to render embed
|
|
|
|
*/
|
2017-02-13 18:54:18 +01:00
|
|
|
instagram_plugin.prepare = function() {
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-02-01 10:14:00 +01:00
|
|
|
return new Promise(function(resolve, reject){
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
codex.editor.core.importScript("https://platform.instagram.com/en_US/embeds.js", 'instagram-api').then(function(){
|
2017-02-01 10:14:00 +01:00
|
|
|
resolve();
|
|
|
|
}).catch(function(){
|
|
|
|
reject(Error('Instagram API was not loaded'));
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2017-01-18 16:12:50 +01:00
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-26 15:43:49 +01:00
|
|
|
* @private
|
|
|
|
*
|
2016-12-25 15:41:57 +01:00
|
|
|
* Make instagram embed via Widgets method
|
|
|
|
*/
|
2017-01-26 15:43:49 +01:00
|
|
|
var make_ = function(data, isInternal) {
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
if (!data.instagram_url)
|
|
|
|
return;
|
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
var block = methods.instagramBlock(data.instagram_url);
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
if (isInternal) {
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
|
|
|
/** Render block */
|
2017-01-18 16:12:50 +01:00
|
|
|
methods.render(block);
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
if (!isInternal) {
|
|
|
|
methods.render(block);
|
|
|
|
}
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
return block;
|
2017-01-18 16:12:50 +01:00
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-02-13 18:54:18 +01:00
|
|
|
instagram_plugin.validate = function(data) {
|
2017-01-18 16:12:50 +01:00
|
|
|
return true;
|
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saving JSON output.
|
|
|
|
* Upload data via ajax
|
|
|
|
*/
|
2017-02-13 18:54:18 +01:00
|
|
|
instagram_plugin.save = function(blockContent) {
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
var data;
|
|
|
|
|
|
|
|
if (!blockContent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/** Example */
|
|
|
|
data = {
|
|
|
|
instagram_url: blockContent.src
|
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-02-13 18:54:18 +01:00
|
|
|
instagram_plugin.validate = function(data) {
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
var checkUrl = new RegExp("http?.+instagram.com\/p?.");
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
if (!data.instagram_url || checkUrl.exec(data.instagram_url).length == 0)
|
|
|
|
return;
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
return true;
|
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-18 16:12:50 +01:00
|
|
|
* Render data
|
2016-12-25 15:41:57 +01:00
|
|
|
*/
|
2017-02-13 18:54:18 +01:00
|
|
|
instagram_plugin.render = function(data) {
|
2017-01-26 15:43:49 +01:00
|
|
|
return make_(data);
|
2017-01-18 16:12:50 +01:00
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-26 01:47:19 +01:00
|
|
|
* callback for instagram url's coming from pasteTool
|
|
|
|
* Using instagram Embed Widgete to render
|
2016-12-25 15:41:57 +01:00
|
|
|
* @param url
|
|
|
|
*/
|
2017-02-13 18:54:18 +01:00
|
|
|
instagram_plugin.urlPastedCallback = function(url) {
|
2017-01-26 01:47:19 +01:00
|
|
|
var data = {
|
|
|
|
instagram_url: url
|
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-26 15:43:49 +01:00
|
|
|
make_(data, true);
|
2017-01-12 14:56:06 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-02-13 18:54:18 +01:00
|
|
|
instagram_plugin.pastePatterns = [
|
2017-02-12 16:25:20 +01:00
|
|
|
{
|
|
|
|
type: 'instagram',
|
2017-02-26 11:22:21 +01:00
|
|
|
regex: /http?.+instagram.com\/p\/([a-zA-Z0-9]*)\S*/,
|
2017-02-13 18:54:18 +01:00
|
|
|
callback: instagram_plugin.urlPastedCallback
|
2017-02-12 16:25:20 +01:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2017-02-13 18:54:18 +01:00
|
|
|
instagram_plugin.destroy = function () {
|
|
|
|
|
|
|
|
instagram = null;
|
|
|
|
delete window.instgrm
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return instagram_plugin;
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
})({});
|
2017-01-26 15:43:49 +01:00
|
|
|
|