wcs/wcs/qommon/static/js/fargo.js

72 lines
2.5 KiB
JavaScript

$(function() {
var iframe = $('<iframe frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>');
var dialog = $("<div></div>").append(iframe).appendTo("body").dialog({
autoOpen: false,
modal: true,
resizable: false,
width: "auto",
height: "auto",
close: function () {
iframe.attr("src", "");
}
});
$('.file-validation a').click(function (e) {
e.preventDefault();
var src = $(e.target).attr('href');
var title = $(e.target).data("title");
iframe.attr({
src: src
});
iframe.css({'width': '100%', 'min-height': '25em'});
iframe.on('load', function() {
iframe.off('load');
iframe[0].contentWindow.postMessage(JSON.stringify({'message': 'helloFargo'}), '*');
$(window).on('message', function(ev) {
var data = JSON.parse(ev.originalEvent.data);
if (data.message == 'fargoButtons') {
buttons = data.buttons;
buttons[0].click = function() { $(this).dialog('close'); return false; };
buttons[1].click = function() {
iframe[0].contentWindow.postMessage(JSON.stringify({'message': 'validate'}), '*');
}
dialog.dialog('option', 'buttons', buttons);
dialog.dialog("option", "title", title);
dialog.dialog("open");
}
});
});
});
$('p.use-file-from-fargo span').click(function(e) {
e.preventDefault();
var base_widget = $(this).parents('.file-upload-widget');
document.fargo_set_token = function (token, title) {
if (token) {
$(base_widget).find('.filename').text(title);
$(base_widget).find('.fileinfo').show();
$(base_widget).find('input[type=hidden]').val(token);
$(base_widget).find('input[type=file]').hide();
$(base_widget).find('.use-file-from-fargo').hide();
$(base_widget).find('input[type=file]').trigger('change');
$(base_widget).addClass('has-file').removeClass('has-no-file');
}
document.fargo_close_dialog();
}
document.fargo_close_dialog = function () {
document.fargo_set_token = undefined;
dialog.dialog('close');
}
var src = $(this).data('src');
var title = $(this).data("title");
var width = $(this).data("width");
var height = $(this).data("height");
iframe.attr({
width: parseInt(width),
height: parseInt(height),
src: src
});
dialog.dialog("option", "title", title);
dialog.dialog("open");
});
});