jsmind.shell.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Released under BSD License
  3. * Copyright (c) 2014-2021 hizzgdev@163.com
  4. *
  5. * Project Home:
  6. * https://github.com/hizzgdev/jsmind/
  7. */
  8. (function ($w) {
  9. 'use strict';
  10. var $d = $w.document;
  11. var __name__ = 'jsMind';
  12. var jsMind = $w[__name__];
  13. if (!jsMind) { return; }
  14. if (typeof (jsMind.shell) != 'undefined') { return; }
  15. var options = {
  16. play_delay: 1000
  17. };
  18. jsMind.shell = function (jm) {
  19. this.jm = jm;
  20. this.step = 0;
  21. this.commands = []; //version
  22. this.delay_handle = 0;
  23. this.playing = false;
  24. this.jm_editable = this.jm.get_editable();
  25. };
  26. jsMind.shell.prototype = {
  27. init: function () {
  28. this.playing = false;
  29. },
  30. record: function (action, obj) {
  31. if (!this.playing) {
  32. var command = { action: action, data: obj.data, node: obj.node };
  33. var prev_command = this.commands[this.step - 1];
  34. if (command.action === 'update_node' && prev_command.action === 'add_node' && prev_command.data[2] === 'New Node') {
  35. prev_command.data[2] = command.data[1];
  36. this.commands[this.step - 1] = prev_command;
  37. } else {
  38. this.step = this.commands.push(command);
  39. }
  40. }
  41. },
  42. execute: function (command) {
  43. var func = this.jm[command.action];
  44. var node = command.node;
  45. this.jm.enable_edit();
  46. func.apply(this.jm, command.data);
  47. this.jm.disable_edit();
  48. if (!!node) {
  49. this.jm.select_node(node);
  50. }
  51. },
  52. add_command: function (command) {
  53. this.commands.push(command);
  54. play();
  55. },
  56. replay: function () {
  57. this.step = 0;
  58. this.play();
  59. },
  60. play: function () {
  61. this.jm.disable_edit();
  62. this.playing = true;
  63. this._play_stepbystep();
  64. },
  65. _play_stepbystep: function () {
  66. if (this.delay_handle != 0) {
  67. $w.clearTimeout(this.delay_handle);
  68. this.delay_handle = 0;
  69. }
  70. if (this.step < this.commands.length) {
  71. this.execute(this.commands[this.step]);
  72. this.step++;
  73. var js = this;
  74. this.delay_handle = $w.setTimeout(function () {
  75. js.play.call(js);
  76. }, options.play_delay);
  77. } else {
  78. this._play_end();
  79. }
  80. },
  81. _play_end: function () {
  82. this.playing = false;
  83. if (this.jm_editable) {
  84. this.jm.enable_edit();
  85. } else {
  86. this.jm.disable_edit();
  87. }
  88. },
  89. jm_event_handle: function (type, data) {
  90. if (type === jsMind.event_type.show) {
  91. this.record('show', data);
  92. }
  93. if (type === jsMind.event_type.edit) {
  94. var action = data.evt;
  95. delete data.evt;
  96. this.record(action, data);
  97. }
  98. }
  99. };
  100. var shell_plugin = new jsMind.plugin('shell', function (jm) {
  101. var js = new jsMind.shell(jm);
  102. jm.shell = js;
  103. js.init();
  104. jm.add_event_listener(function (type, data) {
  105. js.jm_event_handle.call(js, type, data);
  106. });
  107. });
  108. jsMind.register_plugin(shell_plugin);
  109. })(window);