/* Drawing palette to display a drawing onto the canvas */ define([ 'sugar-web/graphics/palette', 'mustache' ], function(palette, mustache) { var drawingspalette = {}; function onDrawingSelect(event) { /* Calculation of the drawing url */ var url = window.location.href.split('/'); url.pop(); url = url.join('/') + '/' + event.detail.drawings; var ctx = PaintApp.elements.canvas.getContext('2d'); /* Http request to load the drawing */ var request = new XMLHttpRequest(); request.open('GET', url, true); request.onload = function(e) { if (request.status === 200 || request.status === 0) { var imgSRC = request.responseText; //We load the drawing inside an image element var element = document.createElement('img'); element.src = 'data:image/svg+xml;base64,' + btoa(imgSRC); element.onload = function() { //We draw the drawing to the canvas var ctx = PaintApp.elements.canvas.getContext('2d'); var imgWidth = element.width; var imgHeight = element.height; var maxWidth = PaintApp.elements.canvas.getBoundingClientRect().width; var maxHeight = PaintApp.elements.canvas.getBoundingClientRect().height; var ratio = Math.min(maxWidth / imgWidth, maxHeight / imgHeight); var newWidth = ratio * imgWidth; var newHeight = ratio * imgHeight; ctx.clearRect(0, 0, PaintApp.elements.canvas.width, PaintApp.elements.canvas.height); ctx.drawImage(element, 0, 0, newWidth, newHeight); PaintApp.saveCanvas(); /* If the activity is shared we send the element to everyone */ if (PaintApp.data.isShared) { try { PaintApp.collaboration.sendMessage({ action: 'toDataURL', data: { width: PaintApp.elements.canvas.width / window.devicePixelRatio, height: PaintApp.elements.canvas.height / window.devicePixelRatio, src: PaintApp.collaboration.compress(PaintApp.elements.canvas.toDataURL()) } }); } catch (e) {} } }; } }; request.send(null); } function initGui() { var drawingsButton = document.getElementById('drawings-button'); var drawingsPalette = new PaintApp.palettes.drawingsPalette.DrawingsPalette(drawingsButton, undefined); drawingsPalette.addEventListener('drawingsChange', onDrawingSelect); } drawingspalette.initGui = initGui; /* We setup the palette with drawings */ drawingspalette.DrawingsPalette = function(invoker, primaryText) { palette.Palette.call(this, invoker, primaryText); this.drawingsChangeEvent = document.createEvent('CustomEvent'); this.drawingsChangeEvent.initCustomEvent('drawingsChange', true, true, { 'drawings': 'icons/size-1.svg' }); this.template = '
' + '{{#rows}}' + '