events.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2016 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Events fired as a result of actions in Blockly's editor.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. /**
  26. * Events fired as a result of actions in Blockly's editor.
  27. * @namespace Blockly.Events
  28. */
  29. goog.provide('Blockly.Events');
  30. goog.require('goog.array');
  31. goog.require('goog.math.Coordinate');
  32. /**
  33. * Group ID for new events. Grouped events are indivisible.
  34. * @type {string}
  35. * @private
  36. */
  37. Blockly.Events.group_ = '';
  38. /**
  39. * Sets whether events should be added to the undo stack.
  40. * @type {boolean}
  41. */
  42. Blockly.Events.recordUndo = true;
  43. /**
  44. * Allow change events to be created and fired.
  45. * @type {number}
  46. * @private
  47. */
  48. Blockly.Events.disabled_ = 0;
  49. /**
  50. * Name of event that creates a block.
  51. * @const
  52. */
  53. Blockly.Events.CREATE = 'create';
  54. /**
  55. * Name of event that deletes a block.
  56. * @const
  57. */
  58. Blockly.Events.DELETE = 'delete';
  59. /**
  60. * Name of event that changes a block.
  61. * @const
  62. */
  63. Blockly.Events.CHANGE = 'change';
  64. /**
  65. * Name of event that moves a block.
  66. * @const
  67. */
  68. Blockly.Events.MOVE = 'move';
  69. /**
  70. * Name of event that records a UI change.
  71. * @const
  72. */
  73. Blockly.Events.UI = 'ui';
  74. /**
  75. * List of events queued for firing.
  76. * @private
  77. */
  78. Blockly.Events.FIRE_QUEUE_ = [];
  79. /**
  80. * Create a custom event and fire it.
  81. * @param {!Blockly.Events.Abstract} event Custom data for event.
  82. */
  83. Blockly.Events.fire = function(event) {
  84. if (!Blockly.Events.isEnabled()) {
  85. return;
  86. }
  87. if (!Blockly.Events.FIRE_QUEUE_.length) {
  88. // First event added; schedule a firing of the event queue.
  89. setTimeout(Blockly.Events.fireNow_, 0);
  90. }
  91. Blockly.Events.FIRE_QUEUE_.push(event);
  92. };
  93. /**
  94. * Fire all queued events.
  95. * @private
  96. */
  97. Blockly.Events.fireNow_ = function() {
  98. var queue = Blockly.Events.filter(Blockly.Events.FIRE_QUEUE_, true);
  99. Blockly.Events.FIRE_QUEUE_.length = 0;
  100. for (var i = 0, event; event = queue[i]; i++) {
  101. var workspace = Blockly.Workspace.getById(event.workspaceId);
  102. if (workspace) {
  103. workspace.fireChangeListener(event);
  104. }
  105. }
  106. };
  107. /**
  108. * Filter the queued events and merge duplicates.
  109. * @param {!Array.<!Blockly.Events.Abstract>} queueIn Array of events.
  110. * @param {boolean} forward True if forward (redo), false if backward (undo).
  111. * @return {!Array.<!Blockly.Events.Abstract>} Array of filtered events.
  112. */
  113. Blockly.Events.filter = function(queueIn, forward) {
  114. var queue = goog.array.clone(queueIn);
  115. if (!forward) {
  116. // Undo is merged in reverse order.
  117. queue.reverse();
  118. }
  119. // Merge duplicates. O(n^2), but n should be very small.
  120. for (var i = 0, event1; event1 = queue[i]; i++) {
  121. for (var j = i + 1, event2; event2 = queue[j]; j++) {
  122. if (event1.type == event2.type &&
  123. event1.blockId == event2.blockId &&
  124. event1.workspaceId == event2.workspaceId) {
  125. if (event1.type == Blockly.Events.MOVE) {
  126. // Merge move events.
  127. event1.newParentId = event2.newParentId;
  128. event1.newInputName = event2.newInputName;
  129. event1.newCoordinate = event2.newCoordinate;
  130. queue.splice(j, 1);
  131. j--;
  132. } else if (event1.type == Blockly.Events.CHANGE &&
  133. event1.element == event2.element &&
  134. event1.name == event2.name) {
  135. // Merge change events.
  136. event1.newValue = event2.newValue;
  137. queue.splice(j, 1);
  138. j--;
  139. } else if (event1.type == Blockly.Events.UI &&
  140. event2.element == 'click' &&
  141. (event1.element == 'commentOpen' ||
  142. event1.element == 'mutatorOpen' ||
  143. event1.element == 'warningOpen')) {
  144. // Merge change events.
  145. event1.newValue = event2.newValue;
  146. queue.splice(j, 1);
  147. j--;
  148. }
  149. }
  150. }
  151. }
  152. // Remove null events.
  153. for (var i = queue.length - 1; i >= 0; i--) {
  154. if (queue[i].isNull()) {
  155. queue.splice(i, 1);
  156. }
  157. }
  158. if (!forward) {
  159. // Restore undo order.
  160. queue.reverse();
  161. }
  162. // Move mutation events to the top of the queue.
  163. // Intentionally skip first event.
  164. for (var i = 1, event; event = queue[i]; i++) {
  165. if (event.type == Blockly.Events.CHANGE &&
  166. event.element == 'mutation') {
  167. queue.unshift(queue.splice(i, 1)[0]);
  168. }
  169. }
  170. return queue;
  171. };
  172. /**
  173. * Modify pending undo events so that when they are fired they don't land
  174. * in the undo stack. Called by Blockly.Workspace.clearUndo.
  175. */
  176. Blockly.Events.clearPendingUndo = function() {
  177. for (var i = 0, event; event = Blockly.Events.FIRE_QUEUE_[i]; i++) {
  178. event.recordUndo = false;
  179. }
  180. };
  181. /**
  182. * Stop sending events. Every call to this function MUST also call enable.
  183. */
  184. Blockly.Events.disable = function() {
  185. Blockly.Events.disabled_++;
  186. };
  187. /**
  188. * Start sending events. Unless events were already disabled when the
  189. * corresponding call to disable was made.
  190. */
  191. Blockly.Events.enable = function() {
  192. Blockly.Events.disabled_--;
  193. };
  194. /**
  195. * Returns whether events may be fired or not.
  196. * @return {boolean} True if enabled.
  197. */
  198. Blockly.Events.isEnabled = function() {
  199. return Blockly.Events.disabled_ == 0;
  200. };
  201. /**
  202. * Current group.
  203. * @return {string} ID string.
  204. */
  205. Blockly.Events.getGroup = function() {
  206. return Blockly.Events.group_;
  207. };
  208. /**
  209. * Start or stop a group.
  210. * @param {boolean|string} state True to start new group, false to end group.
  211. * String to set group explicitly.
  212. */
  213. Blockly.Events.setGroup = function(state) {
  214. if (typeof state == 'boolean') {
  215. Blockly.Events.group_ = state ? Blockly.genUid() : '';
  216. } else {
  217. Blockly.Events.group_ = state;
  218. }
  219. };
  220. /**
  221. * Compute a list of the IDs of the specified block and all its descendants.
  222. * @param {!Blockly.Block} block The root block.
  223. * @return {!Array.<string>} List of block IDs.
  224. * @private
  225. */
  226. Blockly.Events.getDescendantIds_ = function(block) {
  227. var ids = [];
  228. var descendants = block.getDescendants();
  229. for (var i = 0, descendant; descendant = descendants[i]; i++) {
  230. ids[i] = descendant.id;
  231. }
  232. return ids;
  233. };
  234. /**
  235. * Decode the JSON into an event.
  236. * @param {!Object} json JSON representation.
  237. * @param {!Blockly.Workspace} workspace Target workspace for event.
  238. * @return {!Blockly.Events.Abstract} The event represented by the JSON.
  239. */
  240. Blockly.Events.fromJson = function(json, workspace) {
  241. var event;
  242. switch (json.type) {
  243. case Blockly.Events.CREATE:
  244. event = new Blockly.Events.Create(null);
  245. break;
  246. case Blockly.Events.DELETE:
  247. event = new Blockly.Events.Delete(null);
  248. break;
  249. case Blockly.Events.CHANGE:
  250. event = new Blockly.Events.Change(null);
  251. break;
  252. case Blockly.Events.MOVE:
  253. event = new Blockly.Events.Move(null);
  254. break;
  255. case Blockly.Events.UI:
  256. event = new Blockly.Events.Ui(null);
  257. break;
  258. default:
  259. throw 'Unknown event type.';
  260. }
  261. event.fromJson(json);
  262. event.workspaceId = workspace.id;
  263. return event;
  264. };
  265. /**
  266. * Abstract class for an event.
  267. * @param {Blockly.Block} block The block.
  268. * @constructor
  269. */
  270. Blockly.Events.Abstract = function(block) {
  271. if (block) {
  272. this.blockId = block.id;
  273. this.workspaceId = block.workspace.id;
  274. }
  275. this.group = Blockly.Events.group_;
  276. this.recordUndo = Blockly.Events.recordUndo;
  277. };
  278. /**
  279. * Encode the event as JSON.
  280. * @return {!Object} JSON representation.
  281. */
  282. Blockly.Events.Abstract.prototype.toJson = function() {
  283. var json = {
  284. 'type': this.type,
  285. };
  286. if (this.blockId) {
  287. json['blockId'] = this.blockId;
  288. }
  289. if (this.group) {
  290. json['group'] = this.group;
  291. }
  292. return json;
  293. };
  294. /**
  295. * Decode the JSON event.
  296. * @param {!Object} json JSON representation.
  297. */
  298. Blockly.Events.Abstract.prototype.fromJson = function(json) {
  299. this.blockId = json['blockId'];
  300. this.group = json['group'];
  301. };
  302. /**
  303. * Does this event record any change of state?
  304. * @return {boolean} True if null, false if something changed.
  305. */
  306. Blockly.Events.Abstract.prototype.isNull = function() {
  307. return false;
  308. };
  309. /**
  310. * Run an event.
  311. * @param {boolean} forward True if run forward, false if run backward (undo).
  312. */
  313. Blockly.Events.Abstract.prototype.run = function(forward) {
  314. // Defined by subclasses.
  315. };
  316. /**
  317. * Class for a block creation event.
  318. * @param {Blockly.Block} block The created block. Null for a blank event.
  319. * @extends {Blockly.Events.Abstract}
  320. * @constructor
  321. */
  322. Blockly.Events.Create = function(block) {
  323. if (!block) {
  324. return; // Blank event to be populated by fromJson.
  325. }
  326. Blockly.Events.Create.superClass_.constructor.call(this, block);
  327. this.xml = Blockly.Xml.blockToDomWithXY(block);
  328. this.ids = Blockly.Events.getDescendantIds_(block);
  329. };
  330. goog.inherits(Blockly.Events.Create, Blockly.Events.Abstract);
  331. /**
  332. * Type of this event.
  333. * @type {string}
  334. */
  335. Blockly.Events.Create.prototype.type = Blockly.Events.CREATE;
  336. /**
  337. * Encode the event as JSON.
  338. * @return {!Object} JSON representation.
  339. */
  340. Blockly.Events.Create.prototype.toJson = function() {
  341. var json = Blockly.Events.Create.superClass_.toJson.call(this);
  342. json['xml'] = Blockly.Xml.domToText(this.xml);
  343. json['ids'] = this.ids;
  344. return json;
  345. };
  346. /**
  347. * Decode the JSON event.
  348. * @param {!Object} json JSON representation.
  349. */
  350. Blockly.Events.Create.prototype.fromJson = function(json) {
  351. Blockly.Events.Create.superClass_.fromJson.call(this, json);
  352. this.xml = Blockly.Xml.textToDom('<xml>' + json['xml'] + '</xml>').firstChild;
  353. this.ids = json['ids'];
  354. };
  355. /**
  356. * Run a creation event.
  357. * @param {boolean} forward True if run forward, false if run backward (undo).
  358. */
  359. Blockly.Events.Create.prototype.run = function(forward) {
  360. var workspace = Blockly.Workspace.getById(this.workspaceId);
  361. if (forward) {
  362. var xml = goog.dom.createDom('xml');
  363. xml.appendChild(this.xml);
  364. Blockly.Xml.domToWorkspace(xml, workspace);
  365. } else {
  366. for (var i = 0, id; id = this.ids[i]; i++) {
  367. var block = workspace.getBlockById(id);
  368. if (block) {
  369. block.dispose(false, false);
  370. } else if (id == this.blockId) {
  371. // Only complain about root-level block.
  372. console.warn("Can't uncreate non-existant block: " + id);
  373. }
  374. }
  375. }
  376. };
  377. /**
  378. * Class for a block deletion event.
  379. * @param {Blockly.Block} block The deleted block. Null for a blank event.
  380. * @extends {Blockly.Events.Abstract}
  381. * @constructor
  382. */
  383. Blockly.Events.Delete = function(block) {
  384. if (!block) {
  385. return; // Blank event to be populated by fromJson.
  386. }
  387. if (block.getParent()) {
  388. throw 'Connected blocks cannot be deleted.';
  389. }
  390. Blockly.Events.Delete.superClass_.constructor.call(this, block);
  391. this.oldXml = Blockly.Xml.blockToDomWithXY(block);
  392. this.ids = Blockly.Events.getDescendantIds_(block);
  393. };
  394. goog.inherits(Blockly.Events.Delete, Blockly.Events.Abstract);
  395. /**
  396. * Type of this event.
  397. * @type {string}
  398. */
  399. Blockly.Events.Delete.prototype.type = Blockly.Events.DELETE;
  400. /**
  401. * Encode the event as JSON.
  402. * @return {!Object} JSON representation.
  403. */
  404. Blockly.Events.Delete.prototype.toJson = function() {
  405. var json = Blockly.Events.Delete.superClass_.toJson.call(this);
  406. json['ids'] = this.ids;
  407. return json;
  408. };
  409. /**
  410. * Decode the JSON event.
  411. * @param {!Object} json JSON representation.
  412. */
  413. Blockly.Events.Delete.prototype.fromJson = function(json) {
  414. Blockly.Events.Delete.superClass_.fromJson.call(this, json);
  415. this.ids = json['ids'];
  416. };
  417. /**
  418. * Run a deletion event.
  419. * @param {boolean} forward True if run forward, false if run backward (undo).
  420. */
  421. Blockly.Events.Delete.prototype.run = function(forward) {
  422. var workspace = Blockly.Workspace.getById(this.workspaceId);
  423. if (forward) {
  424. for (var i = 0, id; id = this.ids[i]; i++) {
  425. var block = workspace.getBlockById(id);
  426. if (block) {
  427. block.dispose(false, false);
  428. } else if (id == this.blockId) {
  429. // Only complain about root-level block.
  430. console.warn("Can't delete non-existant block: " + id);
  431. }
  432. }
  433. } else {
  434. var xml = goog.dom.createDom('xml');
  435. xml.appendChild(this.oldXml);
  436. Blockly.Xml.domToWorkspace(xml, workspace);
  437. }
  438. };
  439. /**
  440. * Class for a block change event.
  441. * @param {Blockly.Block} block The changed block. Null for a blank event.
  442. * @param {string} element One of 'field', 'comment', 'disabled', etc.
  443. * @param {?string} name Name of input or field affected, or null.
  444. * @param {string} oldValue Previous value of element.
  445. * @param {string} newValue New value of element.
  446. * @extends {Blockly.Events.Abstract}
  447. * @constructor
  448. */
  449. Blockly.Events.Change = function(block, element, name, oldValue, newValue) {
  450. if (!block) {
  451. return; // Blank event to be populated by fromJson.
  452. }
  453. Blockly.Events.Change.superClass_.constructor.call(this, block);
  454. this.element = element;
  455. this.name = name;
  456. this.oldValue = oldValue;
  457. this.newValue = newValue;
  458. };
  459. goog.inherits(Blockly.Events.Change, Blockly.Events.Abstract);
  460. /**
  461. * Type of this event.
  462. * @type {string}
  463. */
  464. Blockly.Events.Change.prototype.type = Blockly.Events.CHANGE;
  465. /**
  466. * Encode the event as JSON.
  467. * @return {!Object} JSON representation.
  468. */
  469. Blockly.Events.Change.prototype.toJson = function() {
  470. var json = Blockly.Events.Change.superClass_.toJson.call(this);
  471. json['element'] = this.element;
  472. if (this.name) {
  473. json['name'] = this.name;
  474. }
  475. json['newValue'] = this.newValue;
  476. return json;
  477. };
  478. /**
  479. * Decode the JSON event.
  480. * @param {!Object} json JSON representation.
  481. */
  482. Blockly.Events.Change.prototype.fromJson = function(json) {
  483. Blockly.Events.Change.superClass_.fromJson.call(this, json);
  484. this.element = json['element'];
  485. this.name = json['name'];
  486. this.newValue = json['newValue'];
  487. };
  488. /**
  489. * Does this event record any change of state?
  490. * @return {boolean} True if something changed.
  491. */
  492. Blockly.Events.Change.prototype.isNull = function() {
  493. return this.oldValue == this.newValue;
  494. };
  495. /**
  496. * Run a change event.
  497. * @param {boolean} forward True if run forward, false if run backward (undo).
  498. */
  499. Blockly.Events.Change.prototype.run = function(forward) {
  500. var workspace = Blockly.Workspace.getById(this.workspaceId);
  501. var block = workspace.getBlockById(this.blockId);
  502. if (!block) {
  503. console.warn("Can't change non-existant block: " + this.blockId);
  504. return;
  505. }
  506. if (block.mutator) {
  507. // Close the mutator (if open) since we don't want to update it.
  508. block.mutator.setVisible(false);
  509. }
  510. var value = forward ? this.newValue : this.oldValue;
  511. switch (this.element) {
  512. case 'field':
  513. var field = block.getField(this.name);
  514. if (field) {
  515. // Run the validator for any side-effects it may have.
  516. // The validator's opinion on validity is ignored.
  517. field.callValidator(value);
  518. field.setValue(value);
  519. } else {
  520. console.warn("Can't set non-existant field: " + this.name);
  521. }
  522. break;
  523. case 'comment':
  524. block.setCommentText(value || null);
  525. break;
  526. case 'collapsed':
  527. block.setCollapsed(value);
  528. break;
  529. case 'disabled':
  530. block.setDisabled(value);
  531. break;
  532. case 'inline':
  533. block.setInputsInline(value);
  534. break;
  535. case 'mutation':
  536. var oldMutation = '';
  537. if (block.mutationToDom) {
  538. var oldMutationDom = block.mutationToDom();
  539. oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
  540. }
  541. if (block.domToMutation) {
  542. value = value || '<mutation></mutation>';
  543. var dom = Blockly.Xml.textToDom('<xml>' + value + '</xml>');
  544. block.domToMutation(dom.firstChild);
  545. }
  546. Blockly.Events.fire(new Blockly.Events.Change(
  547. block, 'mutation', null, oldMutation, value));
  548. break;
  549. default:
  550. console.warn('Unknown change type: ' + this.element);
  551. }
  552. };
  553. /**
  554. * Class for a block move event. Created before the move.
  555. * @param {Blockly.Block} block The moved block. Null for a blank event.
  556. * @extends {Blockly.Events.Abstract}
  557. * @constructor
  558. */
  559. Blockly.Events.Move = function(block) {
  560. if (!block) {
  561. return; // Blank event to be populated by fromJson.
  562. }
  563. Blockly.Events.Move.superClass_.constructor.call(this, block);
  564. var location = this.currentLocation_();
  565. this.oldParentId = location.parentId;
  566. this.oldInputName = location.inputName;
  567. this.oldCoordinate = location.coordinate;
  568. };
  569. goog.inherits(Blockly.Events.Move, Blockly.Events.Abstract);
  570. /**
  571. * Type of this event.
  572. * @type {string}
  573. */
  574. Blockly.Events.Move.prototype.type = Blockly.Events.MOVE;
  575. /**
  576. * Encode the event as JSON.
  577. * @return {!Object} JSON representation.
  578. */
  579. Blockly.Events.Move.prototype.toJson = function() {
  580. var json = Blockly.Events.Move.superClass_.toJson.call(this);
  581. if (this.newParentId) {
  582. json['newParentId'] = this.newParentId;
  583. }
  584. if (this.newInputName) {
  585. json['newInputName'] = this.newInputName;
  586. }
  587. if (this.newCoordinate) {
  588. json['newCoordinate'] = Math.round(this.newCoordinate.x) + ',' +
  589. Math.round(this.newCoordinate.y);
  590. }
  591. return json;
  592. };
  593. /**
  594. * Decode the JSON event.
  595. * @param {!Object} json JSON representation.
  596. */
  597. Blockly.Events.Move.prototype.fromJson = function(json) {
  598. Blockly.Events.Move.superClass_.fromJson.call(this, json);
  599. this.newParentId = json['newParentId'];
  600. this.newInputName = json['newInputName'];
  601. if (json['newCoordinate']) {
  602. var xy = json['newCoordinate'].split(',');
  603. this.newCoordinate =
  604. new goog.math.Coordinate(parseFloat(xy[0]), parseFloat(xy[1]));
  605. }
  606. };
  607. /**
  608. * Record the block's new location. Called after the move.
  609. */
  610. Blockly.Events.Move.prototype.recordNew = function() {
  611. var location = this.currentLocation_();
  612. this.newParentId = location.parentId;
  613. this.newInputName = location.inputName;
  614. this.newCoordinate = location.coordinate;
  615. };
  616. /**
  617. * Returns the parentId and input if the block is connected,
  618. * or the XY location if disconnected.
  619. * @return {!Object} Collection of location info.
  620. * @private
  621. */
  622. Blockly.Events.Move.prototype.currentLocation_ = function() {
  623. var workspace = Blockly.Workspace.getById(this.workspaceId);
  624. var block = workspace.getBlockById(this.blockId);
  625. var location = {};
  626. var parent = block.getParent();
  627. if (parent) {
  628. location.parentId = parent.id;
  629. var input = parent.getInputWithBlock(block);
  630. if (input) {
  631. location.inputName = input.name;
  632. }
  633. } else {
  634. location.coordinate = block.getRelativeToSurfaceXY();
  635. }
  636. return location;
  637. };
  638. /**
  639. * Does this event record any change of state?
  640. * @return {boolean} True if something changed.
  641. */
  642. Blockly.Events.Move.prototype.isNull = function() {
  643. return this.oldParentId == this.newParentId &&
  644. this.oldInputName == this.newInputName &&
  645. goog.math.Coordinate.equals(this.oldCoordinate, this.newCoordinate);
  646. };
  647. /**
  648. * Run a move event.
  649. * @param {boolean} forward True if run forward, false if run backward (undo).
  650. */
  651. Blockly.Events.Move.prototype.run = function(forward) {
  652. var workspace = Blockly.Workspace.getById(this.workspaceId);
  653. var block = workspace.getBlockById(this.blockId);
  654. if (!block) {
  655. console.warn("Can't move non-existant block: " + this.blockId);
  656. return;
  657. }
  658. var parentId = forward ? this.newParentId : this.oldParentId;
  659. var inputName = forward ? this.newInputName : this.oldInputName;
  660. var coordinate = forward ? this.newCoordinate : this.oldCoordinate;
  661. var parentBlock = null;
  662. if (parentId) {
  663. parentBlock = workspace.getBlockById(parentId);
  664. if (!parentBlock) {
  665. console.warn("Can't connect to non-existant block: " + parentId);
  666. return;
  667. }
  668. }
  669. if (block.getParent()) {
  670. block.unplug();
  671. }
  672. if (coordinate) {
  673. var xy = block.getRelativeToSurfaceXY();
  674. block.moveBy(coordinate.x - xy.x, coordinate.y - xy.y);
  675. } else {
  676. var blockConnection = block.outputConnection || block.previousConnection;
  677. var parentConnection;
  678. if (inputName) {
  679. var input = parentBlock.getInput(inputName);
  680. if (input) {
  681. parentConnection = input.connection;
  682. }
  683. } else if (blockConnection.type == Blockly.PREVIOUS_STATEMENT) {
  684. parentConnection = parentBlock.nextConnection;
  685. }
  686. if (parentConnection) {
  687. blockConnection.connect(parentConnection);
  688. } else {
  689. console.warn("Can't connect to non-existant input: " + inputName);
  690. }
  691. }
  692. };
  693. /**
  694. * Class for a UI event.
  695. * @param {Blockly.Block} block The affected block.
  696. * @param {string} element One of 'selected', 'comment', 'mutator', etc.
  697. * @param {string} oldValue Previous value of element.
  698. * @param {string} newValue New value of element.
  699. * @extends {Blockly.Events.Abstract}
  700. * @constructor
  701. */
  702. Blockly.Events.Ui = function(block, element, oldValue, newValue) {
  703. Blockly.Events.Ui.superClass_.constructor.call(this, block);
  704. this.element = element;
  705. this.oldValue = oldValue;
  706. this.newValue = newValue;
  707. this.recordUndo = false;
  708. };
  709. goog.inherits(Blockly.Events.Ui, Blockly.Events.Abstract);
  710. /**
  711. * Type of this event.
  712. * @type {string}
  713. */
  714. Blockly.Events.Ui.prototype.type = Blockly.Events.UI;
  715. /**
  716. * Encode the event as JSON.
  717. * @return {!Object} JSON representation.
  718. */
  719. Blockly.Events.Ui.prototype.toJson = function() {
  720. var json = Blockly.Events.Ui.superClass_.toJson.call(this);
  721. json['element'] = this.element;
  722. if (this.newValue !== undefined) {
  723. json['newValue'] = this.newValue;
  724. }
  725. return json;
  726. };
  727. /**
  728. * Decode the JSON event.
  729. * @param {!Object} json JSON representation.
  730. */
  731. Blockly.Events.Ui.prototype.fromJson = function(json) {
  732. Blockly.Events.Ui.superClass_.fromJson.call(this, json);
  733. this.element = json['element'];
  734. this.newValue = json['newValue'];
  735. };
  736. /**
  737. * Enable/disable a block depending on whether it is properly connected.
  738. * Use this on applications where all blocks should be connected to a top block.
  739. * Recommend setting the 'disable' option to 'false' in the config so that
  740. * users don't try to reenable disabled orphan blocks.
  741. * @param {!Blockly.Events.Abstract} event Custom data for event.
  742. */
  743. Blockly.Events.disableOrphans = function(event) {
  744. if (event.type == Blockly.Events.MOVE ||
  745. event.type == Blockly.Events.CREATE) {
  746. Blockly.Events.disable();
  747. var workspace = Blockly.Workspace.getById(event.workspaceId);
  748. var block = workspace.getBlockById(event.blockId);
  749. if (block) {
  750. if (block.getParent() && !block.getParent().disabled) {
  751. var children = block.getDescendants();
  752. for (var i = 0, child; child = children[i]; i++) {
  753. child.setDisabled(false);
  754. }
  755. } else if ((block.outputConnection || block.previousConnection) &&
  756. Blockly.dragMode_ == Blockly.DRAG_NONE) {
  757. do {
  758. block.setDisabled(true);
  759. block = block.getNextBlock();
  760. } while (block);
  761. }
  762. }
  763. Blockly.Events.enable();
  764. }
  765. };