Solr Admin Helper

Options

目前為 2014-08-06 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Solr Admin Helper
  3. // @author Hang Yuan
  4. // @namespace hyuan.solr
  5. // @description Options
  6. // @include //cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js
  7. // @version 1.1.5
  8. // @match */solr/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. require.config({
  13. packages: [
  14. { name: 'jquery', location: '//code.jquery.com/jquery-2.1.1.min', main: 'jquery-2.1.1.min' }
  15. , { name: 'jqueryui', location: '//code.jquery.com/ui/1.11.0', main: 'jquery-ui' }
  16. , { name: 'css', location: '//cdnjs.cloudflare.com/ajax/libs/require-css/0.1.1', main: 'css' }
  17. , { name: 'domReady', location: '//cdnjs.cloudflare.com/ajax/libs/require-domReady/2.0.1', main: 'domReady.min' }
  18. ]
  19. , shim: {
  20. 'jquery': { exports: 'jquery' }
  21. , 'jqueryui': { exports: 'jqueryui', deps: ['jquery', 'css!jqueryui/themes/smoothness/jquery-ui'] }
  22. }
  23. , map: {
  24. '*': {
  25. 'css': 'css' // or whatever the path to require-css is
  26. }
  27. }
  28. });
  29.  
  30. require(['jquery', 'jqueryui', 'css!jqueryui/themes/smoothness/jquery-ui', 'domReady'],
  31. function($) {
  32. $(document).ready(function() {
  33. console.log('initializing ...');
  34.  
  35. function addCss(cssString) {
  36. var head = document.getElementsByTagName('head')[0];
  37. //text-alignreturn unless head;
  38. var newCss = document.createElement('style');
  39. newCss.type = "text/css";
  40. newCss.innerHTML = cssString;
  41. head.appendChild(newCss);
  42. }
  43.  
  44. function getSelection() {
  45. if (window.getSelection) {
  46. return window.getSelection().toString();
  47. } else if (document.selection && document.selection.type != "Control") {
  48. return document.selection.createRange().text;
  49. }
  50. }
  51. var ID_REGEXP = /<str name="id">([^<]*)<\/str>/gi;
  52. function extractId(val) {
  53. var match = ID_REGEXP.exec($.trim(val));
  54. ID_REGEXP.lastIndex = 0; // XXX need to reset the lastIndex to make the RegExp object reusable
  55. return match && match[1];
  56. }
  57. function getRecordIds(q) {
  58. var command = {
  59. 'q': q
  60. ,'fl': 'id'
  61. };
  62. return $.ajax({
  63. url: location.protocol + '//' + location.host + '/solr/feedback/select/?wt=json&' + $.param(command)
  64. , type : 'GET'
  65. , contentType : 'application/json'
  66. , dataType : 'json'
  67. });
  68. }
  69. function deleteRecord(id) {
  70. var command = {
  71. "delete": { "id": id }
  72. };
  73. return $.ajax({
  74. url: location.protocol + '//' + location.host + '/solr/feedback/update/?commit=true'
  75. , type : 'POST'
  76. , contentType : 'application/json'
  77. , dataType : 'json'
  78. , data: JSON.stringify(command)
  79. });
  80. }
  81. function modifyRecord(id, field, value) {
  82. var record = {};
  83. record.id = id;
  84. record[field] = { "set": value };
  85. return $.ajax({
  86. url: location.protocol + '//' + location.host + '/solr/feedback/update/?commit=true'
  87. , type : 'POST'
  88. , contentType : 'application/json'
  89. , dataType : 'json'
  90. , data: JSON.stringify([record])
  91. });
  92. }
  93. function setUpModifyButton() {
  94. var $modifyRecordDialog = $('<div title="Modify Record">'
  95. + '<form><fieldset>'
  96. + '<label for="solrRecordModifier_id">ID</label>'
  97. + '<input type="text" name="solrRecordModifier_id" id="solrRecordModifier_id" value="" class="text ui-widget-content ui-corner-all" size="70">'
  98. + '<label for="solrRecordModifier_field">Field</label>'
  99. + '<input type="text" name="solrRecordModifier_field" id="solrRecordModifier_field" value="" class="text ui-widget-content ui-corner-all" size="70">'
  100. + '<label for="solrRecordModifier_value">New Value</label>'
  101. + '<input type="text" name="solrRecordModifier_value" id="solrRecordModifier_value" value="" class="text ui-widget-content ui-corner-all" size="70">'
  102. + '<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">'
  103. + '</fieldset></from>'
  104. + '</div>');
  105. $modifyRecordDialog.dialog({
  106. autoOpen: false,
  107. resizable: true,
  108. width:530,
  109. modal: true,
  110. buttons: {
  111. "Submit": function() {
  112. var dialog = this;
  113. modifyRecord($idInput.val(), $fieldInput.val(), $valueInput.val())
  114. .done(function() {
  115. $( dialog ).dialog( "close" );
  116. })
  117. .fail(function(jqXHR, textStatus) {
  118. alert('Failed to modify the specified record. \n\n' + jqXHR.responseText);
  119. });
  120. },
  121. Cancel: function() {
  122. $( this ).dialog( "close" );
  123. }
  124. }
  125. })
  126. var $idInput = $modifyRecordDialog.find('#solrRecordModifier_id');
  127. var $fieldInput = $modifyRecordDialog.find('#solrRecordModifier_field');
  128. var $valueInput = $modifyRecordDialog.find('#solrRecordModifier_value');
  129. var modifyBtn = $('<button id="solrRecordModifier">Modify</button>');
  130. modifyBtn.insertAfter($('#form button:last'));
  131. modifyBtn.click(function(event) {
  132. event.preventDefault();
  133. $idInput.val(extractId(getSelection()));
  134. $modifyRecordDialog.dialog('open');
  135. });
  136. }
  137. function setUpDeleteButton() {
  138. var $deleteRecordDialog = $('<div title="Delete Record">'
  139. + '<p>Are sure to delete the record</p>'
  140. + '<div class="form"><form><fieldset><div class="fieldset">'
  141. + '<label for="solrRecordDeleter_id">ID</label>'
  142. + '<input type="text" name="solrRecordDeleter_id" id="solrRecordDeleter_id" value="" class="text ui-widget-content ui-corner-all" size="70">'
  143. + '</div></fieldset></from></div>'
  144. + '</div>');
  145. $deleteRecordDialog.dialog({
  146. autoOpen: false,
  147. resizable: true,
  148. width:530,
  149. modal: true,
  150. buttons: {
  151. "Delete": function() {
  152. var dialog = this;
  153. if ($idInput.val()) {
  154. deleteRecord($idInput.val())
  155. .done(function() {
  156. $( dialog ).dialog( "close" );
  157. })
  158. .fail(function(jqXHR, textStatus) {
  159. alert('Failed to delete the specified record. \n\n' + jqXHR.responseText);
  160. });
  161. }
  162. },
  163. Cancel: function(jqXHR, textStatus, errorThrown ) {
  164. $( this ).dialog( "close" );
  165. }
  166. }
  167. });
  168. var $idInput = $deleteRecordDialog.find('#solrRecordDeleter_id');
  169. var deleteBtn = $('<button id="solrRecordDeleter">Delete</button>');
  170. deleteBtn.insertAfter($('#form button:last'));
  171. deleteBtn.click(function(event) {
  172. event.preventDefault();
  173. $idInput.val(extractId(getSelection()));
  174. $deleteRecordDialog.dialog('open');
  175. });
  176. }
  177. $(document).one('click', function() {
  178. addCss (
  179. '.ui-dialog-content fieldset { border: 0; text-align: left ! important; }'
  180. + '.ui-dialog-content label, .ui-dialog-content input { display: block; }'
  181. + '.ui-dialog-content input.text { margin-bottom: 12px; padding: 0.4em; width: 95%; }'
  182. );
  183. setUpModifyButton();
  184. setUpDeleteButton();
  185. });
  186. });
  187.  
  188. });