Advertisement
Guest User

Untitled

a guest
May 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. import { Template } from 'meteor/templating';
  2. import { Tracker } from 'meteor/tracker';
  3. import { Random } from 'meteor/random';
  4.  
  5. import './template.html';
  6. import './style.styl';
  7.  
  8. Template.Magnet.viewmodel({
  9. signal: 'mouse',
  10. share: ['mouseBagController', 'magnetController'],
  11. x: 0,
  12. y: 0,
  13. collection() {
  14. return this.doc()
  15. },
  16. checkBagForMagnet() {
  17. let Bag = this.storage()
  18.  
  19. let Magnets = this.collection()
  20.  
  21. let bagNotEmpty = function() {
  22. return Bag.length > 0
  23. }
  24.  
  25. let typeNotConnect = function() {
  26. return _.first(Bag).type !== "connect"
  27. }
  28.  
  29. if (bagNotEmpty() && typeNotConnect()) {
  30. let obj = Bag.shift();
  31. return obj;
  32. }
  33. },
  34. clicked: function(e) {
  35.  
  36. let magnet = this.checkBagForMagnet()
  37. if (magnet) {
  38. //algo for object placement on canvas
  39.  
  40. _.extend(magnet, { x: e.clientX + 10, y: e.clientY + 10 });
  41.  
  42. this.parent().stuff({obj: magnet})
  43. }
  44. },
  45. stuff(o) {
  46. this.parent.stuff(o, function(e, r) {
  47. if (e) {
  48. throw new Meteor.Error('magnet.stuff', e)
  49. }
  50. })
  51. }
  52. })
  53.  
  54. // var magnets = new Mongo.Collection(null);
  55. // we moved this to the world object. easier to observe and more elegant.
  56.  
  57. Template.MagneticObject.viewmodel({
  58. signal: 'mouse',
  59. share: ['magnetController'],
  60. hovering: false,
  61. touching: false,
  62. delta: {
  63. x: 0,
  64. y: 0
  65. },
  66. mousedown: function(e) {
  67. console.log(e.type);
  68. //Starts touching object upon mouse down
  69. this.touching(this.object(e));
  70. },
  71. object: function(e) {
  72. if (e) {
  73. return {
  74. x: e.pageX,
  75. y: e.pageY
  76. }
  77. }
  78. },
  79. magnetType() {
  80. if (this.type() == 'textbox') {
  81. return 'button'
  82. } else {
  83. return 'direct'
  84. }
  85. },
  86. stuff(obj) {
  87. this.parent().stuff({ _id: this._id(), obj: obj }, function(err, r) {
  88. if (err) {
  89. throw new Meteor.Error('magneticObject.stuff', err)
  90. }
  91. })
  92. },
  93. dragging: function() {
  94. //Starts dragging
  95. //While touching happens
  96. //Runs on move pointer
  97. if (this.touching()) {
  98. let mp = this.mp()
  99. this.delta().x = mp.pageX - this.touching().x;
  100. this.delta().y = mp.pageY - this.touching().y;
  101.  
  102. console.log("magneticObject.dragging" + " horz " + this.delta()
  103. .x + "px, vert " + this.delta().y + "px");
  104.  
  105. //change to stuff using parent method and {x, y}
  106. this.x(this.x() + this.delta().x)
  107. this.y(this.y() + this.delta().y)
  108.  
  109. this.touching(this.object(mp));
  110. this.stuff({ y: this.y(), x: this.x() })
  111. }
  112. },
  113. autorun: [
  114. function() {
  115. //Runs on every move pointer event,
  116. //stuffs dragging position
  117.  
  118. this.mp()
  119.  
  120. //Tracker.nonreactive prevents infinite loop & stack lock, ups the property in unreactive bubble
  121.  
  122. Tracker.nonreactive(() => { this.dragging(); });
  123. },
  124. function() {
  125. //Runs on every mouse up event
  126. //When mouse up is true
  127. //Stops touching object
  128. let mouseup = this.mu();
  129.  
  130. //Tracker.nonreactive prevents infinite loop & stack lock, ups the property in unreactive bubble
  131.  
  132. Tracker.nonreactive(() => {
  133. if (this.touching()) {
  134. this.touching.reset();
  135. //this.delta.reset();
  136. }
  137. });
  138.  
  139. }
  140. ]
  141. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement