|
| 1 | +define(['./web-notes.xml', './web-note.xml', './web-draw.xml'], function(notesTemplate, noteTemplate, drawTemplate) { |
| 2 | + |
| 3 | + var notesVue = new Vue({ |
| 4 | + template: notesTemplate, |
| 5 | + data: { |
| 6 | + notes: [] |
| 7 | + }, |
| 8 | + methods: { |
| 9 | + onShow: function() { |
| 10 | + var self = this; |
| 11 | + return fetch('/notes/', { |
| 12 | + headers: { |
| 13 | + "Accept": 'application/json' |
| 14 | + } |
| 15 | + }).then(rejectIfNotOk).then(rejectIfNotOk).then(function(response) { |
| 16 | + return response.json(); |
| 17 | + }).then(function(response) { |
| 18 | + if (!isEmpty(response)) { |
| 19 | + self.notes = response.filter(function(note) { |
| 20 | + return endsWith(note.name, '.txt'); |
| 21 | + }); |
| 22 | + } |
| 23 | + }); |
| 24 | + } |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + var noteVue = new Vue({ |
| 29 | + template: noteTemplate, |
| 30 | + data: { |
| 31 | + name: '', |
| 32 | + newName: false, |
| 33 | + text: '' |
| 34 | + }, |
| 35 | + methods: { |
| 36 | + onShow: function(name) { |
| 37 | + this.name = name; |
| 38 | + this.newName = false; |
| 39 | + this.text = ''; |
| 40 | + var self = this; |
| 41 | + return fetch('/notes/' + this.name).then(rejectIfNotOk).then(function(response) { |
| 42 | + return response.text(); |
| 43 | + }).then(function(text) { |
| 44 | + self.text = text; |
| 45 | + }); |
| 46 | + }, |
| 47 | + onRename: function () { |
| 48 | + var self = this; |
| 49 | + this.onDelete().then(function() { |
| 50 | + self.name = self.newName + '.txt'; |
| 51 | + return self.onSave(); |
| 52 | + }).then(function() { |
| 53 | + self.newName = false; |
| 54 | + }); |
| 55 | + }, |
| 56 | + onDelete: function () { |
| 57 | + return fetch('/notes/' + this.name, { |
| 58 | + method: 'DELETE' |
| 59 | + }).then(function() { |
| 60 | + toaster.toast('Note deleted'); |
| 61 | + }); |
| 62 | + }, |
| 63 | + onSave: function () { |
| 64 | + return fetch('/notes/' + this.name, { |
| 65 | + method: 'PUT', |
| 66 | + body: this.text |
| 67 | + }).then(function() { |
| 68 | + toaster.toast('Note saved'); |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + function drawDot(ctx, x, y, size) { |
| 75 | + ctx.fillStyle = "rgba(0,0,0,0.6)"; |
| 76 | + ctx.beginPath(); |
| 77 | + ctx.arc(x, y, size, 0, Math.PI*2, true); |
| 78 | + ctx.closePath(); |
| 79 | + ctx.fill(); |
| 80 | + } |
| 81 | + |
| 82 | + var canvas, context, size = 6; |
| 83 | + var mouseX, mouseY, mouseDown = 0; |
| 84 | + var touchX, touchY; |
| 85 | + |
| 86 | + function onMouseDown() { |
| 87 | + mouseDown = 1; |
| 88 | + drawDot(context , mouseX, mouseY, size); |
| 89 | + } |
| 90 | + function onMouseUp() { |
| 91 | + mouseDown = 0; |
| 92 | + } |
| 93 | + function onMouseMove(event) { |
| 94 | + getMousePos(event); |
| 95 | + if (mouseDown === 1) { |
| 96 | + drawDot(context, mouseX, mouseY, size); |
| 97 | + } |
| 98 | + } |
| 99 | + function getMousePos(event) { |
| 100 | + if (event.offsetX) { |
| 101 | + mouseX = event.offsetX; |
| 102 | + mouseY = event.offsetY; |
| 103 | + } else if (event.layerX) { |
| 104 | + mouseX = event.layerX; |
| 105 | + mouseY = event.layerY; |
| 106 | + } |
| 107 | + } |
| 108 | + function onTouchStart(event) { |
| 109 | + getTouchPos(); |
| 110 | + drawDot(context, touchX, touchY, size); |
| 111 | + event.preventDefault(); |
| 112 | + } |
| 113 | + function onTouchMove(event) { |
| 114 | + getTouchPos(event); |
| 115 | + drawDot(context, touchX, touchY, size); |
| 116 | + event.preventDefault(); |
| 117 | + } |
| 118 | + function getTouchPos(event) { |
| 119 | + if(event.touches) { |
| 120 | + if (event.touches.length === 1) { |
| 121 | + var touch = event.touches[0]; |
| 122 | + touchX = touch.pageX - touch.target.offsetLeft; |
| 123 | + touchY = touch.pageY - touch.target.offsetTop; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + function resizeCanvas() { |
| 128 | + var draw = document.getElementById('draw'); |
| 129 | + canvas.width = window.innerWidth; |
| 130 | + canvas.height = window.innerHeight - draw.children[0].offsetHeight; |
| 131 | + } |
| 132 | + |
| 133 | + var drawVue = new Vue({ |
| 134 | + template: drawTemplate, |
| 135 | + methods: { |
| 136 | + onShow: function() { |
| 137 | + canvas = document.getElementById('draw-canvas'); |
| 138 | + if (!canvas) { |
| 139 | + return; |
| 140 | + } |
| 141 | + context = canvas.getContext && canvas.getContext('2d'); |
| 142 | + if (context) { |
| 143 | + canvas.addEventListener('mousedown', onMouseDown, false); |
| 144 | + canvas.addEventListener('mousemove', onMouseMove, false); |
| 145 | + window.addEventListener('mouseup', onMouseUp, false); |
| 146 | + canvas.addEventListener('touchstart', onTouchStart, false); |
| 147 | + canvas.addEventListener('touchmove', onTouchMove, false); |
| 148 | + window.addEventListener('resize', resizeCanvas, false); |
| 149 | + resizeCanvas(); |
| 150 | + } |
| 151 | + }, |
| 152 | + onHide: function() { |
| 153 | + canvas.removeEventListener('mousedown', onMouseDown); |
| 154 | + canvas.removeEventListener('mousemove', onMouseMove); |
| 155 | + window.removeEventListener('mouseup', onMouseUp); |
| 156 | + canvas.removeEventListener('touchstart', onTouchStart); |
| 157 | + canvas.removeEventListener('touchmove', onTouchMove); |
| 158 | + window.removeEventListener('resize', resizeCanvas); |
| 159 | + }, |
| 160 | + clear: function() { |
| 161 | + context.clearRect(0, 0, canvas.width, canvas.height); |
| 162 | + } |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + addPageComponent(notesVue, 'fa-sticky-note'); |
| 167 | + addPageComponent(noteVue); |
| 168 | + addPageComponent(drawVue); |
| 169 | + |
| 170 | +}); |
0 commit comments