Solr Admin Helper

Options

当前为 2014-08-01 提交的版本,查看 最新版本

  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.4
  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('tick...');
  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. $(document).on('click', function() {
  45. console.log("now there are " + $('#form button:last').length + " buttons on this page.");
  46. if ($('#solrRecordModifier').length > 0 || $('#form button:last').length == 0) {
  47. console.log('tick...');
  48. return;
  49. }
  50. addCss (
  51. '.ui-dialog-content fieldset { border: 0; text-align: left ! important; }'
  52. + '.ui-dialog-content label, .ui-dialog-content input { display: block; }'
  53. + '.ui-dialog-content input.text { margin-bottom: 12px; padding: 0.4em; width: 95%; }'
  54. );
  55. function getSelection() {
  56. if (window.getSelection) {
  57. return window.getSelection().toString();
  58. } else if (document.selection && document.selection.type != "Control") {
  59. return document.selection.createRange().text;
  60. }
  61. }
  62. function deleteRecord(id) {
  63. var command = {
  64. "delete": { "id": id }
  65. };
  66. return $.ajax({
  67. url: location.protocol + '//' + location.host + '/solr/feedback/update/?commit=true'
  68. , type : 'POST'
  69. , contentType : 'application/json'
  70. , dataType : 'json'
  71. , data: JSON.stringify(command)
  72. });
  73. }
  74. function modifyRecord(id, field, value) {
  75. var record = {};
  76. record.id = id;
  77. record[field] = { "set": value };
  78. return $.ajax({
  79. url: location.protocol + '//' + location.host + '/solr/feedback/update/?commit=true'
  80. , type : 'POST'
  81. , contentType : 'application/json'
  82. , dataType : 'json'
  83. , data: JSON.stringify([record])
  84. });
  85. }
  86. function setUpModifyButton() {
  87. var $modifyRecordDialog = $('<div title="Modify Record">'
  88. + '<form><fieldset>'
  89. + '<label for="solrRecordModifier_id">ID</label>'
  90. + '<input type="text" name="solrRecordModifier_id" id="solrRecordModifier_id" value="" class="text ui-widget-content ui-corner-all" size="70">'
  91. + '<label for="solrRecordModifier_field">Field</label>'
  92. + '<input type="text" name="solrRecordModifier_field" id="solrRecordModifier_field" value="" class="text ui-widget-content ui-corner-all" size="70">'
  93. + '<label for="solrRecordModifier_value">New Value</label>'
  94. + '<input type="text" name="solrRecordModifier_value" id="solrRecordModifier_value" value="" class="text ui-widget-content ui-corner-all" size="70">'
  95. + '<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">'
  96. + '</fieldset></from>'
  97. + '</div>');
  98. var $idInput = $modifyRecordDialog.find('#solrRecordModifier_id');
  99. var $fieldInput = $modifyRecordDialog.find('#solrRecordModifier_field');
  100. var $valueInput = $modifyRecordDialog.find('#solrRecordModifier_value');
  101. var modifyBtn = $('<button id="solrRecordModifier">Modify</button>');
  102. modifyBtn.insertAfter($('#form button:last'));
  103. modifyBtn.click(function() {
  104. $idInput.val(getSelection());
  105. $modifyRecordDialog.dialog({
  106. resizable: true,
  107. width:530,
  108. modal: true,
  109. buttons: {
  110. "Submit": function() {
  111. var dialog = this;
  112. modifyRecord($idInput.val(), $fieldInput.val(), $valueInput.val())
  113. .done(function() {
  114. $( dialog ).dialog( "close" );
  115. })
  116. .fail(function(jqXHR, textStatus) {
  117. alert('Failed to modify the specified record. \n\n' + jqXHR.responseText);
  118. });
  119. },
  120. Cancel: function() {
  121. $( this ).dialog( "close" );
  122. }
  123. }
  124. });
  125. });
  126. }
  127. function setUpDeleteButton() {
  128. var $deleteRecordDialog = $('<div title="Delete Record">'
  129. + '<p>Are sure to delete the record</p>'
  130. + '<div class="form"><form><fieldset><div class="fieldset">'
  131. + '<label for="solrRecordDeleter_id">ID</label>'
  132. + '<input type="text" name="solrRecordDeleter_id" id="solrRecordDeleter_id" value="" class="text ui-widget-content ui-corner-all" size="70">'
  133. + '</div></fieldset></from></div>'
  134. + '</div>');
  135. var $idInput = $deleteRecordDialog.find('#solrRecordDeleter_id');
  136. var deleteBtn = $('<button id="solrRecordDeleter">Delete</button>');
  137. deleteBtn.insertAfter($('#form button:last'));
  138. deleteBtn.click(function() {
  139. $idInput.val(getSelection());
  140. $deleteRecordDialog.dialog({
  141. resizable: true,
  142. width:530,
  143. modal: true,
  144. buttons: {
  145. "Delete": function() {
  146. var dialog = this;
  147. if ($idInput.val()) {
  148. deleteRecord($idInput.val())
  149. .done(function() {
  150. $( dialog ).dialog( "close" );
  151. })
  152. .fail(function(jqXHR, textStatus) {
  153. alert('Failed to delete the specified record. \n\n' + jqXHR.responseText);
  154. });
  155. }
  156. },
  157. Cancel: function(jqXHR, textStatus, errorThrown ) {
  158. $( this ).dialog( "close" );
  159. }
  160. }
  161. });
  162. });
  163. }
  164. setUpModifyButton();
  165. setUpDeleteButton();
  166. });
  167. });
  168.  
  169. });