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 {
 
  49             var old_callback = options.callback || function() { };
 
  51             options.callback = function(){
 
  54                 this.vsplitbar = 'GALERIA';
 
  57                 if (this.config().page == undefined)
 
  58                     this.config().page = CurrentDocument.galleryStart;
 
  59                 this.$element = $("#side-gallery");
 
  60                 this.$numberInput = $('.page-number', this.$element);
 
  66                 this.$image = $('.gallery-image img', this.$element).attr('unselectable', 'on');
 
  69                 this.$numberInput.change(function(event){
 
  70                     event.preventDefault();
 
  71                     self.setPage($(this).val());
 
  74                 $('.start-page', this.$element).click(function(){
 
  75                     self.setPage(CurrentDocument.galleryStart);
 
  78                 $('.previous-page', this.$element).click(function(){
 
  79                     self.setPage(parseInt(self.$numberInput.val(),10) - 1);
 
  82                 $('.next-page', this.$element).click(function(){
 
  83                     self.setPage(parseInt(self.$numberInput.val(),10) + 1);
 
  86                 $('.zoom-in', this.$element).click(function(){
 
  90                 $('.zoom-out', this.$element).click(function(){
 
  91                     self.alterZoom((-0.2));
 
  94                 $('.ctrl-gallery-setstart', this.$element).click(function(e) {
 
  96                     CurrentDocument.setGalleryStart(self.config().page);
 
  98                 $('.ctrl-gallery-edit', this.$element).click(function(e) {
 
 100                     CurrentDocument.openGalleryEdit();
 
 102                 $('.ctrl-gallery-refresh', this.$element).click(function(e) {
 
 104                     self.refreshGallery();
 
 106                 $('#gallery-chooser').on('show.bs.modal', function (event) {
 
 108                     var datalist = modal.find('.modal-body');
 
 110                     self.doc.withGalleryList(function(galleries) {
 
 112                         $.each(galleries, (i, gallery) => {
 
 113                             item = $('<div class="form-check"><label class="form-check-label"><input class="form-check-input" type="radio" name="gallery"></label></div>');
 
 114                             $('input', item).val(gallery);
 
 115                             $('label', item).append(gallery);
 
 116                             if (gallery == self.doc.galleryLink) {
 
 117                                 item.addClass('text-primary')
 
 118                                 $('input', item).prop('checked', true);
 
 120                             item.appendTo(datalist);
 
 122                         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>');
 
 123                         item.appendTo(datalist);
 
 124                         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>');
 
 125                         item.appendTo(datalist);
 
 128                 $('#gallery-chooser .ctrl-ok').on('click', function (event) {
 
 129                     let item = $('#gallery-chooser :checked');
 
 131                     if (item.hasClass('ctrl-none')) {
 
 134                     else if (item.hasClass('ctrl-new')) {
 
 135                         name = $('#gallery-chooser .ctrl-name').val();
 
 140                     self.doc.setGallery(name);
 
 141                     $('#gallery-chooser').modal('hide');
 
 142                     self.refreshGallery(function() {
 
 147                 $(window).resize(function(){
 
 148                     self.dimensions.galleryWidth = self.$image.parent().width();
 
 149                     self.dimensions.galleryHeight = self.$image.parent().height();
 
 152                 this.$image.load(function(){
 
 154                 }).bind('mousedown', function() {
 
 155                     self.imageMoveStart.apply(self, arguments);
 
 158                 old_callback.call(this);
 
 165             var $img = this.$image;
 
 173                 width: $img.width() * this.zoomFactor,
 
 174                 height: $img.height() * this.zoomFactor,
 
 175                 originWidth: $img.width(),
 
 176                 originHeight: $img.height(),
 
 177                 galleryWidth: $img.parent().width(),
 
 178                 galleryHeight: $img.parent().height()
 
 181             if (!(this.dimensions.width && this.dimensions.height)) {
 
 182                 setTimeout(function(){
 
 187             var position = normalizePosition($img.position().left, $img.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 192                 width: $img.width() * this.zoomFactor,
 
 193                 height: $img.height() * this.zoomFactor
 
 198             newPage = normalizeNumber(newPage, this.doc.galleryImages.length);
 
 199             this.$numberInput.val(newPage);
 
 200             this.config().page = newPage;
 
 201             $('.gallery-image img', this.$element).attr('src', this.doc.galleryImages[newPage - 1].url);
 
 205             var zoomFactor = this.zoomFactor + delta;
 
 206             if (zoomFactor < 0.2)
 
 210             this.setZoom(zoomFactor);
 
 214             this.zoomFactor = factor;
 
 215             this.dimensions.width = this.dimensions.originWidth * this.zoomFactor;
 
 216             this.dimensions.height = this.dimensions.originHeight * this.zoomFactor;
 
 218             // var position = normalizePosition(this.$image.position().left, this.$image.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 227             event.preventDefault();
 
 229             // origin is where the drag started
 
 230             // imageOrigin is where the drag started on the image
 
 232             var newX = event.clientX - this.origin.x + this.imageOrigin.left;
 
 233             var newY = event.clientY - this.origin.y + this.imageOrigin.top;
 
 235             var position = normalizePosition(newX, newY, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 245         imageMoveStart(event) {
 
 246             event.preventDefault();
 
 255             this.imageOrigin = self.$image.position();
 
 256             $(document).bind('mousemove.gallery', function(){
 
 257                 self.imageMoved.apply(self, arguments);
 
 258             }).bind('mouseup.gallery', function() {
 
 259                 self.imageMoveStop.apply(self, arguments);
 
 265         imageMoveStop(event) {
 
 266             $(document).unbind('mousemove.gallery').unbind('mouseup.gallery');
 
 272         refreshGallery(success, failure) {
 
 274             this.doc.refreshGallery({
 
 275                 success: function(doc, data){
 
 277                     console.log("gconfig:", self.config().page );
 
 278                     self.setPage( self.config().page );
 
 279                     $('#imagesCount').html("/" + doc.galleryImages.length);
 
 281                     $('.error_message', self.$element).hide();
 
 282                     if(success) success();
 
 284                 failure: function(doc, message){
 
 286                     $('.error_message', self.$element).show().html(message);
 
 287                     if(failure) failure();
 
 292         onEnter(success, failure) {
 
 294             this.refreshGallery(success, failure);
 
 297         onExit(success, failure) {
 
 300     $.wiki.ScanGalleryPerspective = ScanGalleryPerspective;