index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'jstree'],
  2. function ($, undefined, Backend, Table, Form, Template, jstree) {
  3. var SELECTED_NODE = null;
  4. var KEYDATA = null;
  5. var Controller = {
  6. jstreeoptions: function () {
  7. var options = {
  8. "types": {
  9. "root": {
  10. "icon": "/assets/addons/faredis/img/redis_16.png"
  11. },
  12. "db": {
  13. "icon": "/assets/addons/faredis/img/db_16.png"
  14. },
  15. "loading": {
  16. "icon": "/assets/addons/faredis/img/loading.gif"
  17. },
  18. "key": {
  19. "icon": "/assets/addons/faredis/img/key_16.png"
  20. }
  21. },
  22. "plugins": ["types", "wholerow", "contextmenu", "search"],
  23. "contextmenu": {
  24. "items": Controller.menu
  25. },
  26. "state": {
  27. 'opened': true
  28. },
  29. 'core': {
  30. "data": {
  31. "url": "faredis/index",
  32. },
  33. "check_callback": true,
  34. }
  35. };
  36. return options;
  37. },
  38. jstreeevent: function () {
  39. $('#tree').bind("select_node.jstree", function (event, data) {
  40. var inst = data.instance;
  41. var selectedNode = inst.get_node(data.selected);
  42. SELECTED_NODE = selectedNode;
  43. var level = $("[id^='" + selectedNode.id + "']").attr('aria-level');
  44. if (!level || typeof (level) == "undefined") {
  45. level = $("[id^='" + selectedNode.id + "'] a").attr('aria-level');
  46. }
  47. $('.addkey').hide();
  48. $('.string').hide();
  49. $('.set').hide();
  50. $('.hash').hide();
  51. $('.zset').hide();
  52. if (level == 2) {
  53. $('#c-newdb').val(selectedNode.id);
  54. $('.addkey').show();
  55. if (selectedNode.children.length > 0) {
  56. return;
  57. }
  58. inst.set_type(selectedNode, 'loading');
  59. Controller.loadChild(inst, selectedNode);
  60. } else if (level == 3) {
  61. var tmp = selectedNode.id.split('_');
  62. var db = tmp[0];
  63. var key = selectedNode.text;
  64. Controller.loadValue(db, key);
  65. }
  66. });
  67. },
  68. isJSON: function (str) {
  69. if (typeof str == 'string') {
  70. try {
  71. var obj = JSON.parse(str);
  72. if (typeof obj == 'object' && obj) {
  73. return true;
  74. } else return false;
  75. } catch (e) {
  76. return false;
  77. }
  78. }
  79. },
  80. jsonFormat: function (el) {
  81. if (Controller.isJSON($(el).val())) {
  82. $(el).val(JSON.stringify(eval('(' + $(el).val() + ')'), null, 4));
  83. }
  84. },
  85. loadChild: function (inst, selectedNode) {
  86. $.post("faredis/index/keys", {
  87. db: selectedNode.id - 1
  88. }, function (res) {
  89. selectedNode.children = [];
  90. $.each(res, function (i, item) {
  91. inst.create_node(selectedNode, item, 'last');
  92. });
  93. inst.open_node(selectedNode);
  94. inst.set_type(selectedNode, 'db');
  95. });
  96. },
  97. loadValue: function (db, key) {
  98. $.post('faredis/index/getValue', {
  99. db: db,
  100. key: key
  101. }, function (res) {
  102. //reset
  103. $('#c-value').val('');
  104. $('.size').text('');
  105. $('.set-value').val('');
  106. $('.hash-key').val('');
  107. $('.hash-value').val('');
  108. $('.zset-value').val('');
  109. $('.zset-score').val('');
  110. KEYDATA = res;
  111. $('.p-title').text("db" + db + "::" + key);
  112. $('.c-key').val(key);
  113. $('.c-db').val(db);
  114. $(".ttl").text(res.ttl);
  115. switch (res.type) {
  116. case 1: //string
  117. $('.string').show();
  118. $('.set').hide();
  119. $('.hash').hide();
  120. $('.zset').hide();
  121. $('#c-value').val(res.value);
  122. Controller.jsonFormat($('#c-value'));
  123. break;
  124. case 2: //set
  125. case 3: //list
  126. $('.type-lb').text(res.type == 2 ? "SET:" : "LIST:");
  127. $('.set').show();
  128. $('.string').hide();
  129. $('.hash').hide();
  130. $('.zset').hide();
  131. // $('#set-value').val(res.value);
  132. var html = '';
  133. if (res.type == 2) {
  134. res.value.forEach(v => {
  135. html += "<option value='" + v + "'>" + v + "</option>"
  136. });
  137. } else {
  138. var lidx = 0;
  139. res.value.forEach(v => {
  140. html += "<option value='" + lidx++ + "'>" + v + "</option>"
  141. });
  142. }
  143. $('#set-list').html(html);
  144. $('.size').text(res.value.length);
  145. break;
  146. case 4:
  147. $('.type-lb').text("ZSET:");
  148. $('.hash').hide();
  149. $('.string').hide();
  150. $('.set').hide();
  151. $('.zset').show();
  152. let count4 = 0;
  153. Object.keys(res.value).forEach(v => {
  154. html += "<option value='" + v + "'>" + v + "</option>"
  155. count4++;
  156. });
  157. $('#zset-list').html(html);
  158. $('.size').text(count4);
  159. break;
  160. case 5: //hash
  161. $('.type-lb').text("HASH:");
  162. $('.hash').show();
  163. $('.string').hide();
  164. $('.set').hide();
  165. $('.zset').hide();
  166. var html = '';
  167. let count = 0;
  168. Object.keys(res.value).forEach(v => {
  169. html += "<option value='" + v + "'>" + v + "</option>"
  170. count++;
  171. });
  172. $('#hash-list').html(html);
  173. $('.size').text(count);
  174. break;
  175. default:
  176. $('.string').hide();
  177. $('.set').hide();
  178. break;
  179. }
  180. });
  181. },
  182. menu: function (node) {
  183. var items = {
  184. "reload": {
  185. "label": "重载",
  186. "icon": "fa fa-refresh",
  187. "action": function (data) {
  188. console.log('reload');
  189. var inst = $.jstree.reference(data.reference);
  190. obj = inst.get_node(data.reference);
  191. $.post('faredis/index/reloadDb?db=' + obj.id, null, function (res) {
  192. console.log(res);
  193. $("#tree").jstree('set_text', obj, res.text);
  194. Controller.loadChild(inst, obj);
  195. })
  196. }
  197. },
  198. "flush": {
  199. "label": "清空",
  200. "icon": "fa fa-trash",
  201. "action": function (data) {
  202. console.log('flush');
  203. var inst = $.jstree.reference(data.reference);
  204. obj = inst.get_node(data.reference);
  205. console.log(obj);
  206. Layer.confirm("确认清空吗?", function () {
  207. $.post("faredis/index/flushDb", {
  208. db: obj.id
  209. }, function (res) {
  210. Layer.closeAll('dialog');
  211. $("#tree").jstree('set_text', SELECTED_NODE, "DB" + (obj.id - 1) + "(0)");
  212. var childNodes = inst.get_children_dom(obj);
  213. $.each(childNodes, function (index, item) {
  214. inst.delete_node(item);
  215. })
  216. });
  217. });
  218. }
  219. },
  220. };
  221. if (node.type == 'db') {
  222. return items;
  223. }
  224. },
  225. index: function () {
  226. $('#tree').jstree(Controller.jstreeoptions());
  227. Controller.jstreeevent();
  228. $('.rds_del').on('click', function () {
  229. Layer.confirm("确认删除?", function () {
  230. $.post("faredis/index/delKey", {
  231. db: KEYDATA.db,
  232. key: KEYDATA.key
  233. }, function (res) {
  234. Layer.closeAll('dialog');
  235. $("#tree").jstree('set_text', SELECTED_NODE, KEYDATA.key + "(Removed)");
  236. });
  237. });
  238. });
  239. $('.rds_ttl').on('click', function () {
  240. Layer.prompt({
  241. title: "修改TTL",
  242. value: KEYDATA.ttl
  243. },
  244. function (value, index, ele) {
  245. $.get("faredis/index/ttl", {
  246. db: KEYDATA.db,
  247. key: KEYDATA.key,
  248. ttl: value
  249. }, function (res) {
  250. Layer.close(index);
  251. $(".ttl").text(value);
  252. });
  253. }
  254. )
  255. });
  256. $('.rds_rename').on('click', function () {
  257. Layer.prompt({
  258. title: "重命名",
  259. value: KEYDATA.key
  260. },
  261. function (value, index, ele) {
  262. $.get("faredis/index/rename", {
  263. db: KEYDATA.db,
  264. key: KEYDATA.key,
  265. newkey: value
  266. }, function (res) {
  267. Layer.close(index);
  268. $("#tree").jstree('set_text', SELECTED_NODE, value);
  269. });
  270. }
  271. )
  272. });
  273. //set list
  274. $('#set-list').on('click', function () {
  275. if ($('.type-lb').first().text() == 'LIST:') {
  276. $('.set-value').val($(this).find("option:selected").text());
  277. } else
  278. $('.set-value').val($(this).val());
  279. Controller.jsonFormat($('.set-value'));
  280. });
  281. $('.rds_add_row').on('click', function () {
  282. console.log('add row')
  283. Layer.prompt({
  284. title: "增加新项",
  285. placeholder: '请输入新项的值'
  286. },
  287. function (value, index, ele) {
  288. $.get("faredis/index/addValue", {
  289. db: KEYDATA.db,
  290. key: KEYDATA.key,
  291. value: value
  292. }, function (res) {
  293. Layer.close(index);
  294. var treeNode = $('#tree').jstree(true).get_selected(true)[0];
  295. $("#tree").jstree("deselect_all", true);
  296. $('#tree').jstree('select_node', treeNode.id);
  297. });
  298. }
  299. )
  300. });
  301. $('.rds_add_row_hash').on('click', function () {
  302. Layer.prompt({
  303. title: "增加新项(key/value)",
  304. placeholder: '请输入新项的Key'
  305. },
  306. function (value, index, ele) {
  307. $.get("faredis/index/addValue", {
  308. db: KEYDATA.db,
  309. key: KEYDATA.key,
  310. hash_key: value,
  311. value: $('#hash_value').val()
  312. }, function (res) {
  313. Layer.close(index);
  314. var treeNode = $('#tree').jstree(true).get_selected(true)[0];
  315. $("#tree").jstree("deselect_all", true);
  316. $('#tree').jstree('select_node', treeNode.id);
  317. });
  318. });
  319. $('.layui-layer-content').append('<br/><input type="text" id="hash_value" class="layui-layer-input" placeholder="请输入value值" />')
  320. });
  321. $('.rds_add_row_zset').on('click', function () {
  322. Layer.prompt({
  323. title: "增加新项",
  324. placeholder: '请输入新项的Key'
  325. },
  326. function (value, index, ele) {
  327. $.get("faredis/index/addValue", {
  328. db: KEYDATA.db,
  329. key: KEYDATA.key,
  330. value: value,
  331. zset_score: $('#add_row_zset_score').val()
  332. }, function (res) {
  333. Layer.close(index);
  334. var treeNode = $('#tree').jstree(true).get_selected(true)[0];
  335. $("#tree").jstree("deselect_all", true);
  336. $('#tree').jstree('select_node', treeNode.id);
  337. });
  338. });
  339. $('.layui-layer-content').append('<br/><input type="text" id="add_row_zset_score" class="layui-layer-input" placeholder="请设置value的score" />')
  340. });
  341. $('.rds_delete_row').on('click', function () {
  342. var type = $(this).data('type');
  343. console.log("rds_delete_row", $('#' + type).find("option:selected").text());
  344. $.get("faredis/index/delValue", {
  345. db: KEYDATA.db,
  346. key: KEYDATA.key,
  347. value: $('#' + type).find("option:selected").text(),
  348. }, function (res) {
  349. if (res.code == 1) {
  350. var treeNode = $('#tree').jstree(true).get_selected(true)[0];
  351. $("#tree").jstree("deselect_all", true);
  352. $('#tree').jstree('select_node', treeNode.id);
  353. } else {
  354. Layer.alert("操作失败");
  355. }
  356. });
  357. });
  358. //hash
  359. $('#hash-list').on('click', function () {
  360. let key = $(this).val();
  361. $('.hash-key').val(key);
  362. $('.hash-value').val(KEYDATA.value[key]);
  363. Controller.jsonFormat($('.hash-value'));
  364. });
  365. //zset
  366. $('#zset-list').on('click', function () {
  367. let key = $(this).val();
  368. $('.zset-value').val(key);
  369. $('.zset-score').val(KEYDATA.value[key]);
  370. });
  371. $('#search_input').keyup(function () {
  372. $("#tree").jstree(true).search($('#search_input').val());
  373. });
  374. $('.newsource').hide();
  375. $('.hash-filed').hide();
  376. $('#c-newtype').on('changed.bs.select', function (e) {
  377. if (e.target.value == 'zset') {
  378. $('.newsource').show();
  379. } else {
  380. $('.newsource').hide();
  381. }
  382. if (e.target.value == 'hash') {
  383. $('.hash-filed').show();
  384. } else {
  385. $('.hash-filed').hide();
  386. }
  387. });
  388. Form.api.bindevent($("form[role=form]"), function (data, ret) {
  389. var treeNode = $('#tree').jstree(true).get_selected(true)[0];
  390. $("#tree").jstree("deselect_all", true);
  391. $('#tree').jstree('select_node', treeNode.id);
  392. });
  393. Form.api.bindevent($("form[role=form-add]"), function (data, ret) {
  394. Layer.confirm('新增成功,是否重新加载该库下所有keys?', function () {
  395. Layer.closeAll();
  396. $.post('faredis/index/reloadDb?db=' + SELECTED_NODE.id, null, function (res) {
  397. var inst = $.jstree.reference(SELECTED_NODE);
  398. $("#tree").jstree('set_text', SELECTED_NODE, res.text);
  399. Controller.loadChild(inst, SELECTED_NODE);
  400. })
  401. })
  402. $('#add-reset').trigger('click');
  403. });
  404. }
  405. };
  406. return Controller;
  407. });