/* Mozilla Ubiquity Bartle Command
Written by David Futcher (bobbo) <bobbo@ubuntu.com>
Heavily based on an earlier command to do the same thing (thanks!)
Do whatever the hell you want with this */
// max of 140 chars is recommended, but it really allows 160
const BARTLE_STATUS_MAXLEN = 160;
CmdUtils.CreateCommand({
name: "bartle.doomicile.de",
icon: "",
takes: {status: noun_arb_text},
modifiers: {},
preview: function(previewBlock, statusText) {
var charsLeft = BARTLE_STATUS_MAXLEN - statusText.text.length;
var truncCharsLeft = charsLeft;
if (charsLeft < 0) charsLeft = 0
var previewTemplate = "Updates your Bartle status to: <br /><b>${status}</b><br /><br />Characters remaining: <b>${chars}</b>";
var truncateTemplate = "<br />The last <b>${truncate}</b> characters will be truncated!";
var previewData = {
status: statusText.text,
chars: charsLeft
};
var previewHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
if(previewData.chars <= 0) {
var truncateData = {
truncate: 0 - truncCharsLeft
};
previewHTML += CmdUtils.renderTemplate(truncateTemplate, truncateData);
}
previewBlock.innerHTML = previewHTML;
},
execute: function(statusText) {
if(statusText.text.length < 1) {
displayMessage("Bartle requires a status to be entered");
return;
}
var updateUrl = "http://bartle.doomicile.de/api/statuses/update.json";
var updateParams = {
source: "ubiquity",
status: statusText.text
};
jQuery.ajax({
type: "POST",
url: updateUrl,
data: updateParams,
dataType: "json",
error: function() {
displayMessage("Bartle error - status not updated");
},
success: function() {
displayMessage("Bartle status updated");
}
});
}
});