PageRenderTime 113ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/commands/mozrepl/command.js

https://github.com/Gozala/ubiquity-commands
JavaScript | 189 lines | 167 code | 22 blank | 0 comment | 11 complexity | 803953073a88cf285c803815f0bb7c0a MD5 | raw file
  1. var noun_type_connection = {
  2. _name: "mozrepl connection",
  3. readAllFromSocket : function (host,port,outputData,listener) {
  4. try {
  5. var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService(Components.interfaces.nsISocketTransportService);
  6. var transport = transportService.createTransport(null,0,host,port,null);
  7. var outstream = transport.openOutputStream(0,0,0);
  8. outstream.write(outputData,outputData.length);
  9. var stream = transport.openInputStream(0,0,0);
  10. var instream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
  11. instream.init(stream);
  12. var dataListener = {
  13. data : "",
  14. onStartRequest: function(request, context){},
  15. onStopRequest: function(request, context, status){
  16. instream.close();
  17. outstream.close();
  18. listener.finished(this.data);
  19. },
  20. onDataAvailable: function(request, context, inputStream, offset, count){
  21. this.data += instream.read(count);
  22. }
  23. };
  24. var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance(Components.interfaces.nsIInputStreamPump);
  25. pump.init(stream, -1, -1, 0, 0, false);
  26. pump.asyncRead(dataListener,null);
  27. } catch (ex){
  28. return ex;
  29. }
  30. return null;
  31. },
  32. get openConnections() {
  33. return Application.storage.get('ubiquity-repl-openConnections',[]);
  34. },
  35. get recentConnections() {
  36. return Application.prefs.get('ubiquity.commands.repl.recentConnections','localhost:4242').split('|');
  37. },
  38. suggest : function(text, html) {
  39. var suggestions = [];
  40. if (!noun_type_jira.baseJiraURL) {
  41. var message = 'Set JIRA url to : '+text;
  42. suggestions.push({
  43. text : message,
  44. html : '<img src="'+noun_type_jira.iconJiraURL+'" alt="" width="16px"/> '+message,
  45. summary: '<span style="color: red">You need to set jira url</span>',
  46. data : {
  47. action : function () {
  48. noun_type_jira.baseJiraURL = text.substr(-1)=="/"?text:text+"/";
  49. }
  50. }
  51. });
  52. } else {
  53. var findMessage = 'Find in JIRA';
  54. suggestions.push({
  55. text : findMessage,
  56. html : '<img src="'+noun_type_jira.iconJiraURL+'" alt="" width="16px"/> '+findMessage+' : '+text,
  57. summary: findMessage,
  58. data : {
  59. action : function () {
  60. window.getBrowser().selectedTab = window.getBrowser().addTab(noun_type_jira.searchJiraURL+text);
  61. }
  62. }
  63. });
  64. var createMessage = 'Create new issue';
  65. suggestions.push({
  66. text : createMessage,
  67. html : '<img src="'+noun_type_jira.iconJiraURL+'" alt="" width="16px"/> '+createMessage,
  68. summary: createMessage,
  69. data : {
  70. action : function () {
  71. window.getBrowser().selectedTab = window.getBrowser().addTab(noun_type_jira.createJiraURL);
  72. }
  73. }
  74. });
  75. }
  76. if (!noun_type_jira.baseCrucibleURL) {
  77. message = 'Set Crucible url to : '+text;
  78. suggestions.push({
  79. text : message,
  80. html : '<img src="'+noun_type_jira.iconCrucibleURL+'" alt="" width="16px"/> '+message,
  81. summary: '<span style="color: red">You need to set Crusible url</span>',
  82. data : {
  83. action : function() {
  84. noun_type_jira.baseCrucibleURL = text.substr(-1)=="/"?text:text+"/";
  85. }
  86. }
  87. });
  88. } else {
  89. var findMessage = 'Find in Crucible';
  90. suggestions.push({
  91. text : findMessage,
  92. html : '<img src="'+noun_type_jira.iconCrucibleURL+'" alt="" width="16px"/> '+findMessage+' : <b>'+text+'</b>',
  93. summary: findMessage,
  94. data : {
  95. action : function() {
  96. window.getBrowser().selectedTab = window.getBrowser().addTab(noun_type_jira.searchCrucibleURL+text);
  97. }
  98. }
  99. });
  100. var createMessage = 'Create new review';
  101. suggestions.push({
  102. text : createMessage,
  103. html : '<img src="'+noun_type_jira.iconCrucibleURL+'" alt="" width="16px"/> '+createMessage,
  104. summary: createMessage,
  105. data : {
  106. action : function() {
  107. window.getBrowser().selectedTab = window.getBrowser().addTab(noun_type_jira.createCrucibleURL);
  108. }
  109. }
  110. });
  111. }
  112. return suggestions;
  113. }
  114. };
  115. var noun_type_mozreplAction = new CmdUtils.NounType("mozrepl actions",['eval','inspect','enter']);
  116. var connection = null;
  117. var MozReplClient = {
  118. eval : function(expression) {
  119. return window.eval(expression);
  120. }
  121. }
  122. CmdUtils.CreateCommand({
  123. name: 'repl',
  124. icon: 'http://hyperstruct.net/sites/hyperstruct.net/files/zen-hyperstruct_favicon.png',
  125. description: 'mozrepl clinet build in Ubiquity',
  126. author: { name: "Irakli Gozalishvili", email: "Irakli.Gozalishvili@tomtom.com"},
  127. homepage: 'http://rfobic.blogspot.com/2008/10/ubiquity-MozRepl.html',
  128. help: 'Type everything the way if it was a console and soon you\'ll discover diff!!',
  129. takes: {
  130. 'expression' : noun_arb_text
  131. },
  132. modifiers: {
  133. 'do' : noun_type_mozreplAction
  134. },
  135. preview: function(pblock, expression, mods) {
  136. var connection = 1;
  137. var action = mods.do.text || eval
  138. if (connection) {
  139. switch (action) {
  140. case 'inspect':
  141. var description = 'MozRepl will inspect :';
  142. break;
  143. case 'enter' :
  144. var description = 'MozRepl will enter :';
  145. break;
  146. default:
  147. var description = 'MozRepl will evaluate :';
  148. }
  149. pblock.innerHTML = '<i>'+description+'</i><br/><br/><code style="white-space: pre;">'+expression.text+'</code>';
  150. } else {
  151. pblock.innerHTML = '<span style="color: red">Please use repl-connect first</span>';
  152. }
  153. },
  154. execute: function(expression,mods) {
  155. var action = mods.do.text || eval
  156. switch (action) {
  157. case 'inspect':
  158. displayMessage(MozReplClient.eval(expression.text).toSource());
  159. break;
  160. case 'enter' :
  161. var description = 'MozRepl will enter :';
  162. break;
  163. default:
  164. displayMessage(MozReplClient.eval(expression.text).toString());
  165. }
  166. }
  167. });