Created
December 17, 2014 15:44
-
-
Save lperrin/1e56d2e0f4c09e15b957 to your computer and use it in GitHub Desktop.
Plugin API
This file contains hidden or 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
var _ = require('underscore'); | |
module.exports = function (ngModule) { | |
ngModule.service('pluginapi', function ($window, $rootScope, team, conversationsSelection, singleConversation) { | |
this.publish = function () { | |
var allowedDomain = $window.__allowEmbed; | |
if (!allowedDomain) | |
return; | |
$rootScope.$on('plugin_event', function ($event, data) { | |
$window.parent.postMessage(data, allowedDomain); | |
}); | |
function execPluginCommand(data) { | |
var replyDefaults = { | |
body: '', | |
subject: null, | |
send_now: false, | |
mark_archived: false, | |
to: null | |
}; | |
switch (data.type) { | |
case 'assign': | |
var assignee = team.getTeammate(data.assignee); | |
if (assignee) | |
conversationsSelection.assignSelection(assignee); | |
else | |
console.error('unknown assignee', data.assignee); | |
break; | |
case 'unassign': | |
conversationsSelection.assignSelection(null); | |
break; | |
case 'toggle_archived': | |
conversationsSelection.toggleArchived(); | |
break; | |
case 'toggle_trashed': | |
conversationsSelection.toggleTrashed(); | |
break; | |
case 'reply': | |
data = _(data).defaults(replyDefaults); | |
data.reply_type = 'single'; | |
singleConversation.sendReplyToLatest(data); | |
break; | |
case 'reply_all': | |
data = _(data).defaults(replyDefaults); | |
data.reply_type = 'all'; | |
singleConversation.sendReplyToLatest(data); | |
break; | |
} | |
} | |
$window.execPluginCommand = execPluginCommand; | |
$window.addEventListener('message', function (event) { | |
if (event.origin !== allowedDomain) { | |
if (event.origin !== 'https://app.frontapp.com') | |
console.error('ignoring message from unknown domain', event.origin); | |
return; | |
} | |
$rootScope.$apply(function () { | |
execPluginCommand(event.data); | |
}); | |
}); | |
}; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment