-
-
Save sabberworm/48961 to your computer and use it in GitHub Desktop.
UnitConversion Ubiquity command (uses GNU Units)
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 UnitConversion = {}; | |
UnitConversion.convert = function(from, to, callback) { | |
jQuery.get('http://www.sabberworm.com/get_file/units/', {from: from, to: to}, callback); | |
}; | |
UnitConversion.conversionFinished = function(data, textStatus) { | |
if(data.indexOf('invalid') !== -1) { | |
displayMessage(data); | |
return; | |
} | |
CmdUtils.setSelection(data); | |
}; | |
UnitConversion.prepareFrom = function(modifiers) { | |
var from = CmdUtils.getSelection(); | |
if(modifiers['from']) { | |
from = modifiers['from'].text; | |
} | |
from = from.replace(/(\d+)\s*\/\s*(\d+)/, "$1|$2"); | |
from = from.replace(/(USD|\$)(d+)/, 'USD $2'); | |
return from; | |
}; | |
UnitConversion.getPreview = function(previewBlock, input, modifiers) { | |
var short_message = "Converts ${from} to ${to}"; | |
var long_message = CmdUtils.renderTemplate(short_message, {from: '${from} (${converted_from})', to: '${to}'}); | |
var from = UnitConversion.prepareFrom(modifiers); | |
var to = input.text; | |
previewBlock.innerHTML = CmdUtils.renderTemplate(short_message, {from: from, to: to}); | |
if(!from) { | |
return; | |
} | |
UnitConversion.convert(from, '', function(data, textStatus) { | |
previewBlock.innerHTML = CmdUtils.renderTemplate(long_message, {from: from, converted_from: data, to: to}); | |
}); | |
}; | |
CmdUtils.CreateCommand({ | |
name: 'unit-convert', | |
takes: {to: noun_arb_text}, | |
modifiers: {from: noun_arb_text}, | |
preview: UnitConversion.getPreview, | |
execute: function(input, modifiers) { | |
UnitConversion.convert(UnitConversion.prepareFrom(modifiers), input.text, UnitConversion.conversionFinished); | |
}, | |
description: "Converts the unit of the selected text (or the from modifier) to the specfied unit using GNU Units.", | |
author: {name: 'Raphael Schweikert', homepage: 'http://www.sabberworm.com/'} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment