(function () { var ns = $.namespace('pskl.controller'); var ACTION = { SELECT : 'select', CLONE : 'clone', DELETE : 'delete', NEW_FRAME : 'newframe' }; ns.FramesListController = function (piskelController, container) { this.piskelController = piskelController; this.container = container; this.previewList = container.querySelector('#preview-list'); this.refreshZoom_(); this.redrawFlag = true; this.regenerateDomFlag = true; this.justDropped = false; this.cachedFrameProcessor = new pskl.model.frame.CachedFrameProcessor(); this.cachedFrameProcessor.setFrameProcessor(this.frameToPreviewCanvas_.bind(this)); this.cachedFrameProcessor.setOutputCloner(this.clonePreviewCanvas_.bind(this)); this.initDragndropBehavior_(); }; ns.FramesListController.prototype.init = function() { $.subscribe(Events.TOOL_RELEASED, this.flagForRedraw_.bind(this)); $.subscribe(Events.PISKEL_RESET, this.flagForRedraw_.bind(this, true)); $.subscribe(Events.USER_SETTINGS_CHANGED, this.flagForRedraw_.bind(this)); $.subscribe(Events.PISKEL_RESET, this.refreshZoom_.bind(this)); this.previewListScroller = document.querySelector('#preview-list-scroller'); this.previewListScroller.addEventListener('scroll', this.updateScrollerOverflows.bind(this)); this.container.addEventListener('click', this.onContainerClick_.bind(this)); this.updateScrollerOverflows(); }; ns.FramesListController.prototype.flagForRedraw_ = function (regenerateDom) { this.redrawFlag = true; if (regenerateDom) { this.regenerateDomFlag = true; } }; ns.FramesListController.prototype.refreshZoom_ = function () { this.zoom = this.calculateZoom_(); }; ns.FramesListController.prototype.render = function () { if (this.redrawFlag) { if (this.regenerateDomFlag) { this.tiles = []; this.addFrameTile = null; this.createPreviews_(); this.regenerateDomFlag = false; } this.updatePreviews_(); this.redrawFlag = false; } }; ns.FramesListController.prototype.updateScrollerOverflows = function () { var scroller = this.previewListScroller; var scrollerHeight = scroller.offsetHeight; var scrollTop = scroller.scrollTop; var scrollerContentHeight = this.previewList.offsetHeight; var treshold = this.container.querySelector('.top-overflow').offsetHeight; var overflowTop = false; var overflowBottom = false; if (scrollerHeight < scrollerContentHeight) { if (scrollTop > treshold) { overflowTop = true; } var scrollBottom = (scrollerContentHeight - scrollTop) - scrollerHeight; if (scrollBottom > treshold) { overflowBottom = true; } } this.container.classList.toggle('top-overflow-visible', overflowTop); this.container.classList.toggle('bottom-overflow-visible', overflowBottom); }; ns.FramesListController.prototype.onContainerClick_ = function (event) { var target = pskl.utils.Dom.getParentWithData(event.target, 'tileAction'); if (!target) { return; } var action = target.dataset.tileAction; var index = parseInt(target.dataset.tileNumber, 10); if (action === ACTION.CLONE) { this.piskelController.duplicateFrameAt(index); var clonedTile = this.createPreviewTile_(index + 1); this.previewList.insertBefore(clonedTile, this.tiles[index].nextSibling); this.tiles.splice(index, 0, clonedTile); this.updateScrollerOverflows(); } else if (action === ACTION.DELETE) { this.piskelController.removeFrameAt(index); this.previewList.removeChild(this.tiles[index]); this.tiles.splice(index, 1); this.updateScrollerOverflows(); } else if (action === ACTION.SELECT && !this.justDropped) { this.piskelController.setCurrentFrameIndex(index); } else if (action === ACTION.NEW_FRAME) { this.piskelController.addFrame(); var newtile = this.createPreviewTile_(this.tiles.length); this.tiles.push(newtile); this.previewList.insertBefore(newtile, this.addFrameTile); this.updateScrollerOverflows(); } this.flagForRedraw_(); }; ns.FramesListController.prototype.updatePreviews_ = function () { var i; var length; for (i = 0, length = this.tiles.length; i < length; i++) { // Remove selected class this.tiles[i].classList.remove('selected'); // Update tile numbers this.tiles[i].setAttribute('data-tile-number', i); this.tiles[i].querySelector('.tile-count').innerHTML = (i + 1); // Check if any tile is updated var hash = this.piskelController.getCurrentLayer().getFrameAt(i).getHash(); if (this.tiles[i].getAttribute('data-tile-hash') !== hash) { if (this.tiles[i].querySelector('canvas')) { this.tiles[i].querySelector('.canvas-container').replaceChild( this.getCanvasForFrame(this.piskelController.getCurrentLayer().getFrameAt(i)), this.tiles[i].querySelector('canvas') ); } else { this.tiles[i].querySelector('.canvas-container').appendChild( this.getCanvasForFrame(this.piskelController.getCurrentLayer().getFrameAt(i)) ); } } } // Hide/Show buttons if needed var buttons = this.container.querySelectorAll('.delete-frame-action, .dnd-action'); var display = (this.piskelController.getFrameCount() > 1) ? 'block' : 'none'; for (i = 0, length = buttons.length; i < length; i++) { buttons[i].style.display = display; } // Add selected class this.tiles[this.piskelController.getCurrentFrameIndex()].classList.add('selected'); }; ns.FramesListController.prototype.createPreviews_ = function () { this.previewList.innerHTML = ''; // Manually remove tooltips since mouseout events were shortcut by the DOM refresh: $('.tooltip').remove(); var frameCount = this.piskelController.getFrameCount(); for (var i = 0 ; i < frameCount ; i++) { var tile = this.createPreviewTile_(i); this.previewList.appendChild(tile); this.tiles[i] = tile; } // Append 'new empty frame' button var newFrameButton = document.createElement('div'); newFrameButton.id = 'add-frame-action'; newFrameButton.className = 'add-frame-action'; newFrameButton.setAttribute('data-tile-action', ACTION.NEW_FRAME); newFrameButton.innerHTML = '