Module: Tap::Ubiquity::Utils
- Defined in:
- lib/tap/ubiquity/utils.rb
Overview
Utils provides methods for serving ubiquity commands that link back to actions on a controller.
:startdoc:::-
# ::controller
# ::ubiquity
class Sample < Tap::Controller
include Ubiquity::Utils
def commands
serve command(:action)
end
def action
input = request['input']
request.get? ? "got: #{input}" : "received: #{input}"
end
end
:startdoc:::+
Utils expects to be included in a Tap::Controller.
Class Method Summary collapse
-
.command(action, attrs = {}) ⇒ Object
Formats a Ubiquity command that calls action with an ‘input’ parameter containing the currently selected text.
-
.command_link(action = self.action) ⇒ Object
Returns the link tag for subscribing to a ubiquity command served from the specified action.
-
.command_request? ⇒ Boolean
Returns true if the current request indicates the command javascript should be returned by serve (ie the path_info ends with ‘.js’).
-
.js_injection(action, attrs = {}, inject = true) ⇒ Object
class Sample < Tap::Controller include Ubiquity::Utils.
-
.serve(command) ⇒ Object
Serves the command from the calling method.
Class Method Details
.command(action, attrs = {}) ⇒ Object
Formats a Ubiquity command that calls action with an ‘input’ parameter containing the currently selected text. If the request is a get, the action results will be set in the preview box. Otherwise the current page is redirected to the action url.
– TODO name should be minipath, not action
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/tap/ubiquity/utils.rb', line 59 def command(action, attrs={}) attrs = {:name => action}.merge(attrs) %Q{ var cmd = #{attrs.to_json}; cmd.takes = {"selection": noun_arb_text}; cmd.preview = function( pblock, selection ) { jQuery.ajax({ type: 'GET', url: '#{uri(action)}', data: {input: selection.text}, success: function(data) { pblock.innerHTML = data; }, error: function(XMLHttpRequest, textStatus, errorThrown) { pblock.innerHTML = "Could not generate preview: " + errorThrown; }, }); }; cmd.execute = function(selection) { var document = Application.activeWindow.activeTab.document; var form = document.createElement("form"); form.setAttribute('action', '#{uri(action)}'); form.setAttribute('style', 'display:none'); form.setAttribute('method', 'POST'); var input = document.createElement('input'); input.setAttribute('name', 'input'); input.setAttribute('type', 'text'); input.value = selection.text; form.appendChild(input); document.body.appendChild(form); form.submit(); }; CmdUtils.CreateCommand(cmd); } end |
.command_link(action = self.action) ⇒ Object
Returns the link tag for subscribing to a ubiquity command served from the specified action.
48 49 50 |
# File 'lib/tap/ubiquity/utils.rb', line 48 def command_link(action=self.action) "<link rel='commands' href='#{uri(action)}.js' />" end |
.command_request? ⇒ Boolean
Returns true if the current request indicates the command javascript should be returned by serve (ie the path_info ends with ‘.js’)
42 43 44 |
# File 'lib/tap/ubiquity/utils.rb', line 42 def command_request? File.extname(request.path_info) == '.js' end |
.js_injection(action, attrs = {}, inject = true) ⇒ Object
class Sample < Tap::Controller
include Ubiquity::Utils
def commands
serve js_injection(:action)
end
def action
response['Content-Type'] = 'text/plain'
"alert('#{request['input']}')"
end
end
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/tap/ubiquity/utils.rb', line 112 def js_injection(action, attrs={}, inject=true) attrs = {:name => action}.merge(attrs) %Q{ var cmd = #{attrs.to_json}; cmd.takes = {"selection": noun_arb_text}; cmd.preview = function( pblock, selection ) { jQuery.ajax({ type: 'GET', url: '#{uri(action)}', data: {input: selection.text}, success: function(data) { pblock.innerHTML = data; }, error: function(XMLHttpRequest, textStatus, errorThrown) { pblock.innerHTML = "Could not generate preview: " + errorThrown; }, }); }; cmd.execute = function(selection) { jQuery.ajax({ type: 'POST', url: '#{uri(action)}', data: {input: selection.text}, success: function(data) { if(#{inject ? 'true' : 'false'}) { var doc = Application.activeWindow.activeTab.document; var script = doc.createElement("script"); script.type = "text/javascript"; script.innerHTML = data; doc.body.appendChild(script); } else { eval(data); } }, error: function(XMLHttpRequest, textStatus, errorThrown) { displayMessage("Could not execute: " + errorThrown); }, }); }; CmdUtils.CreateCommand(cmd);} end |
.serve(command) ⇒ Object
Serves the command from the calling method.
31 32 33 34 35 36 37 38 |
# File 'lib/tap/ubiquity/utils.rb', line 31 def serve(command) if command_request? response['Content-Type'] = 'application/x-javascript' command else block_given? ? yield(command_link) : command_link end end |