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 function ScanGalleryPerspective(options){
48 var old_callback = options.callback || function() { };
50 this.vsplitbar = 'GALERIA';
52 options.callback = function(){
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) {
111 console.log(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(){
153 console.log("Image loaded.")
155 }).bind('mousedown', function() {
156 self.imageMoveStart.apply(self, arguments);
161 old_callback.call(this);
164 $.wiki.SidebarPerspective.call(this, options);
167 ScanGalleryPerspective.prototype = new $.wiki.SidebarPerspective();
169 ScanGalleryPerspective.prototype._resizeImage = function(){
170 var $img = this.$image;
178 width: $img.width() * this.zoomFactor,
179 height: $img.height() * this.zoomFactor,
180 originWidth: $img.width(),
181 originHeight: $img.height(),
182 galleryWidth: $img.parent().width(),
183 galleryHeight: $img.parent().height()
186 if (!(this.dimensions.width && this.dimensions.height)) {
187 setTimeout(function(){
192 var position = normalizePosition($img.position().left, $img.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
197 width: $img.width() * this.zoomFactor,
198 height: $img.height() * this.zoomFactor
202 ScanGalleryPerspective.prototype.setPage = function(newPage){
203 newPage = normalizeNumber(newPage, this.doc.galleryImages.length);
204 this.$numberInput.val(newPage);
205 this.config().page = newPage;
206 $('.gallery-image img', this.$element).attr('src', this.doc.galleryImages[newPage - 1].url);
209 ScanGalleryPerspective.prototype.alterZoom = function(delta){
210 var zoomFactor = this.zoomFactor + delta;
211 if (zoomFactor < 0.2)
215 this.setZoom(zoomFactor);
218 ScanGalleryPerspective.prototype.setZoom = function(factor){
219 this.zoomFactor = factor;
221 this.dimensions.width = this.dimensions.originWidth * this.zoomFactor;
222 this.dimensions.height = this.dimensions.originHeight * this.zoomFactor;
224 // var position = normalizePosition(this.$image.position().left, this.$image.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
232 ScanGalleryPerspective.prototype.imageMoved = function(event){
233 event.preventDefault();
235 // origin is where the drag started
236 // imageOrigin is where the drag started on the image
238 var newX = event.clientX - this.origin.x + this.imageOrigin.left;
239 var newY = event.clientY - this.origin.y + this.imageOrigin.top;
241 var position = normalizePosition(newX, newY, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
251 ScanGalleryPerspective.prototype.imageMoveStart = function(event){
252 event.preventDefault();
261 this.imageOrigin = self.$image.position();
262 $(document).bind('mousemove.gallery', function(){
263 self.imageMoved.apply(self, arguments);
264 }).bind('mouseup.gallery', function() {
265 self.imageMoveStop.apply(self, arguments);
271 ScanGalleryPerspective.prototype.imageMoveStop = function(event){
272 $(document).unbind('mousemove.gallery').unbind('mouseup.gallery');
278 ScanGalleryPerspective.prototype.refreshGallery = function(success, failure) {
280 this.doc.refreshGallery({
281 success: function(doc, data){
283 console.log("gconfig:", self.config().page );
284 self.setPage( self.config().page );
285 $('#imagesCount').html("/" + doc.galleryImages.length);
287 $('.error_message', self.$element).hide();
288 if(success) success();
290 failure: function(doc, message){
292 $('.error_message', self.$element).show().html(message);
293 if(failure) failure();
298 ScanGalleryPerspective.prototype.onEnter = function(success, failure){
299 $.wiki.SidebarPerspective.prototype.onEnter.call(this);
300 this.refreshGallery(success, failure);
303 ScanGalleryPerspective.prototype.onExit = function(success, failure) {
307 $.wiki.ScanGalleryPerspective = ScanGalleryPerspective;