This repository has been archived by the owner on Nov 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwall.js
1346 lines (1094 loc) · 62.6 KB
/
wall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Victoria and Albert Museum Collections Browser jQuery plugin
* http://www.vam.ac.uk/
*
* Copyright 2011, Victoria and Albert Museum
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function ($) {
$.fn.centerHoriz = function() {
return this.each(function() {
var $this = $(this);
$this.css({'left': $this.parent().width()/2 - $this.width()/2});
});
};
$.fn.centerVert = function() {
return this.each(function() {
var $this = $(this);
$this.css({'top': $this.parent().height()/2 - $this.height()/2});
});
};
$.fn.wall = function(options) {
var wall = this;
var defaults = {
// dimensions and styles
'width': 1024, // width of the wall
'height': 768, // height of the wall
'sizes': [
{ 'dim': 130, 'suff': '_jpg_o', 'name': 'Small images' },
{ 'dim': 177, 'suff': '_jpg_ws', 'name': 'Medium images' },
{ 'dim': 265, 'suff': '_jpg_w', 'name': 'Large images' },
{ 'dim': 355, 'suff': '_jpg_ds', 'name': 'X-Large images' }
],
'start_size': 2,
'tile_margin': 8, // margin around each tile
'sidebar_image_size': 265, // size of the image in the sidebar panel
'minimized_sidebar': {
'width': '550px',
'height': '360px'
},
'background_color': '#ffffff', // background colour of the whole wall
'tile_border_color': '#a1a1a1', // colour of tile borders
'img_fadein': 500, // how long each image should take to fade in (ms)
'fullscreen_speed': 250, // how long the wall should take to resize (ms)
'min_category_count': 30, // minimum number of objects a category must have to be displayed in the sidebar panel (because, say 10 objects don't make a good wall)
'padding': 8, // amount of padding to add to elements that require padding
'wall_border': '1px solid #a1a1a1',
'panel_width': 'auto',
'hide_loader_time': 2000, // how long to display the loading dialog after the images are all loaded
'fill_direction': 'random', // what order to fill blank tile - values are 'forwards', 'backwards' or 'random'
'tag_style': 'list', // how to display the tags - values are 'tagcloud', or 'list'
'show_loading': false, // whether to display the loading dialog
'show_loading_numbers': false, // whether to display the loading progress count
'show_more_link': false, // whether to display the link to the item details page in the sidebar
'enable_history': false,
'enable_clipboard': false,
'enable_map': false,
'max_history': 20, // maximum items in history
'hide_controls_on_load': false, // in this mode, the controls only appear in fullscreen mode
'logo': false, // the logo to display in the title panel
'image_counts_in_sidebar': false,
// messaging
'search_box_default': 'New search', // initial text in the search box
'alert_title': 'Oops',
'alert_msg_no_images': 'Sorry, there are not enough images for that search to fill the screen.',
'alert_msg_enter_search': 'Please enter a search term and try again.',
'alert_msg_zoom_max': 'Sorry, cannot zoom in any further.',
'alert_msg_zoom_min': 'Sorry, cannot zoom out any further.',
'title_no_term': 'Showing 1000 selected images.', // what to display in the title bar if there is no search term
'loading_msg': 'Loading...',
'tips': [
'Drag the grid or click the button in the corner to reveal more images.',
'Try dragging the image grid to reveal more images.',
'You can change the size of the images using the zoom buttons in the panel below.',
'You can shuffle the images using the button in the panel below.',
'Try switching to full screen and back using the toggle fullscreen button.',
'Click on an image to reveal the sidebar with more information about the object.',
'You can load images from a category by clicking the category names in the sidebar.',
'You can drag this window.',
'You can search for similar objects by clicking the object name in the sidebar.',
'The search returns a maximum of 1000 objects.',
'You can see the list of searches you\'ve done by clicking the toggle history button in the panel.',
//~ 'Click the map button in the panel to view the current set on a map.'
],
'start_tip': 0, // index of the tip that will be displayed on start
// api stuff
'api_stub': 'http://www.vam.ac.uk/api/json/museumobject/',
'api_search_path': 'search',
'images_url': 'http://media.vam.ac.uk/media/thira/collection_images/',
'collections_record_url': 'http://collections.vam.ac.uk/item/',
'search_term': '', // the search to display.
'category': { // the category to display
'id': null,
'name': '',
'term': ''
},
'max_results': 225, // the max results we can handle
'limit': 45, // how many images to get per api request. Also affects the size of progressbar increments
'search_term': '', // term to search the api for
'category-stub': '', // category to retrieve images from
'sidebar_image_suffix': '_jpg_w',
'large_image_suffix': '_jpg_l',
'map_start_lat': 51.49645,
'map_start_lng': -0.17197,
// html fragments
'blank_tile': '<li class="blank"></li>',
// nuts and bolts
'cache_interval': 500, // how often to cache some images (ms)
'fill_interval': 100, // how often to fill tiles from cache (ms)
// list of taxonomy terms to populate the sidebar
'taxonomy': [
'styles',
'collections',
'subjects',
'names',
'exhibitions',
'galleries',
'techniques',
'materials',
'categories',
'places'
],
// fields to display in sidebar
'tombstone': [
['Artist', 'artist' ],
['Date', 'date_text' ],
['Museum no.', 'museum_number' ],
['Materials & techniques', 'materials_techniques'],
['Location', 'location'],
['Place', 'place'],
['Description', 'brief_description']
],
// fields to include in the footer text of the info window
'tombstone_string': ['artist', 'date_text', 'museum_number'],
'event_click_sidebar_img': function(event) {
event.preventDefault();
var fsdiag = $('#fullsize', wall);
if(!fullsize_dragged) {
fsdiag.css({
'top': 0,
'left': 0
});
}
fsdiag.show();
}
};
var settings = $.extend({}, defaults, options);
var cache = [], cache_full = false, fill_loop_id, cache_loop_id,
ajax_in_progress = false, offset = 0, offset_anchor = 0, allow_shuffle = false,
fullsize_dragged = false, old_parent, map=null, map_center,
map_markers=[], counter = 0, S = {};
var fragments = {
panel: '<div id="panel" class="ui-widget ui-widget-content ui-corner-all">' +
'<ul id="icons">' +
'<li><input class="ui-state-default ui-corner-all" type="text" name="search" value="'+settings.search_box_default+'"></li>' +
'<li class="ui-state-default ui-corner-all"><span class="submitsearch ui-icon ui-icon-search" title="Submit search"></span></li>' +
'<li class="ui-state-default ui-corner-all"><span class="fullscreen ui-icon ui-icon-arrow-4-diag" title="Toggle full screen"></span></li>' +
'<li class="ui-state-default ui-corner-all"><span class="shuffle ui-icon ui-icon-shuffle" title="Shuffle images"></span></li>' +
'<li class="ui-state-default ui-corner-all"><span class="zoomin ui-icon ui-icon-zoomin" title="Larger images"></span></li>' +
'<li class="ui-state-default ui-corner-all"><span class="zoomout ui-icon ui-icon-zoomout" title="Smaller images"></span></li>' +
'<li class="ui-state-default ui-corner-all hide"><span class="togglehist ui-icon ui-icon-clock" title="Toggle history panel"></span></li>' +
'<li class="ui-state-default ui-corner-all hide"><span class="toggleclip ui-icon ui-icon-clipboard" title="Toggle clipboard"></span></li>' +
'<li class="ui-state-default ui-corner-all hide"><span class="togglemap ui-icon ui-icon-image" title="Map this set"></span></li>' +
'</ul>' +
'</div>',
sidebar: '<div id="sidebar" class="ui-dialog ui-widget ui-widget-content ui-corner-all">' +
'<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span class="ui-dialog-title object-title"></span><a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"><span class="ui-icon ui-icon-closethick">close</span></a></div>' +
'<div><div class="sidebar_image side_panel"></div>' +
'<div class="sidebar_info side_panel"></div></div>' +
'<div id="disabled"></div>' +
'</div>',
panelbtns: '<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix clear panelbuttons">' +
'<span class="tombstring"></span>' +
'<div class="ui-dialog-buttonset"><button title="More details" type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">More</span></button></div>' +
'</div>',
dialog: '<div id="dialog" class="ui-dialog ui-widget ui-widget-content ui-corner-all">' +
'<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span class="ui-dialog-title">'+settings.alert_title+'</span><a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"><span class="ui-icon ui-icon-closethick">close</span></a></div>' +
'<p><span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-alert"></span><span id="dialog_text"></span></p>' +
'<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">Ok</span></button></div></div>' +
'</div>',
loading: '<div id="loading" class="ui-dialog ui-widget ui-widget-content ui-corner-all">' +
'<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span class="ui-dialog-title"></span><a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"><span class="ui-icon ui-icon-closethick">close</span></a></div>' +
'<p><span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-info"></span><strong>Tip: </strong><span class="tip"></span></p>' +
'<p class="results_info hide"></p>' +
'<div id="progressbar"></div>' +
'</div>',
title: '<div id="title" class="ui-dialog ui-widget ui-widget-content ui-corner-all">' +
'<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span class="ui-dialog-title"><p class="title_info"></p></span><a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"><span class="ui-icon ui-icon-closethick">close</span></a></div>' +
//~ '<p class="title_info"></p>' +
'</div>',
fs: '<div id="fullsize" class="ui-dialog ui-widget ui-widget-content ui-corner-all">' +
'<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span class="ui-dialog-title"></span><a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"><span class="ui-icon ui-icon-closethick">close</span></a></div>' +
'<img src="" alt="" title="" />' +
'</div>',
hist: '<div id="hist" class="ui-dialog ui-widget ui-widget-content ui-corner-all">' +
'<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span class="ui-dialog-title">Your history</span><a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"><span class="ui-icon ui-icon-closethick">close</span></a></div>' +
'<div id="histlist" class="ui-widget ui-state-highlight ui-corner-all hide"><ul class="list"></ul></div>' +
'</div>',
clipboard: '<div id="clipboard" class="ui-dialog ui-widget ui-widget-content ui-corner-all">' +
'<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span class="ui-dialog-title">Your clipboard</span><a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"><span class="ui-icon ui-icon-closethick">close</span></a></div>' +
'<div id="clipboardlist" class="ui-widget ui-state-highlight ui-corner-all hide"><ul class="list"></ul><div class="clearfix"></div></div>' +
'</div>',
map: '<div id="mapwrapper" class="ui-dialog"><div id="mapcanvas"></div></div>',
fsbtn: '<span id="fs_button" class="ui-state-default ui-corner-all fullscreen"><span class="fullscreen ui-icon ui-icon-arrow-4-diag" title="Toggle full screen"></span></span>'
};
var methods = {
showLoading: function() {
var l = $('#loading', wall);
$("span.ui-dialog-title", l).html(settings.loading_msg);
l.centerHoriz().centerVert().show();
},
showDialog: function(wall, msg) {
var d = $('#dialog', wall);
$('#dialog_text', d).html(msg);
d.centerHoriz().centerVert().show();
},
populateSidebar: function(wall, objnum) {
var url = settings.api_stub + objnum;
$('#fullsize').hide();
var sidebar = $("#sidebar", wall);
var disabled = $("#disabled", wall);
if(!S.fullscreen) {
sidebar.css(settings.minimized_sidebar).centerVert().centerHoriz();
$(".side_panel", sidebar).width('50%');
$(".sidebar_info", sidebar).height(settings.sidebar_image_size);
} else {
sidebar.css({
'width': settings.sidebar_width,
'height': wall.height() - 2*settings.padding,
'top': 0,
'left': wall.width() - (settings.sidebar_width + 2*settings.padding),
'padding': settings.padding
});
$(".side_panel", sidebar).width('100%');
}
disabled.show();
if(!sidebar.is(':visible')) {
sidebar.show();
}
$.ajax({
dataType: 'jsonp',
url: url,
success: function (json) {
var musobj = json[0].fields;
var image_url = settings.images_url + musobj.primary_image_id.substr(0, 6) + "/" + musobj.primary_image_id + settings.sidebar_image_suffix + ".jpg";
var objname = musobj.object;
var more_url = settings.collections_record_url + musobj.object_number;
if(musobj.title) {
objname += ': ' + musobj.title;
}
var sidebar_image = '<img src="' + image_url + '" title="' + objname + '" alt="' + objname + '" width="'+ settings.sidebar_image_size +'" height="'+ settings.sidebar_image_size +'" data-objnum="' + musobj.object_number + '">';
$('span.object-title', sidebar).html('<span class="ui-icon ui-icon-search" style="float: left; margin-right: 2px;"></span><span id="objname" class="searchable" title="Search for \'' + musobj.object +'\'">' + musobj.object + '</span>');
var info = '';
if(settings.show_more_link) {
if(settings.enable_clipboard) {
info += '<div><span class="ui-icon ui-icon-copy" style="float:left;"></span><a data-name="' + objname + '" data-objnum="' + musobj.object_number + '" data-imref="' + musobj.primary_image_id + '" class="save" href="#" title="Save this object to your clipboard">Save</a></div></div>';
}
}
if (S.fullscreen) {
if(typeof(musobj.descriptive_line) != 'undefined' && musobj.descriptive_line !== '' && musobj.descriptive_line != ['Unknown']) {
info += '<div class="ui-widget ui-state-highlight ui-corner-all descriptiveline">' + musobj.descriptive_line + '</div>';
}
//~ info += '<div class="ui-dialog-buttonset"><button title="More details" type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">More</span></button></div>';
info += '<div class="ui-widget ui-state-highlight ui-corner-all tombstone">';
info += '<ul>';
for(var k=0; k<settings.tombstone.length; k++) {
var t = settings.tombstone[k][0];
var c = settings.tombstone[k][1];
if(typeof(musobj[c]) != 'undefined' && musobj[c] !== '' && musobj[c] != ['Unknown']) {
info += '<li><strong>'+t + '</strong>: ' + musobj[c] +'</li>';
}
}
info += '</ul></div>';
}
info += '<div class="ui-widget ui-state-highlight ui-corner-all" id="browse">';
info += '<ul class="' + settings.tag_style + '">';
var lines = 0;
for( k=0; k<settings.taxonomy.length; k++ ) {
var category = musobj[settings.taxonomy[k]];
if(methods.countGroups(category) > 0) {
var taxonomy_title = methods.ucfirst(settings.taxonomy[k]);
lines++;
if(settings.tag_style == 'list') {
info += '<li><strong>' + taxonomy_title + '</strong>';
info += '<ul>';
}
for(var p=0; p < category.length; p++ ) {
// TO DO: algoritmo for tag sizing
var s = Math.floor(Math.random()*6);
var cat = category[p];
var category_name = methods.ucfirst(cat.fields.name);
if( cat.fields.museumobject_image_count > settings.min_category_count && category_name != 'Unknown') {
lines++;
info += '<li class="size-'+parseInt(s, 10)+'"><a href="#" data-name="' + cat.model.split('.')[1] + '" data-pk="' + cat.pk + '" data-term="' + category_name + '" title="Browse images for \'' + category_name + '\'">' + category_name + '</a>';
if(settings.image_counts_in_sidebar) {
info += ' (' + cat.fields.museumobject_image_count + ')';
}
info += '</li>';
}
}
if(settings.tag_style == 'list') info += '</ul>';
info += '</li>';
}
}
if(lines===0) {
info += '<li>Sorry, no categories for this object.</li>';
}
info += '</ul><div class="clearfix"></div></div>';
$(".sidebar_image", sidebar).html(sidebar_image);
$(".sidebar_info", sidebar).html(info);
if(S.fullscreen) {
$(fragments.panelbtns).appendTo($('.sidebar_info', sidebar));
var h = sidebar.height() - $(".sidebar_image").outerHeight() - $(".ui-dialog-titlebar", sidebar).outerHeight();
$(".sidebar_info", sidebar).height(h).scrollTop(0);
} else {
$(fragments.panelbtns).appendTo(sidebar);
$(".sidebar_info", sidebar).height(settings.sidebar_image_size);
var tombstring = objname;
for(var i=0; i < settings.tombstone_string.length; i++) {
tombstring += '; ' + musobj[settings.tombstone_string[i]];
}
$("span.tombstring", sidebar).html(tombstring);
}
$("button", sidebar).data('href', more_url);
var bigimg = new Image();
bigimg.src = image_url.replace(settings.sidebar_image_suffix, settings.large_image_suffix);
$('#fullsize img', wall).attr('src', bigimg.src);
$('#fullsize .ui-dialog-title').html(objname);
disabled.fadeOut();
}
});
},
apiStart: function() {
ajax_in_progress = true;
$('#panel .shuffle').addClass('disabled');
$.ajax({
dataType: 'jsonp',
url: methods.buildUrl(),
success: function (json) {
if(settings.enable_map) {
for(var i=0; i<map_markers.length; i++) {
var p = map_markers[i]
p.setMap(null);
}
}
map_markers.length=0;
if(json.meta.result_count <= settings.min_category_count) {
methods.showDialog(wall, settings.alert_msg_no_images);
} else {
if(settings.show_loading) {
var r;
if(counter == 0) {
r = 0;
} else {
r = Math.floor(Math.random()*settings.tips.length);
}
$('#loading .tip').html(settings.tips[r]);
methods.showLoading();
}
if(typeof(cache_loop_id) != 'undefined') clearInterval(cache_loop_id);
if(typeof(fill_loop_id) != 'undefined') clearInterval(fill_loop_id);
cache = [];
offset = 0;
$("#grid ul li", wall).addClass('blank');
// from the result count set the grid size
S.num_results = (json.meta.result_count > settings.max_results) ? settings.max_results : json.meta.result_count;
S.display_results = parseInt(settings.num_results, 10);
S.grid_width = Math.floor(Math.sqrt(S.num_results));
S.grid_height = S.grid_width;
S.max_offset = S.grid_width * S.grid_height;
// populate title bar and history
var in_hist = false, title_text;
if(S.category.id !== null) {
title_text = 'Showing ' + S.num_results + ' images for <span class="">' + methods.ucfirst(S.category.name) + ': '+ methods.ucfirst(S.category.term) + '</span>';
var cat_token = S.category.id + S.category.name + S.category.term;
cat_token = cat_token.replace(/ /gi, '').toLowerCase();
if($.inArray(cat_token, settings.browse_hist) == -1) {
settings.browse_hist.push(cat_token);
$("#histlist ul").append('<li><a href="#" data-name="' + S.category.name + '" data-pk="' + S.category.id + '" data-term="' + S.category.term + '">' + methods.ucfirst(S.category.name) + ': ' + S.category.term + '</a></li>');
}
$("#histlist").show();
} else if(S.search_term !== '') {
title_text = 'Showing ' + S.num_results + ' images for <span class="">' + S.search_term + '</span>';
if($.inArray(S.search_term, settings.browse_hist) == -1) {
$("#histlist ul").append('<li><a href="#" data-search_term="'+S.search_term+'" title="Search for \'' + S.search_term + '\'">Search: '+S.search_term+'</a></li>');
settings.browse_hist.push(S.search_term);
}
$("#histlist").show();
} else {
title_text = settings.title_no_term;
}
if(settings.browse_hist.length > settings.max_history) {
$("#histlist li:first").remove();
settings.browse_hist.shift();
}
$("#title .title_info", wall).html(title_text);
if(counter > 0) {
$("#title").show();
}
$("#progressbar").progressbar({ value: 0, max: S.max_offset });
$('#loading p.results_info', wall).html('Loading <strong><span class="loaded">0</span>/' + S.display_results + '</strong> images');
// add offset attributes
offset_anchor = 0;
var num_cols = $("#grid>ul:first li", wall).size(), tiles = $('#grid>ul>li', wall), count = 0, row = 0, o = offset_anchor;
for(var k=0; k < tiles.size(); k++) {
$(tiles[k]).data('offset', o);
count++;
if(count==num_cols) {
count = 0;
row++;
o = row * S.grid_width;
} else {
o++;
}
}
ajax_in_progress = false;
cache_loop_id = setInterval(function() { methods.fillCache(settings, cache); }, settings.cache_interval);
fill_loop_id = setInterval(function() { methods.fillTiles(settings, cache); }, settings.fill_interval);
counter++;
}
}
});
return true;
},
resize: function(size) {
methods.prepareSession(size)
$('#grid', wall).html('');
methods.draw(wall);
// add offset attributes
offset_anchor = 0;
var num_cols = $("#grid>ul:first li", wall).size(), tiles = $('#grid>ul>li', wall), count = 0, row = 0, o = offset_anchor;
for(var k=0; k < tiles.size(); k++) {
$(tiles[k]).data('offset', o);
count++;
if(count==num_cols) {
count = 0;
row++;
o = row * S.grid_width;
} else {
o++;
}
}
},
prepareSession: function(size) {
S.tile_w = settings.sizes[size].dim;
S.tile_h = settings.sizes[size].dim;
S.tile_sidebar_image_suffix = settings.sizes[size].suff;
S.cell_w = S.tile_w + settings.tile_margin + 2; // the '2' accounts for borders
S.cell_h = S.tile_h + settings.tile_margin + 2;
S.start_rows = Math.ceil(settings.height / S.cell_h);
S.start_cols = Math.ceil(settings.width / S.cell_w);
},
buildUrl: function(offset, limit) {
if(typeof(offset)=='undefined' || isNaN(offset)) {
offset = 0;
}
if(typeof(limit)=='undefined' || isNaN(limit)) {
limit = 45;
}
var url, display_term;
if(S.category.id !== null) {
url = settings.api_stub;
url += '?' + S.category.name + '=' + S.category.id;
url += '&getgroup=' + S.category.name;
display_term = methods.ucfirst(S.category.term);
} else {
url = settings.api_stub + settings.api_search_path;
url += "?q=" + S.search_term;
display_term = methods.ucfirst(S.search_term);
}
url += '&limit=' + limit;
url += '&offset=' + offset;
url += "&images=1";
return url;
},
drawEmptyRow: function(n) {
var r = '<ul>';
for(var j=0; j < n; j++) { r += settings.blank_tile; }
r += '</ul>';
return r;
},
styleTiles: function(wall) {
$('#grid li', wall).css({
'width': S.tile_w,
'height': S.tile_h,
'margin-right': settings.tile_margin,
'margin-bottom': settings.tile_margin,
'border-color': settings.tile_border_color
});
},
countGroups: function(category) {
var c = 0;
for (var n = 0; n < category.length; n++ ) {
if(category[0].fields.name != 'Unknown' && category[0].fields.museumobject_image_count > settings.min_category_count) {
c++;
}
}
return c;
},
ucfirst: function(str) {
return str.charAt(0).toUpperCase() + str.substr(1);
},
toggleFullScreen: function() {
var sidebar = $("#sidebar", wall);
sidebar.hide();
var loading = $('#loading', wall);
loading.hide();
if (S.fullscreen) {
// shrink
S.fullscreen = false;
$("body").css({'overflow': 'auto'});
wall.prependTo(old_parent)
.animate({
'width': settings.width,
'height': settings.height
}, settings.fullscreen_speed, function() {
$('#panel').centerHoriz().css({'bottom': 0});
methods.draw(wall);
if(map) {
google.maps.event.trigger(map, "resize");
$('#mapwrapper', wall).centerHoriz().centerVert();
}
});
} else {
// expand
S.fullscreen = true;
old_parent = wall.parent();
wall.prependTo($("body"));
$("body").css({'overflow': 'hidden'});
$(window).scrollTop(0);
wall.animate({
'width': $(document).width(),
'height': $(window).height()
}, settings.fullscreen_speed, function() {
$('#panel').centerHoriz().css({'bottom': 0});
methods.draw(wall);
if(map) {
google.maps.event.trigger(map, "resize");
$('#mapwrapper', wall).centerHoriz().centerVert();
};
if(!$('#panel').is(':visible')) {
$('#panel').show().centerHoriz().css({'bottom': 0});
}
if(!$('#title').is(':visible')) {
$('#title').show();
}
});
}
},
draw: function(wall) {
// before we do anything, let's get the current anchor offset
offset_anchor = $("#grid ul:first li:first", wall).data('offset');
// is there any blank space inside the wall?
var grid = $("#grid", wall);
grid.width(grid.width() + wall.position().left + wall.width());
var tiles = {
'N': Math.ceil(grid.position().top / S.cell_h),
'S': Math.ceil((wall.height() - grid.position().top + grid.height()) / S.cell_h),
'E': Math.ceil((grid.position().left + grid.width()) / S.cell_w),
'W': Math.ceil(grid.position().left / S.cell_w)
};
var p;
for(p in tiles) { tiles[p] = tiles[p] < 0 ? 0 : tiles[p]; }
var do_offsets;
// add new rows to top
if(tiles.N) {
for(var i=0;i<tiles.N;i++) {
grid.prepend(methods.drawEmptyRow($("#grid ul:first > li", wall).size()));
}
do_offsets = true;
}
// add new rows to bottom
if(tiles.S) {
for(var j=0;j<tiles.S;j++) {
grid.append(methods.drawEmptyRow($("#grid ul:first > li", wall).size()));
}
do_offsets = true;
}
// add new cols to left
if(tiles.W) {
var tl = '';
for(var k=0;k<tiles.W;k++) {
tl += settings.blank_tile;
}
$("#grid ul", wall).prepend(tl);
do_offsets = true;
}
// add new cols to right
if(tiles.E) {
var tr = '';
for(var l=0;l<tiles.E;l++) {
tr += settings.blank_tile;
}
$("#grid ul", wall).append(tr);
do_offsets = true;
}
// reposition and resize the grid AFTER adding new tiles
grid.css({
'top': tiles.N > 0 ? grid.position().top - tiles.N * S.cell_h : grid.position().top,
'left': tiles.W > 0 ? grid.position().left - tiles.W * S.cell_w : grid.position().left,
'width': $("#grid ul:first > li", wall).length * S.cell_w
});
// make sure all the new tiles are styled up
methods.styleTiles(wall);
// find tiles outside the viewport and remove them
var remove = {
'N': Math.floor(grid.position().top * -1 / S.cell_h),
'S': Math.floor( (grid.height() - wall.height() + grid.position().top) / S.cell_h),
'E': Math.floor((grid.width() - wall.width() + grid.position().left) / S.cell_w),
'W': Math.floor(grid.position().left * -1 / S.cell_w)
};
for(p in remove) { remove[p] = remove[p] < 0 ? 0 : remove[p]; }
var tiles_removed = 0, rows_removed = 0;
while(tiles_removed < remove.W) {
$("#grid ul li:first-child").remove();
tiles_removed ++;
grid.css({'left': grid.position().left + S.cell_w});
}
tiles_removed = 0;
while(tiles_removed < remove.E) {
$("#grid ul li:last-child").remove();
tiles_removed ++;
}
while(rows_removed < remove.N) {
$("#grid ul:first-child").remove();
rows_removed ++;
grid.css({'top': grid.position().top + S.cell_h});
}
rows_removed = 0;
while(rows_removed < remove.S) {
$("#grid ul:last-child").remove();
rows_removed ++;
}
grid.width($("#grid ul:first > li", wall).length * S.cell_w);
if(do_offsets) {
methods.updateOffsets(wall, tiles, remove, settings);
}
},
updateOffsets: function(wall, tiles, remove, settings) {
offset_anchor = parseInt(offset_anchor, 10);
if(tiles.N > 0) {
offset_anchor -= S.grid_width * tiles.N;
if(offset_anchor < 0) {
offset_anchor += S.max_offset;
}
}
var min, max;
if(tiles.W > 0) {
min = Math.floor(offset_anchor/S.grid_width) * S.grid_width;
max = min + S.grid_width -1;
offset_anchor -= tiles.W;
if(offset_anchor < min) { offset_anchor += S.grid_width; }
}
offset_anchor += remove.W;
offset_anchor -= remove.N * S.grid_width;
// TODO: fix this:
if(offset_anchor < 0) offset_anchor = 0;
offset = offset_anchor - settings.limit;
var num_cols = $("#grid>ul:first li", wall).size(), rows = $('#grid>ul');
for(var j=0;j<rows.size();j++) {
var o = offset_anchor + (j * S.grid_width);
if(o>=S.max_offset) {
o -= S.max_offset;
}
tiles = $("li", rows[j]);
min = Math.floor(o/S.grid_width) * S.grid_width;
max = min + S.grid_width -1;
for(var i=0;i<tiles.size();i++) {
$(tiles[i]).data('offset', o);
o++;
if(o > max) {
o = min;
}
}
o = max + 1;
if(o >= S.max_offset-1) {
o -= S.max_offset;
}
}
},
getImageUrl: function(url_base, image_ref) {
var u;
try {
u = url_base + image_ref.substr(0, 6) + "/" + image_ref + S.tile_sidebar_image_suffix + ".jpg";
} catch(err) {
u = "";
}
return u;
},
retrieveFromCache: function(offset) {
var q;
for(q in cache) {
if(cache[q].offset == offset) return cache[q];
}
if(cache_full) {
$.ajax({
dataType: 'jsonp',
url: methods.buildUrl(offset, 45),
success: function (json) {
var record, obj, objname;
for(var i=0;i<json.records.length;i++) {
record = json.records[i];
obj = {};
obj.offset = offset;
obj.imref = record.fields.primary_image_id;
obj.num = record.fields.object_number;
if(record.fields.title) {
objname = record.fields.object + ' ' + record.fields.title;
} else {
objname = record.fields.object;
}
obj.title = objname;
cache.push(obj);
}
}
});
}
return false;
},
fillCache: function(settings, cache) {
if(typeof(offset)=='undefined' || isNaN(offset) || offset < 0) {
offset = 0;
}
if(cache.length < S.max_offset) {
cache_full = false;
if(!ajax_in_progress) {
ajax_in_progress = true;
var url = methods.buildUrl(offset, settings.limit);
$.ajax({
dataType: 'jsonp',
url: url,
success: function (json) {
var record, cache_obj, objname;
for(var i=0;i<json.records.length;i++) {
record = json.records[i];
cache_obj = {};
cache_obj.offset = offset;
cache_obj.imref = record.fields.primary_image_id;
cache_obj.num = record.fields.object_number;
cache_obj.lng = record.fields.longitude;
cache_obj.lat = record.fields.latitude;
if(record.fields.title) {
objname = record.fields.object + ' ' + record.fields.title;
} else {
objname = record.fields.object;
}
cache_obj.title = objname;
cache.push(cache_obj);
offset ++;
if(settings.enable_map && map && cache_obj.lat && cache_obj.lng) {
var object_icon_url = settings.images_url + cache_obj.imref.substr(0, 6) + "/" + cache_obj.imref + "_jpg_s.jpg";
var point = new google.maps.LatLng(cache_obj.lat, cache_obj.lng);
var marker = new google.maps.Marker({
position: point,
title: cache_obj.title,
//~ icon: object_icon_url,
objnum: cache_obj.num
});
google.maps.event.addListener(marker, 'click', function(event) {
methods.populateSidebar(wall, this.objnum);
});
map_markers.push(marker);
if(map && !marker.map) {
marker.setMap(map);
}
}
}
if(offset >= S.max_offset && cache.length < S.max_offset) { offset = 0; }
ajax_in_progress = false;
$("#progressbar").progressbar({ value: cache.length });
$("#loading .loaded").html(cache.length);
}
});
}
} else { // cache is full
$('#panel .shuffle').removeClass('disabled');
allow_shuffle = true;
clearInterval(cache_loop_id);
cache_full = true;
setTimeout(methods.hideLoading, settings.hide_loader_time);
$("#loading .loaded").html(S.display_results);
$('#loading span.ui-dialog-title').html('Done.');
}
},
hideLoading: function() {
$('#loading').fadeOut();
},
fillTile: function(tile, item) {
if(tile && typeof(tile) != 'undefined') {
tile.css({ 'background-image': 'url('+methods.getImageUrl(settings.images_url, item.imref)+')'})
tile.attr('title', item.title + ' [' + item.num + ']')
tile.data('objnum', item.num)
tile.removeClass('blank');
}
},
fillTiles: function(settings, cache) {
var t, tt;
switch(settings.fill_direction) {
case 'forwards':
t = $("ul li.blank:first");
break;
case 'backwards':
t = $("ul li.blank:last");
break;
case 'random':
tt = $("ul li.blank");
t = $(tt[Math.floor(Math.random()*tt.length)]);
break;
default:
t = $("ul li.blank:first");
break;
}
var item = methods.retrieveFromCache(t.data('offset'), cache);
if(item) {
methods.fillTile(t, item);
}
}
};
S.category = settings.category;
S.search_term = settings.search_term;