Created
February 17, 2016 20:46
-
-
Save ra100/55c8c1c1bd4d2b9fe387 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* =========================================================== | |
* trumbowyg.cro.js v1.0 | |
* Cro plugin for Tumbowyg | |
* http://alex-d.github.com/Trumbowyg | |
* =========================================================== | |
* Author : Rastislav Å varba (ra100) | |
* Twitter : @ra100 | |
* Website : ra100.net | |
*/ | |
(function ($) { | |
'use strict'; | |
addXhrProgressEvent(); | |
$.extend(true, $.trumbowyg.tagToButton, {'p': ['p']}); | |
$.extend(true, $.trumbowyg, { | |
langs: { | |
en: { | |
crInsert: "Edit", | |
crImage: "Insert Picture from RSCR", | |
crAudio: "Insert Audio", | |
crUpload: "Upload Image", | |
crSource: 'Add source', | |
imageId: "Picture ID ", | |
audioId: "Audio ID", | |
fileToUpload: "File", | |
author: 'Author', | |
source: 'Source' | |
}, | |
cs: { | |
crInsert: "Vložit", | |
crImage: "Vložit Obrázek z RSCR", | |
crAudio: "Vložit Audio z RSCR", | |
crUpload: "Nahrát obrázek", | |
crSource: "Vložit zdroj", | |
imageId: "ID obrázku", | |
audioId: "ID audia", | |
fileToUpload: "Soubor", | |
author: "Autor", | |
editLink: 'Upravit odkaz', | |
source: 'Zdroj' | |
} | |
}, | |
upload: { | |
serverPath: '/reportaze/cro/upload/ajax', | |
fileFieldName: 'fileToUpload', | |
data: [], | |
success: undefined, | |
error: undefined | |
}, | |
opts: { | |
on: { | |
cro: { | |
events: 'showcode', | |
handler: function (event, t) { | |
if (t.$box.hasClass(t.o.prefix + 'editor-visible')) { | |
t.toggle() | |
}; | |
} | |
} | |
}, | |
btnsDef: { | |
crImage: { | |
fn: function (params, tbw) { | |
var id; | |
tbw.saveSelection(); | |
var $modal = tbw.openModalInsert( | |
// Title | |
tbw.lang.crImage, | |
/** Fields */ | |
{ | |
imageId: { | |
type: '', | |
required: true | |
} | |
}, | |
// Callback | |
function (values) { | |
tbw.restoreSelection(); | |
tbw.execCmd('insertText', '[croimage]' + values.imageId + '[/croimage]'); | |
return true; | |
}); | |
$modal.find('.trumbowyg-modal-box').append('<div class="image-title">Název: <span></span></div><div class="image-preview"></div><div class="image-author">autor: <span></div>'); | |
$modal.height(330); | |
$('input[name=imageId]').on('change', function (e) { | |
var id = $(this).val(); | |
var $preview = $modal.find('.image-preview'); | |
var $title = $modal.find('.image-title span'); | |
var $author = $modal.find('.image-author span'); | |
$preview.empty(); | |
$author.empty(); | |
$title.empty(); | |
$title.append('Nahrávám...'); | |
$.getJSON('http://bridge.rozhlas.cz/rscrapi/media?rscr_id=' + id, function (data) { | |
if (data.detail == 'Not Found') { | |
$title.empty(); | |
$title.append('Nesprávné ID.'); | |
} else { | |
var image = new Image(); | |
var width = 133; | |
var height = 133 * (data.properties.height / data.properties.width); | |
image.onload = function () { | |
$preview.empty(); | |
$preview.append('<img src="' + this.src + '" alt="" title="" class="image" width="' + width + '" height="' + height + '"/>'); | |
}; | |
image.onerror = function () { | |
$preview.empty(); | |
$preview.append('Chyba, pÅ™i naÄÃtánà obrázku.'); | |
}; | |
image.src = 'http://media.rozhlas.cz/_obrazek/' + id + '----1-' + width + 'x' + height + '.jpg'; | |
$title.empty(); | |
var ellipsis = ''; | |
if (data.title.length > 60) { | |
ellipsis = '...'; | |
} | |
$title.text(data.title.substring(0, 60) + ellipsis); | |
$author.empty(); | |
var authors = []; | |
for (var i in data._embedded.authors) { | |
var a = data._embedded.authors[i]; | |
authors[i] = [a.prefix, a.first_name, a.last_name, a.suffix, ].join(' '); | |
} | |
$author.text(authors.join(', ')); | |
} | |
}); | |
}); | |
} | |
}, | |
crAudio: { | |
fn: function (params, tbw) { | |
var id; | |
tbw.saveSelection(); | |
var $modal = tbw.openModalInsert( | |
// Title | |
tbw.lang.crAudio, | |
/** Fields */ | |
{ | |
audioId: { | |
type: '', | |
required: true | |
} | |
}, | |
// Callback | |
function (values) { | |
; | |
tbw.restoreSelection(); | |
tbw.execCmd('insertText', '[croaudio]' + values.audioId + '[/croaudio]'); | |
return true; | |
}); | |
//'' | |
$modal.find('.trumbowyg-modal-box').append('<div class="audio-preview"></div>'); | |
$modal.height(350); | |
$('input[name=audioId]').on('change', function (e) { | |
var $preview = $modal.find('.audio-preview'); | |
$preview.empty(); | |
$preview.append('<iframe src="http://prehravac.rozhlas.cz/audio/' + $(this).val() + '/embedded" scrolling="no" style="border:none;height:160px;width:100%;overflow:hidden"></iframe>'); | |
}); | |
} | |
}, | |
crUpload: { | |
fn: function (params, tbw) { | |
var file, | |
pfx = tbw.o.prefix; | |
tbw.saveSelection(); | |
var $modal = tbw.openModalInsert( | |
// Title | |
tbw.lang.crUpload, | |
/** Fields */ | |
{ | |
fileToUpload: { | |
type: 'file', | |
required: true | |
}, | |
author: { | |
type: '', | |
required: false | |
} | |
}, | |
// Callback | |
function (values) { | |
var data = new FormData(); | |
data.append($.trumbowyg.upload.fileFieldName, file); | |
tbw.restoreSelection(); | |
$.trumbowyg.upload.data.map(function (cur) { | |
data.append(cur.name, cur.value); | |
}); | |
if ($('.' + pfx + 'progress', $modal).length === 0) | |
$('.' + pfx + 'modal-title', $modal).after($('<div/>', { | |
'class': pfx + 'progress' | |
}).append($('<div/>', { | |
'class': pfx + 'progress-bar' | |
}))); | |
$.ajax({ | |
url: $.trumbowyg.upload.serverPath, | |
type: 'POST', | |
data: data, | |
cache: false, | |
dataType: 'json', | |
processData: false, | |
contentType: false, | |
progressUpload: function (e) { | |
$('.' + pfx + 'progress-bar').stop().animate({ | |
width: Math.round(e.loaded * 100 / e.total) + '%' | |
}, 200); | |
}, | |
success: $.trumbowyg.upload.success || function (data) { | |
if (data.status == "ok") { | |
tbw.execCmd('insertText', '[s3image author="' + values.author + '"]' + data.data.fid + '[/s3image]'); | |
setTimeout(function () { | |
tbw.closeModal(); | |
}, 250); | |
} else { | |
tbw.addErrorOnModalField($('input[type=file]', $modal), tbw.lang[data.message]); | |
} | |
}, | |
error: $.trumbowyg.upload.error || function (data) { | |
tbw.addErrorOnModalField($('input[type=file]', $modal), tbw.lang.uploadError); | |
} | |
}); | |
}); | |
$('input[type=file]').on('change', function (e) { | |
try { | |
// If multiple files allowed, we just get the first. | |
file = e.target.files[0]; | |
} catch (err) { | |
// In IE8, multiple files not allowed | |
file = e.target.value; | |
} | |
}); | |
} | |
}, | |
crSource: { | |
ico: 'addSource', | |
fn: function (params, tbw) { | |
var src; | |
tbw.saveSelection(); | |
var $modal = tbw.openModalInsert( | |
// Title | |
tbw.lang.crSource, | |
/** Fields */ | |
{ | |
source: { | |
type: '', | |
required: true | |
} | |
}, | |
// Callback | |
function (values) { | |
tbw.restoreSelection(); | |
tbw.execCmd('insertText', '[crosource]' + values.source + '[/crosource]'); | |
return true; | |
}); | |
} | |
}, | |
crInsert: { | |
ico: 'insertImage', | |
dropdown: ['crImage', 'crUpload', 'crAudio', ] | |
} | |
} | |
} | |
}); | |
function addXhrProgressEvent() { | |
if (!$.trumbowyg && !$.trumbowyg.addedXhrProgressEvent) { // Avoid adding progress event multiple times | |
var originalXhr = $.ajaxSettings.xhr; | |
$.ajaxSetup({ | |
xhr: function () { | |
var req = originalXhr(), | |
that = this; | |
if (req && typeof req.upload == "object" && that.progressUpload !== undefined) | |
req.upload.addEventListener("progress", function (e) { | |
that.progressUpload(e); | |
}, false); | |
return req; | |
} | |
}); | |
$.trumbowyg.addedXhrProgressEvent = true; | |
} | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment