Skip to content

Instantly share code, notes, and snippets.

@tad-lispy
Last active August 29, 2015 14:15
Show Gist options
  • Save tad-lispy/7bee05d43d1eb0bcd942 to your computer and use it in GitHub Desktop.
Save tad-lispy/7bee05d43d1eb0bcd942 to your computer and use it in GitHub Desktop.
Funcase - IAI communication proposal
(function() {
var data;
data = {
fabric: {
type: 'shop',
name: 'XCATS',
stuff: 'Cool cats',
staff: {
boss: 'Lionel King',
accountant: 'Marry A. Bobcat',
sales: ['Katie Cat', 'ms. Ocelot', 'Snow, Leopold']
}
}
};
jQuery(function($) {
var host;
if (window.top === window) {
return $('body').html("<h1>This application is supposed to be running in a frame</h1>");
}
host = window.top;
host.postMessage({
type: 'hello'
}, '*');
return window.addEventListener('message', function(message) {
var body, canvas, image, type, _ref;
if (message.origin !== 'https://xgsm.pl') {
console.error('This PostMessage event should be ignored. For test purposes it is not.');
}
_ref = message.data, type = _ref.type, body = _ref.body;
switch (type) {
case 'init':
canvas = document.createElement('canvas');
image = document.createElement('img');
$(image).one('load', function() {
canvas.id = 'overlay';
canvas.height = image.height;
canvas.width = image.width;
canvas.getContext('2d').drawImage(image, 0, 0);
return document.body.appendChild(canvas);
});
image.src = body.overlay;
if (image.complete || image.width) {
return $(image).load();
}
break;
case 'get data':
canvas = document.getElementById('overlay');
data.preview = canvas.toDataURL('image/png');
return message.source.postMessage({
type: 'data',
body: data
}, '*');
}
});
});
}).call(this);
jQuery(function($) {
var funcase;
funcase = document.getElementById('appframe').contentWindow;
window.addEventListener('message', function(message) {
var body, type, _ref;
if (message.origin !== 'http://funcase.lazurski.pl') {
console.error('This PostMessage event should be ignored. For test purposes it is not.');
}
_ref = message.data, type = _ref.type, body = _ref.body;
switch (type) {
case 'hello':
return message.source.postMessage({
type: 'init',
body: {
platform: 'IAI Shop',
api: '1.0',
overlay: '/cats.jpg'
}
}, '*');
}
});
return $('#sample-form').submit(function(event) {
var data, form;
event.preventDefault();
form = event.target;
data = new FormData(form);
funcase.postMessage({
type: 'get data'
}, '*');
return window.addEventListener('message', function(message) {
var blob, json;
if (message.origin !== 'http://funcase.lazurski.pl') {
console.error('This PostMessage event should be ignored. For test purposes it is not.');
}
json = JSON.stringify(message.data);
blob = new Blob([json], {
type: 'application/json'
});
data.append('attachment', blob);
return jQuery.ajax({
url: form.action,
data: data,
type: 'POST',
processData: false,
contentType: false
}).done(function(res) {
console.dir(res);
return $('#response-preview').html(JSON.stringify(res, null, 2));
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment