3     function normalizeNumber(pageNumber, pageCount){
 
   4         // Page number should be >= 1, <= pageCount; 0 if pageCount = 0
 
   5         var pageNumber = parseInt(pageNumber, 10);
 
  12                 pageNumber == Infinity ||
 
  13                 pageNumber == -Infinity ||
 
  17         if (pageNumber > pageCount)
 
  23     function bounds(galleryWidth, galleryHeight, imageWidth, imageHeight){
 
  27             minX: galleryWidth - imageWidth,
 
  28             minY: galleryHeight - imageHeight
 
  32     function normalizePosition(x, y, galleryWidth, galleryHeight, imageWidth, imageHeight){
 
  33         var b = bounds(galleryWidth, galleryHeight, imageWidth, imageHeight);
 
  35             x: Math.min(b.maxX, Math.max(b.minX, x)),
 
  36             y: Math.min(b.maxY, Math.max(b.minY, y))
 
  40     function fixImageSize(){
 
  47     class ScanGalleryPerspective extends $.wiki.SidebarPerspective {
 
  48         vsplitbar = 'GALERIA';
 
  54         constructor(options) {
 
  57             if (this.config().page == undefined)
 
  58                 this.config().page = CurrentDocument.galleryStart;
 
  59             this.$element = $("#side-gallery");
 
  60             this.$numberInput = $('.page-number', this.$element);
 
  62             this.$image = $('.gallery-image img', this.$element).attr('unselectable', 'on');
 
  65             this.$numberInput.change(function(event){
 
  66                 event.preventDefault();
 
  67                 self.setPage($(this).val());
 
  70             $('.start-page', this.$element).click(function(){
 
  71                 self.setPage(CurrentDocument.galleryStart);
 
  74             $('.previous-page', this.$element).click(function(){
 
  75                 self.setPage(parseInt(self.$numberInput.val(),10) - 1);
 
  78             $('.next-page', this.$element).click(function(){
 
  79                 self.setPage(parseInt(self.$numberInput.val(),10) + 1);
 
  82             $('.zoom-in', this.$element).click(function(){
 
  86             $('.zoom-out', this.$element).click(function(){
 
  87                 self.alterZoom((-0.2));
 
  90             $('.ctrl-gallery-setstart', this.$element).click(function(e) {
 
  92                 CurrentDocument.setGalleryStart(self.config().page);
 
  94             $('.ctrl-gallery-edit', this.$element).click(function(e) {
 
  96                 CurrentDocument.openGalleryEdit();
 
  98             $('.ctrl-gallery-refresh', this.$element).click(function(e) {
 
 100                 self.refreshGallery();
 
 102             $('#gallery-chooser').on('show.bs.modal', function (event) {
 
 104                 var datalist = modal.find('.modal-body');
 
 106                 self.doc.withGalleryList(function(galleries) {
 
 108                     $.each(galleries, (i, gallery) => {
 
 109                         item = $('<div class="form-check"><label class="form-check-label"><input class="form-check-input" type="radio" name="gallery"></label></div>');
 
 110                         $('input', item).val(gallery);
 
 111                         $('label', item).append(gallery);
 
 112                         if (gallery == self.doc.galleryLink) {
 
 113                             item.addClass('text-primary')
 
 114                             $('input', item).prop('checked', true);
 
 116                         item.appendTo(datalist);
 
 118                     item = $('<div class="form-check"><label class="form-check-label"><input class="ctrl-none form-check-input" type="radio" name="gallery"><em class="text-secondary">brak</em></label></div>');
 
 119                     item.appendTo(datalist);
 
 120                     item = $('<div class="form-check"><label class="form-check-label"><input class="ctrl-new form-check-input" type="radio" name="gallery"><input class="ctrl-name form-control" placeholder="nowa"></label></div>');
 
 121                     item.appendTo(datalist);
 
 124             $('#gallery-chooser .ctrl-ok').on('click', function (event) {
 
 125                 let item = $('#gallery-chooser :checked');
 
 127                 if (item.hasClass('ctrl-none')) {
 
 130                 else if (item.hasClass('ctrl-new')) {
 
 131                     name = $('#gallery-chooser .ctrl-name').val();
 
 136                 self.doc.setGallery(name);
 
 137                 $('#gallery-chooser').modal('hide');
 
 138                 self.refreshGallery(function() {
 
 143             $(window).resize(function(){
 
 144                 self.dimensions.galleryWidth = self.$image.parent().width();
 
 145                 self.dimensions.galleryHeight = self.$image.parent().height();
 
 148             this.$image.load(function(){
 
 150             }).bind('mousedown', function() {
 
 151                 self.imageMoveStart.apply(self, arguments);
 
 156             var $img = this.$image;
 
 164                 width: $img.width() * this.zoomFactor,
 
 165                 height: $img.height() * this.zoomFactor,
 
 166                 originWidth: $img.width(),
 
 167                 originHeight: $img.height(),
 
 168                 galleryWidth: $img.parent().width(),
 
 169                 galleryHeight: $img.parent().height()
 
 172             if (!(this.dimensions.width && this.dimensions.height)) {
 
 173                 setTimeout(function(){
 
 178             var position = normalizePosition($img.position().left, $img.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 183                 width: $img.width() * this.zoomFactor,
 
 184                 height: $img.height() * this.zoomFactor
 
 189             newPage = normalizeNumber(newPage, this.doc.galleryImages.length);
 
 190             this.$numberInput.val(newPage);
 
 191             this.config().page = newPage;
 
 192             $('.gallery-image img', this.$element).attr('src', this.doc.galleryImages[newPage - 1].url);
 
 196             var zoomFactor = this.zoomFactor + delta;
 
 197             if (zoomFactor < 0.2)
 
 201             this.setZoom(zoomFactor);
 
 205             this.zoomFactor = factor;
 
 206             this.dimensions.width = this.dimensions.originWidth * this.zoomFactor;
 
 207             this.dimensions.height = this.dimensions.originHeight * this.zoomFactor;
 
 209             // var position = normalizePosition(this.$image.position().left, this.$image.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 218             event.preventDefault();
 
 220             // origin is where the drag started
 
 221             // imageOrigin is where the drag started on the image
 
 223             var newX = event.clientX - this.origin.x + this.imageOrigin.left;
 
 224             var newY = event.clientY - this.origin.y + this.imageOrigin.top;
 
 226             var position = normalizePosition(newX, newY, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 236         imageMoveStart(event) {
 
 237             event.preventDefault();
 
 246             this.imageOrigin = self.$image.position();
 
 247             $(document).bind('mousemove.gallery', function(){
 
 248                 self.imageMoved.apply(self, arguments);
 
 249             }).bind('mouseup.gallery', function() {
 
 250                 self.imageMoveStop.apply(self, arguments);
 
 256         imageMoveStop(event) {
 
 257             $(document).unbind('mousemove.gallery').unbind('mouseup.gallery');
 
 263         refreshGallery(success, failure) {
 
 265             this.doc.refreshGallery({
 
 266                 success: function(doc, data){
 
 268                     console.log("gconfig:", self.config().page );
 
 269                     self.setPage( self.config().page );
 
 270                     $('#imagesCount').html("/" + doc.galleryImages.length);
 
 272                     $('.error_message', self.$element).hide();
 
 273                     if(success) success();
 
 275                 failure: function(doc, message){
 
 277                     $('.error_message', self.$element).show().html(message);
 
 278                     if(failure) failure();
 
 283         onEnter(success, failure) {
 
 285             this.refreshGallery(success, failure);
 
 288         onExit(success, failure) {
 
 291     $.wiki.ScanGalleryPerspective = ScanGalleryPerspective;