spacedeck_whiteboard.js 29 KB
Newer Older
mntmn's avatar
mntmn committed
1
2
3
4
5
6
7

/*
  Spacedeck Whiteboard Directive
  This module registers a custom Vue directive that handles Whiteboard sections.
*/

function setup_whiteboard_directives() {
mntmn's avatar
mntmn committed
8
9
  var mode_touch = false;
  
mntmn's avatar
mntmn committed
10
  if ('ontouchstart' in window) {
mntmn's avatar
mntmn committed
11
    mode_touch = true;
mntmn's avatar
mntmn committed
12
13
14
15
16
17
18
19
20
    var edown = "touchstart";
    var emove = "touchmove";
    var eup = "touchend";
  } else {
    var edown = "mousedown";
    var emove = "mousemove";
    var eup = "mouseup";
  }

mntmn's avatar
mntmn committed
21
22
23
24
25
26
  // detect first touch event
  window.addEventListener('touchstart', function on_first_touch() {
    mode_touch = true;
    window.removeEventListener('touchstart', on_first_touch, false);
  }, false);

mntmn's avatar
mntmn committed
27
28
29
30
31
32
33
34
  Vue.directive('sd-whiteboard', {
    bind: function () {
      var el = this.el;

      $(el).on(edown, ".artifact", this.handle_mouse_down_artifact.bind(this));
      $(el).on("dblclick", ".artifact", this.handle_double_click_artifact.bind(this));
      $(el).on("keyup", ".artifact", this.handle_key_up_artifact.bind(this));
      $(el).on("keydown", ".artifact", this.handle_key_down_artifact.bind(this));
mntmn's avatar
mntmn committed
35
36
37
38
39
40
      $(el).bind("touchstart", this.handle_mouse_down_space.bind(this));
      $(el).bind("touchmove", this.handle_mouse_move.bind(this));
      $(el).bind("touchend", this.handle_mouse_up_space.bind(this));
      $(el).bind("mousedown", this.handle_mouse_down_space.bind(this));
      $(el).bind("mousemove", this.handle_mouse_move.bind(this));
      $(el).bind("mouseup", this.handle_mouse_up_space.bind(this));
mntmn's avatar
mntmn committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
      
      $(el).bind("wheel", this.handle_wheel_space.bind(this));

      $(document.body).bind("mouseleave", this.handle_mouse_leave.bind(this));

      $(el).find(".handle.resize-nw").bind(edown, function(e){this.handle_transform_mouse_down(e,1,1)}.bind(this));
      $(el).find(".handle.resize-n").bind(edown, function(e){this.handle_transform_mouse_down(e,0.5,1)}.bind(this));
      $(el).find(".handle.resize-ne").bind(edown, function(e){this.handle_transform_mouse_down(e,0,1)}.bind(this));
      $(el).find(".handle.resize-e").bind(edown, function(e){this.handle_transform_mouse_down(e,0,0.5)}.bind(this));
      $(el).find(".handle.resize-se").bind(edown, function(e){this.handle_transform_mouse_down(e,0,0)}.bind(this));
      $(el).find(".handle.resize-s").bind(edown, function(e){this.handle_transform_mouse_down(e,0.5,0)}.bind(this));
      $(el).find(".handle.resize-sw").bind(edown, function(e){this.handle_transform_mouse_down(e,1,0)}.bind(this));
      $(el).find(".handle.resize-w").bind(edown, function(e){this.handle_transform_mouse_down(e,1,0.5)}.bind(this));

      $(el).find(".edge-handle.resize-n").bind(edown, function(e){this.handle_transform_mouse_down(e,0.5,1)}.bind(this));
      $(el).find(".edge-handle.resize-s").bind(edown, function(e){this.handle_transform_mouse_down(e,0.5,0)}.bind(this));
      $(el).find(".edge-handle.resize-e").bind(edown, function(e){this.handle_transform_mouse_down(e,0,0.5)}.bind(this));
      $(el).find(".edge-handle.resize-w").bind(edown, function(e){this.handle_transform_mouse_down(e,1,0.5)}.bind(this));

      $(el).on(edown, ".vector-handle", function(e){this.handle_vector_transform_mouse_down(e)}.bind(this));

      var $scope = this.vm.$root;

      this.space_zoom = 1;
      this.artifacts_before_transaction = [];
      $scope.active_tool = "pointer";
    },
    update: function () {
    },
    unbind: function () {
      // do clean up work
      // e.g. remove event listeners added in bind()

      var el = this.el;
      $(el).off(edown+" "+emove+" "+eup+" "+"keyup keydown mouseleave");
      $(document.body).unbind("mouseleave");
    },

    handle_key_down_artifact: function(evt) {
      var $scope = this.vm.$root;
    },

    handle_key_up_artifact: function(evt) {
      var $scope = this.vm.$root;
    },

    handle_mouse_down_artifact: function(evt) {
      var $scope = this.vm.$root;

      if (!$scope.editing_artifact_id) {
        evt.preventDefault();
        evt.stopPropagation();
      }

      if ($scope.active_tool == "zoom") return;

mntmn's avatar
mntmn committed
97
98
99
100
101
102
103
104
      if (evt.which == 2) {
        // middle mouse button
        this.handle_mouse_down_space(evt);
        return;
      }
      
      var a = $scope.find_artifact_by_id(evt.currentTarget.id.replace("artifact-",""));

mntmn's avatar
mntmn committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
      if ($scope.active_tool == "eyedrop") {
        var arts = $scope.selected_artifacts();
        if (!$scope.is_selected(a) && arts.length > 0) {
          // copy style from clicked artifact to selected artifacts
          $scope.begin_transaction();

          $scope.update_selected_artifacts(function(selected_artifact) {
            selected_artifact.style = _.clone(a.style);
          });

          $scope.active_tool = "pointer";
          return;
        }
      }

      if ($scope.active_tool == "pan") {
        this.start_pan(evt);
        return;
      }

      if ($scope.active_tool == "pointer") {
        if (!$scope.is_selected(a) || evt.shiftKey) {
          this.select(evt,a);
        }

        // copy via alt+move
        if (evt.altKey) {
          a = $scope.clone_artifact(a);
          this.select(evt,a);
        }
      }

      $scope.begin_transaction();

      var cursor = this.cursor_point_to_space(evt);
      $scope.mouse_ox = cursor.x;
      $scope.mouse_oy = cursor.y;
      $scope.mouse_moved = false;
      this.mouse_state = "move";
      evt.stopPropagation();
    },

    handle_double_click_artifact: function(evt) {
      var $scope = this.vm.$root;

      var a = $scope.find_artifact_by_id(evt.currentTarget.id.replace("artifact-",""));
      if (!a) return;
      
      if (a.payload_uri) {
        $scope.download_selected_artifacts();
      }

      $scope.toggle_selected_artifact_editing(true);
    },

    handle_transform_mouse_down: function(evt,origin_x,origin_y) {
      evt.stopPropagation();
      evt.preventDefault();

      var $scope = this.vm.$root;

      $scope.begin_transaction();

      var cursor = this.cursor_point_to_space(evt);
      this.mouse_state = "transform";
      $scope.mouse_ox = cursor.x;
      $scope.mouse_oy = cursor.y;
      $scope.transform_ox = origin_x;
      $scope.transform_oy = origin_y;
    },

    handle_vector_transform_mouse_down: function(evt) {
      evt.stopPropagation();
      evt.preventDefault();

      var $scope = this.vm.$root;

      var idx = parseInt($(evt.currentTarget).attr("data-idx"));
      $scope.selected_control_point_idx = idx;

      $scope.begin_transaction();
      var cursor = this.cursor_point_to_space(evt);
      this.mouse_state = "vector_transform";
      $scope.mouse_ox = cursor.x;
      $scope.mouse_oy = cursor.y;
      //$scope.transform_ox = origin_x;
      //$scope.transform_oy = origin_y;
    },

    handle_wheel_space: function(evt) {
      var $scope = this.vm.$root;

      if (!evt.ctrlKey && !evt.shiftKey) return;
      
      evt.preventDefault();
      evt.stopPropagation();
      
      var amount = 1;
      var dy = evt.originalEvent.deltaY;
      if (dy>0) {
        amount=1.2;
        if ($scope.viewport_zoom<=0.05) return false;
      } else if (dy<0) {
        amount=0.9;
        if ($scope.viewport_zoom>=2) return false;
      } else {
        return false;
      }
      $scope.zoom_to_cursor(evt,amount);
    },

    handle_mouse_down_space: function(evt) {
mntmn's avatar
mntmn committed
217
218
219
220
      if (evt.which != 2) {
        if (evt.target != evt.currentTarget && !_.include(["wrapper"],evt.target.className)) return;
      }
      
mntmn's avatar
mntmn committed
221
222
223
224
225
226
227
228
      var $scope = this.vm.$root;

      $scope.opened_dialog="none";

      var cursor = this.cursor_point_to_space(evt);
      $scope.mouse_ox = cursor.x;
      $scope.mouse_oy = cursor.y;

mntmn's avatar
mntmn committed
229
      if ((mode_touch && $scope.active_tool=="pointer") || evt.which == 2 || evt.buttons == 4) {
mntmn's avatar
mntmn committed
230
231
232
233
234
235
236
        $scope.active_tool = "pan";
      }
      
      if ($scope.active_tool=="note") {
        this.deselect();
        this.mouse_state = "transform";
        $scope.mouse_state = this.mouse_state;
mntmn's avatar
mntmn committed
237
        this.start_drawing_note(evt);
mntmn's avatar
mntmn committed
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
        return;

      } else if ($scope.active_tool=="arrow") {
        this.deselect();
        this.mouse_state = "vector_transform";
        $scope.mouse_state = this.mouse_state;
        this.start_drawing_arrow(evt);
        return;

      } else if ($scope.active_tool=="line") {
        this.deselect();
        this.mouse_state = "vector_transform";
        $scope.mouse_state = this.mouse_state;
        this.start_drawing_line(evt);
        return;

      } else if ($scope.active_tool=="scribble") {
        this.deselect();
        this.mouse_state = "scribble";
        $scope.mouse_state = this.mouse_state;
        this.start_drawing_scribble(evt);
        return;

      } else if ($scope.active_tool=="zoom") {
        if (evt.altKey) {
          $scope.zoom_out();
        } else {
          $scope.zoom_in();
        }
        return;

      } else if ($scope.active_tool=="pointer") {
        this.mouse_state = "lasso";
        this.start_lasso(evt);
      } else if ($scope.active_tool=="zone") {
        this.deselect();
        this.mouse_state = "transform";
        $scope.start_adding_zone(evt);
        return;
      } else if ($scope.active_tool=="image") {
        this.deselect();
        this.mouse_state = "transform";
        $scope.start_adding_placeholder(evt);
        return;
      } else if ($scope.active_tool=="pan") {
        this.start_pan(evt);
        return;
      }

      if ($scope.selection_metrics.count>0) {
        this._no_artifact_toolbar_this_round = true;
      }

      this.deselect();
    },

    start_pan: function(evt) {
      var $scope = this.vm.$root;

      el = $("#space")[0];
      if (el) {
        this.mouse_state = "pan";
        this.old_panx = el.scrollLeft;
        this.old_pany = el.scrollTop;
      }
      
      var cursor = this.cursor_point_to_space(evt);
      $scope.mouse_ox = cursor.x;
      $scope.mouse_oy = cursor.y;
      $scope.mouse_moved = false;
    },

    deselect: function() {
      var $scope = this.vm.$root;
      
      $scope.deselect();
    },

    select: function(evt, a) {
      var $scope = this.vm.$root;

      $scope.select(evt, a);
    },

    multi_select: function(arts) {
      var $scope = this.vm.$root;

      $scope.multi_select(arts);
    },

    start_lasso: function(evt) {
      var point = this.cursor_point_to_space(evt);
      this.lasso = {
        x: point.x,
        y: point.y,
        w: 0,
        h: 0
      }
    },

    rects_intersecting: function(r1,r2) {
      if (!r1 || !r2) return false;
      
      if ( (r1.x+r1.w < r2.x)
        || (r1.x > r2.x+r2.w)
        || (r1.y+r1.h < r2.y)
        || (r1.y > r2.y+r2.h) ) return false;
      return true;
    },

    artifacts_in_rect: function(rect) {
      if (!rect) return [];
      
      var $scope = this.vm.$root;

      return _.filter($scope.active_space_artifacts, function(a) {
354
        return this.rects_intersecting(a, rect);
mntmn's avatar
mntmn committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
      }.bind(this));
    },

    abs_rect: function(rect) {
      var res = {
        x: rect.x,
        y: rect.y,
        w: Math.abs(rect.w),
        h: Math.abs(rect.h)
      }
      if (rect.w<0) res.x+=rect.w;
      if (rect.h<0) res.y+=rect.h;
      return res;
    },

    lasso_style: function() {
      var $scope = this.vm.$root;

      if (!this.lasso) return "";
      var lasso_scaled = {
        x:this.lasso.x,
        y:this.lasso.y,
377
378
        w:this.lasso.w,
        h:this.lasso.h
mntmn's avatar
mntmn committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
      }
      lasso_scaled = this.abs_rect(lasso_scaled);

      var s = "left:"  +lasso_scaled.x+"px;";
      s +=    "top:"   +lasso_scaled.y+"px;";
      s +=    "width:" +lasso_scaled.w+"px;";
      s +=    "height:"+lasso_scaled.h+"px;";
      s +=    "opacity: 1;";
      return s;
    },

    render_lasso: function() {
      if (!this.lasso) {
        $("#lasso").hide();
        return;
      }

      $("#lasso").attr("style", this.lasso_style());
      $("#lasso").show();
    },

400
    // Translate the mouse cursor location from device window coordinates to virtual space coordinates
mntmn's avatar
mntmn committed
401
402
403
404
405
406
    cursor_point_to_space: function(evt) {
      var $scope = this.vm.$root;

      evt = fixup_touches(evt);

      return {
407
408
        x: $scope.scroll_left + (parseInt(evt.pageX) - $scope.bounds_margin_horiz) / $scope.viewport_zoom,
        y: $scope.scroll_top  + (parseInt(evt.pageY) - $scope.bounds_margin_vert)  / $scope.viewport_zoom
mntmn's avatar
mntmn committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
      };
    },

    rect_to_points: function(rect) {
      return [
        {x:rect.x,y:rect.y},
        {x:rect.x+rect.w,y:rect.y},
        {x:rect.x,y:rect.y+rect.h},
        {x:rect.x+rect.w,y:rect.y+rect.h}
      ];
    },

    old_selection_rect: function() {
      var $scope = this.vm.$root;

      var selected = $scope.selected_artifacts().map(function(a){
        return $scope.find_artifact_before_transaction(a);
      }.bind(this));

      return $scope.enclosing_rect(selected);
    },

    snap_point: function(x,y,snap_middle) {
      var $scope = this.vm.$root;

      var TOL = 8;
      var dists = [];

      if (snap_middle) {
        dists.push([[x-window.innerWidth/2,Math.abs(y-window.innerHeight/2)],[x-window.innerWidth/2,Math.abs(y-window.innerHeight/2)]]);
      }

      if ($scope.grid_active) {
        // snap to grid
        var gw = $scope.grid.spacing/$scope.grid.subdivisions;
        var gh = $scope.grid.spacing/$scope.grid.subdivisions;

        var sx1 = parseInt(x/gw)*gw;
        var sy1 = parseInt(y/gh)*gh;
        var sx2 = (parseInt(x/gw)+1)*gw;
        var sy2 = (parseInt(y/gh)+1)*gh;

        dists = [[[Math.abs(sx1-x),sx1], [Math.abs(sy1-y),sy1]],
                 [[Math.abs(sx2-x),sx2], [Math.abs(sy2-y),sy2]]];

      } else {

        // snap to other artifacts

        dists = $scope.unselected_artifacts().map(function(a){

460
          var r  = this.rect_to_points(a);
mntmn's avatar
mntmn committed
461
462
463

          var xd1 = Math.abs(r[0].x-x);
          var xd2 = Math.abs(r[1].x-x);
464
          var xd3 = Math.abs(r[0].x+a.w/2 - x);
mntmn's avatar
mntmn committed
465
466
467

          var yd1 = Math.abs(r[0].y-y);
          var yd2 = Math.abs(r[2].y-y);
468
          var yd3 = Math.abs(r[0].y+a.h/2 - y);
mntmn's avatar
mntmn committed
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489

          if (!snap_middle) {
            if (xd2<xd1) {
              var xd = xd2;
              var sx = r[1].x;
            } else {
              var xd = xd1;
              var sx = r[0].x;
            }

            if (yd2<yd1) {
              var yd = yd2;
              var sy = r[2].y;
            } else {
              var yd = yd1;
              var sy = r[0].y;
            }
          }

          if (snap_middle) {
            var xd = xd3;
490
            var sx = r[0].x+a.w/2;
mntmn's avatar
mntmn committed
491
492

            var yd = yd3;
493
            var sy = r[0].y+a.h/2;
mntmn's avatar
mntmn committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
          }

          return [[xd,sx],[yd,sy]];
        }.bind(this));
      }

      // snap to space edges

      dists.push([[Math.abs(x),0],[Math.abs(y),0]]);
      //dists.push([[Math.abs(dims.width-x),dims.width],[Math.abs(dims.height-y),dims.height]]);

      var unzipped = _.unzip(dists);
      var xdists = _.sortBy(unzipped[0], function(pair) {return pair[0];});
      var ydists = _.sortBy(unzipped[1], function(pair) {return pair[0];});

      var results = {snapx:xdists[0], snapy:ydists[0]};
      if (!xdists[0] || xdists[0][0]>TOL) {
        results.snapx = [0,x];  // distance, coordinate
      } else {
mntmn's avatar
mntmn committed
513
        // FIXME snap rulers are broken
mntmn's avatar
mntmn committed
514
515
516
517
518
519
520
521
522
523
524
        //$scope.snap_ruler_x = xdists[0][1];
      }
      if (!ydists[0] || ydists[0][0]>TOL) {
        results.snapy = [0,y];
      } else {
        //$scope.snap_ruler_y = ydists[0][1];
      }

      return results;
    },

mntmn's avatar
mntmn committed
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
    start_drawing_note: function(evt) {
      evt.preventDefault();
      evt.stopPropagation();

      var $scope = this.vm.$root;
      var point = this.cursor_point_to_space(evt);
      var z = $scope.highest_z()+1;

      var a = {
        space_id: $scope.active_space._id,
        mime: "text/html",
        description: "<p>Text</p>",
        x: point.x,
        y: point.y,
        z: z,
        w: 64,
        h: 64,
        align: "center",
        valign: "middle",
        stroke_color: "#000000",
        fill_color: "rgb(241, 196, 15)",
        stroke: 0
      };

      $scope.save_artifact(a, function(saved_a) {
        $scope.update_board_artifact_viewmodel(saved_a);
        $scope.active_space_artifacts.push(saved_a);
        $scope.select(evt,a);
        $scope.transform_ox = 0;
        $scope.transform_oy = 0;
        $scope.begin_transaction();
      }.bind(this));
    },

mntmn's avatar
mntmn committed
559
560
561
562
563
    start_drawing_scribble: function(evt) {
      evt.preventDefault();
      evt.stopPropagation();

      var $scope = this.vm.$root;
564
      var point = this.cursor_point_to_space(evt);
mntmn's avatar
mntmn committed
565
566
567
568
569
570
571
572
573
      var z = $scope.highest_z()+1;

      $scope.deselect();

      var a = {
        space_id: $scope.active_space._id,
        mime: "x-spacedeck/vector",
        description: "",
        control_points: [{dx:0,dy:0}],
574
575
576
577
578
        x: point.x,
        y: point.y,
        z: z,
        w: 64,
        h: 64,
579
580
        // Set the color to the last selected color, fallback to black if no color was selected
        stroke_color: "#" + $scope.color_picker_rgb || "#000000",
581
582
        stroke: 2,
        shape: "scribble"
mntmn's avatar
mntmn committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
      };

      $scope.save_artifact(a, function(saved_a) {
        $scope.update_board_artifact_viewmodel(saved_a);
        $scope.active_space_artifacts.push(saved_a);

        this.select(evt,saved_a);
        //$scope.tool_artifact = a;
        $scope.transform_ox = 0;
        $scope.transform_oy = 0;
        $scope.begin_transaction();
      }.bind(this));
    },

    start_drawing_arrow: function(evt) {

      evt.preventDefault();
      evt.stopPropagation();

      var $scope = this.vm.$root;
      var point = this.cursor_point_to_space(evt);
      var z = $scope.highest_z()+1;

      var a = {
        space_id: $scope.active_space._id,
        mime: "x-spacedeck/vector",
        description: "",
        control_points: [{dx:0,dy:0},{dx:0,dy:0},{dx:0,dy:0}],
611
612
613
614
615
        x: point.x,
        y: point.y,
        z: z,
        w: 64,
        h: 64,
616
617
        // Set the color to the last selected color, fallback to black if no color was selected
        stroke_color: "#" + $scope.color_picker_rgb || "#000000",
618
619
        stroke: 2,
        shape: "arrow"
mntmn's avatar
mntmn committed
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
      };

      $scope.save_artifact(a, function(saved_a) {
        $scope.update_board_artifact_viewmodel(saved_a);
        $scope.active_space_artifacts.push(saved_a);
        $scope.select(evt,a);
        $scope.selected_control_point_idx = 1;
        $scope.transform_ox = 0;
        $scope.transform_oy = 0;
        $scope.begin_transaction();
      }.bind(this));
    },

    // FIXME: consolidate with arrow drawing?
    start_drawing_line: function(evt) {
      evt.preventDefault();
      evt.stopPropagation();

      var $scope = this.vm.$root;
      var point = this.cursor_point_to_space(evt);
      var z = $scope.highest_z()+1;

      var a = {
        space_id: $scope.active_space._id,
        mime: "x-spacedeck/vector",
        description: "",
        control_points: [{dx:0,dy:0},{dx:0,dy:0}],
647
648
649
650
651
652
653
654
        x: point.x,
        y: point.y,
        z: z,
        w: 64,
        h: 64,
        stroke_color: "#000000",
        stroke: 2,
        shape: "line"
mntmn's avatar
mntmn committed
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
      };

      $scope.save_artifact(a, function(saved_a) {
        $scope.update_board_artifact_viewmodel(saved_a);
        $scope.active_space_artifacts.push(saved_a);
        $scope.select(evt,a);
        $scope.selected_control_point_idx = 1;
        $scope.transform_ox = 0;
        $scope.transform_oy = 0;
        $scope.begin_transaction();
      }.bind(this));
    },

    snap_point_simple: function(point) {
      var snapped = this.snap_point(point.x, point.y);
      return {
        x: snapped.snapx[1],
        y: snapped.snapy[1]
      }
    },

    handle_mouse_up_space: function(evt) {
      var $scope = this.vm.$root;

      evt.preventDefault();
      
      if (this.mouse_state == "lasso") {
682
        var lasso_rect = this.abs_rect(this.lasso);
mntmn's avatar
mntmn committed
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704

        if (lasso_rect.w>0 && lasso_rect.h>0) {
          var arts = this.artifacts_in_rect(lasso_rect);
          this.multi_select(arts);
        } else {
          
          if (this._no_artifact_toolbar_this_round) {
            this._no_artifact_toolbar_this_round = false;
          } else {
            $scope.start_adding_artifact(evt);
          }
        }
        this.lasso = null;
        this.render_lasso();
      }
      else if (_.include(["transform","move","vector_transform","scribble"],this.mouse_state)) {
        var ars = $scope.selected_artifacts();
        
        for (var i=0; i<ars.length; i++) {

          if (_.include(["text","placeholder"],$scope.artifact_major_type(ars[i]))) {
            // some types of artifact need a minimum size
705
706
            if (ars[i].w<10) {
              ars[i].w = 10;
mntmn's avatar
mntmn committed
707
            }
708
709
            if (ars[i].h<10) {
              ars[i].h = 10;
mntmn's avatar
mntmn committed
710
711
712
713
714
715
716
717
718
719
720
            }
          }

          //save_artifact(ars[i], null, $scope.display_saving_error);
        }
      }

      if (this.mouse_state == "text_editor") {
        return;
      }

721
      if (_.include(["zoom", "scribble"], $scope.active_tool)) {
mntmn's avatar
mntmn committed
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
        // tools that stay active after use
        this.mouse_state = "idle";
        $scope.mouse_state = this.mouse_state;
        $scope.end_transaction();
        $scope.deselect();
        return;
      }

      this.mouse_state = "idle";
      $scope.mouse_state = this.mouse_state;
      this.lasso = null;
      $scope.active_tool = "pointer";
      $scope.end_transaction();
      $scope.show_toolbar_props();

    },

    handle_mouse_leave: function(evt) {
      var $scope = this.vm.$root;

      this.mouse_state = "idle";
      this.lasso = null;
      $scope.active_tool = "pointer";
      $scope.end_transaction();

      this.render_lasso();
    },

    handle_mouse_move: function(evt) {
      var $scope = this.vm.$root;
      if (!$scope.active_space) return;

      if (!$scope.editing_artifact_id) {
        evt.preventDefault();
        evt.stopPropagation();
      }

      $scope.handle_scroll();

761
      var cursor = this.cursor_point_to_space(evt); // takes the raw event data and finds the mouse location in virtual space
mntmn's avatar
mntmn committed
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
      var dx = cursor.x - $scope.mouse_ox;
      var dy = cursor.y - $scope.mouse_oy;
      var dt = (new Date()).getTime() - this.last_mouse_move_time;
      this.last_mouse_move_time = (new Date()).getTime();

      // send cursor
      if (dx>10 || dy>10 || dt>100) {
        var name = "anonymous";
        if ($scope.logged_in) {
          name = $scope.user.nickname || $scope.user.email;
        } else {
          name = $scope.guest_nickname || "anonymous";
        }

        var cursor_msg = {
          action: "cursor",
778
779
          x: cursor.x,
          y: cursor.y,
mntmn's avatar
mntmn committed
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
          name: name,
          id: $scope.user._id||name
        };

        $scope.websocket_send(cursor_msg);
      }
      
      // side effects ftw!
      $scope.snap_ruler_x = -1000;
      $scope.snap_ruler_y = -1000;

      $scope.mouse_moved = true;

      $scope.transform_lock = evt.shiftKey;

      if ($scope.transform_lock) {
        if (this.mouse_state == "transform") {
          // lock aspect is done in transform
        } else {
          // lock axis
          if (Math.abs(dy)>Math.abs(dx)) {
            dx = 0;
          } else {
            dy = 0;
          }
        }
      }

      if (this.mouse_state == "move") {
        $scope.hide_toolbar_props();
        
        var snap_dx = 0;
        var snap_dy = 0;

        var selected = $scope.selected_artifacts();
        var snap_edges = this.old_selection_rect();

        if (selected.length && selected[0]._id==$scope.editing_artifact_id) {
          // bail out of moving editable artifact
          return;
        }

        if (snap_edges) {
          var mx = snap_edges.x1 + (snap_edges.x2-snap_edges.x1)/2;
          var my = snap_edges.y1 + (snap_edges.y2-snap_edges.y1)/2;
          var snapped1 = this.snap_point(snap_edges.x1 + dx, snap_edges.y1 + dy, false);
          var snapped2 = this.snap_point(snap_edges.x2 + dx, snap_edges.y2 + dy, false);
          var snapped3 = this.snap_point(mx + dx, my + dy, true);

          if (snapped3.snapx[0]>0) {
            snap_dx = mx + dx - snapped3.snapx[1];
          } else if (snapped2.snapx[0]>0) {
            snap_dx = snap_edges.x2 + dx - snapped2.snapx[1];
          } else {
            snap_dx = snap_edges.x1 + dx - snapped1.snapx[1];
          }

          if (snapped3.snapy[0]>0) {
            snap_dy = my + dy - snapped3.snapy[1];
          } else if (snapped2.snapy[0]>0) {
            snap_dy = snap_edges.y2 + dy - snapped2.snapy[1];
          } else {
            snap_dy = snap_edges.y1 + dy - snapped1.snapy[1];
          }
        }

        $scope.update_selected_artifacts(function(a) {
          var old_a = $scope.find_artifact_before_transaction(a);

          if (old_a) {
            return {
851
852
              x: old_a.x + dx - snap_dx,
              y: old_a.y + dy - snap_dy
mntmn's avatar
mntmn committed
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
            };
          } else {
            // deleted?
            return {};
          }
        }.bind(this));

      } else if (this.mouse_state == "transform") {
        var selected = $scope.selected_artifacts();
        var edges = this.old_selection_rect();

        if (!edges) {
          this.mouse_state = "idle";
          return;
        }

        $scope.hide_toolbar_props();
        
        var ew = (edges.x2-edges.x1);
        var eh = (edges.y2-edges.y1);

        var origin_x = edges.x1 + ew * $scope.transform_ox;
        var origin_y = edges.y1 + eh * $scope.transform_oy;

        // "leading point"
        var lead_x = edges.x1 + ew * (1-$scope.transform_ox) - origin_x;
        var lead_y = edges.y1 + eh * (1-$scope.transform_oy) - origin_y;

        var lead_snapped = this.snap_point(origin_x + lead_x + dx, origin_y + lead_y + dy);
        var moved_x = (lead_snapped.snapx[1] - origin_x);
        var moved_y = (lead_snapped.snapy[1] - origin_y);

        var scale_x = lead_x ? (moved_x)/lead_x : 1;
        var scale_y = lead_y ? (moved_y)/lead_y : 1;
mntmn's avatar
mntmn committed
887
        if ($scope.transform_lock) scale_y = scale_x;
mntmn's avatar
mntmn committed
888
889
890
891

        $scope.update_selected_artifacts(function(a) {
          var old_a = $scope.find_artifact_before_transaction(a);

892
893
894
895
          var x1 = origin_x + ((old_a.x - origin_x) * scale_x);
          var y1 = origin_y + ((old_a.y - origin_y) * scale_y);
          var x2 = origin_x + (((old_a.x + old_a.w) - origin_x) * scale_x);
          var y2 = origin_y + (((old_a.y + old_a.h) - origin_y) * scale_y);
mntmn's avatar
mntmn committed
896
897
898
899
900

          if (x1>x2) { var t = x1; x1 = x2; x2 = t; }
          if (y1>y2) { var t = y1; y1 = y2; y2 = t; }

          return {
901
902
903
904
            x: x1,
            y: y1,
            w: x2 - x1,
            h: y2 - y1
mntmn's avatar
mntmn committed
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
          };
        }.bind(this));

      } else if (this.mouse_state == "lasso") {
        this.lasso.w = dx;
        this.lasso.h = dy;

        this.render_lasso();

      } else if (this.mouse_state == "vector_transform") {
        $scope.hide_toolbar_props();
        
        var _this = this;
        $scope.update_selected_artifacts(function(a) {
          var old_a = $scope.find_artifact_before_transaction(a);

          var control_points = _.cloneDeep(old_a.control_points);
          var cp = control_points[$scope.selected_control_point_idx];

924
925
926
          var snapped = _this.snap_point(old_a.x+cp.dx+dx, old_a.y+cp.dy+dy);
          dx = snapped.snapx[1]-(old_a.x+cp.dx);
          dy = snapped.snapy[1]-(old_a.y+cp.dy);
mntmn's avatar
mntmn committed
927
928
929
930
931

          cp.dx += dx;
          cp.dy += dy;

          // special case for arrow's 3rd point
932
          if (a.shape == "arrow" && $scope.selected_control_point_idx!=2) {
mntmn's avatar
mntmn committed
933
934
935
936
937
938
939
            /*control_points[2].dx += dx/2;
            control_points[2].dy += dy/2; */

            control_points[2].dx = (control_points[0].dx+control_points[1].dx)/2;
            control_points[2].dy = (control_points[0].dy+control_points[1].dy)/2;
          }

940
          return _this.normalize_control_points(control_points, old_a);
mntmn's avatar
mntmn committed
941
942
943
944
945
946
947
948
        });

      } else if (this.mouse_state == "scribble") {

        $scope.update_selected_artifacts(function(a) {
          var old_a = a;

          var control_points = _.cloneDeep(old_a.control_points);
949
          var offset = {x:cursor.x,y:cursor.y};
mntmn's avatar
mntmn committed
950
951

          control_points.push({
952
953
            dx: offset.x-old_a.x,
            dy: offset.y-old_a.y
mntmn's avatar
mntmn committed
954
955
          });

956
          return this.normalize_control_points(simplify_scribble_points(control_points), old_a);
mntmn's avatar
mntmn committed
957
958
959
960
961
962
963
964
965
966
967
968
        }.bind(this));

        var arts = $scope.selected_artifacts();

        if (arts.length) {
          $scope.update_board_artifact_viewmodel(arts[0]);
        }
      }
      else if (this.mouse_state == "pan") {
        if (!$("#space").length) return;
        el = $("#space")[0];

969
970
        el.scrollLeft -= dx*$scope.viewport_zoom;
        el.scrollTop  -= dy*$scope.viewport_zoom;
mntmn's avatar
mntmn committed
971
972
973
974
975

        $scope.handle_scroll();
      }
    },

976
    normalize_control_points: function(control_points, artifact) {
mntmn's avatar
mntmn committed
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
      var x1 = _.min(control_points,"dx").dx;
      var y1 = _.min(control_points,"dy").dy;
      var x2 = _.max(control_points,"dx").dx;
      var y2 = _.max(control_points,"dy").dy;

      var shiftx = -x1;
      var shifty = -y1;

      var shifted_cps = control_points.map(function(cp) {
        return {
          dx: cp.dx + shiftx,
          dy: cp.dy + shifty
        };
      });

      var w = Math.abs(x2 - x1);
      var h = Math.abs(y2 - y1);

      var bshiftx = 0;
      var bshifty = 0;

998
999
      if (artifact.w < 0) bshiftx = -artifact.w;
      if (artifact.h < 0) bshifty = -artifact.h;
mntmn's avatar
mntmn committed
1000

1001
1002
1003
      return {
        x: artifact.x + bshiftx - shiftx,
        y: artifact.y + bshifty - shifty,
mntmn's avatar
mntmn committed
1004
1005
        w: w,
        h: h,
1006
        z: artifact.z,
mntmn's avatar
mntmn committed
1007
1008
1009
1010
1011
1012
        control_points: shifted_cps
      };
    }

  });
}