forked from openlink/html5pivotviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pivotviewer.js
4584 lines (4183 loc) · 218 KB
/
pivotviewer.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
//
// HTML5 PivotViewer
//
// Collection loader interface - used so that different types of data sources can be used
//
// Original Code:
// Copyright (C) 2011 LobsterPot Solutions - http:https://www.lobsterpot.com.au/
//
// Enhancements:
// Copyright (C) 2012-2013 OpenLink Software - http:https://www.openlinksw.com/
//
// This software is licensed under the terms of the
// GNU General Public License v2 (see COPYING)
//
///PivotViewer
var PivotViewer = PivotViewer || {};
PivotViewer.Version="v0.9.112-d482060";
PivotViewer.Models = {};
PivotViewer.Models.Loaders = {};
PivotViewer.Utils = {};
PivotViewer.Views = {};
//Debug
var Debug = Debug || {};
/*
http:https://higginsforpresident.net/js/static/jq.pubsub.js
jQuery pub/sub plugin by Peter Higgins ([email protected])
Loosely based on Dojo publish/subscribe API, limited in scope. Rewritten blindly.
Original is (c) Dojo Foundation 2004-2010. Released under either AFL or new BSD, see:
http:https://dojofoundation.org/license for more information.
*/
; (function (d) {
// the topic/subscription hash
var cache = {};
d.publish = function (/* String */topic, /* Array? */args) {
// summary:
// Publish some data on a named topic.
// topic: String
// The channel to publish on
// args: Array?
// The data to publish. Each array item is converted into an ordered
// arguments on the subscribed functions.
//
// example:
// Publish stuff on '/some/topic'. Anything subscribed will be called
// with a function signature like: function(a,b,c){ ... }
//
// | $.publish("/some/topic", ["a","b","c"]);
cache[topic] && d.each(cache[topic], function () {
this.apply(d, args || []);
});
};
d.subscribe = function (/* String */topic, /* Function */callback) {
// summary:
// Register a callback on a named topic.
// topic: String
// The channel to subscribe to
// callback: Function
// The handler event. Anytime something is $.publish'ed on a
// subscribed channel, the callback will be called with the
// published array as ordered arguments.
//
// returns: Array
// A handle which can be used to unsubscribe this particular subscription.
//
// example:
// | $.subscribe("/some/topic", function(a, b, c){ /* handle data */ });
//
if (!cache[topic]) {
cache[topic] = [];
}
cache[topic].push(callback);
return [topic, callback]; // Array
};
d.unsubscribe = function (/* Array */handle) {
// summary:
// Disconnect a subscribed function for a topic.
// handle: Array
// The return value from a $.subscribe call.
// example:
// | var handle = $.subscribe("/something", function(){});
// | $.unsubscribe(handle);
var t = handle[0];
cache[t] && d.each(cache[t], function (idx) {
if (this == handle[1]) {
cache[t].splice(idx, 1);
}
});
};
})(jQuery);//
// HTML5 PivotViewer
//
// Collection loader interface - used so that different types of data sources can be used
//
// Original Code:
// Copyright (C) 2011 LobsterPot Solutions - http:https://www.lobsterpot.com.au/
//
// Enhancements:
// Copyright (C) 2012-2013 OpenLink Software - http:https://www.openlinksw.com/
//
// This software is licensed under the terms of the
// GNU General Public License v2 (see COPYING)
//
Debug.Log = function (message) {
if (window.console && window.console.log && typeof debug != "undefined" && debug == true) {
window.console.log(message);
}
};
//Gets the next 'frame' from the browser (there are several methods) and controls the frame rate
window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
PivotViewer.Utils.EscapeMetaChars = function (jQuerySelector) {
//!"#$%&'()*+,./:;<=>?@[\]^`{|}~
return jQuerySelector
.replace(/\|/gi, "\\|")
.replace(/\//gi, "\\/")
.replace(/'/gi, "\\'")
.replace(/,/gi, "\\,")
.replace(/:/gi, "\\:")
.replace(/\(/gi, "\\(")
.replace(/\)/gi, "\\)")
.replace(/\+/gi, "\\+")
.replace(/\+/gi, "\\-")
.replace(/\+/gi, "\\_")
.replace(/\+/gi, "\\%");
};
PivotViewer.Utils.EscapeItemId = function (itemId) {
return itemId
.replace(/\s+/gi, "|")
.replace(/'/gi, "")
.replace(/\(/gi, "")
.replace(/\)/gi, "")
.replace(/\./gi, "");
};
PivotViewer.Utils.HtmlSpecialChars = function (orig) {
return jQuery('<div />').text(orig).html();
}
PivotViewer.Utils.Now = function () {
if (Date.now)
return Date.now();
else
return (new Date().getTime());
};
// Provided the minimum number is < 1000000
PivotViewer.Utils.Min = function (values) {
var min = 1000000;
for (var i = 0, _iLen = values.length; i < _iLen; i++)
min = min > values[i] ? values[i] : min;
return min;
}
// Provided the maximum number is > -1000000
PivotViewer.Utils.Max = function (values) {
var max = -1000000;
for (var i = 0, _iLen = values.length; i < _iLen; i++)
max = max < values[i] ? values[i] : max;
return max;
}
PivotViewer.Utils.Histogram = function (values) {
if (!values instanceof Array)
return null;
var min = PivotViewer.Utils.Min(values);
var max = PivotViewer.Utils.Max(values);
var bins = (Math.floor(Math.pow(2 * values.length, 1 / 3)) + 1) * 2;
if (bins > 10)
bins = 10;
var stepSize = ((max + 1) - (min - 1)) / bins;
var histogram = [];
for (var i = 0; i < bins; i++) {
var minRange = min + (i * stepSize);
var maxRange = min + ((i + 1) * stepSize);
histogram.push([]);
for (var j = 0, _jLen = values.length; j < _jLen; j++) {
if (minRange <= values[j] && maxRange > values[j])
histogram[i].push(values[j]);
}
}
return { Histogram: histogram, Min: min, Max: max, BinCount: bins };
};
// A simple class creation library.
// From Secrets of the JavaScript Ninja
// Inspired by base2 and Prototype
(function () {
var initializing = false,
// Determine if functions can be serialized
fnTest = /xyz/.test(function () { xyz; }) ? /\b_super\b/ : /.*/;
// Create a new Class that inherits from this class
Object.subClass = function (prop) {
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var proto = new this();
initializing = false;
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
proto[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function (name, fn) {
return function () {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}
// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if (!initializing && this.init)
this.init.apply(this, arguments);
}
// Populate our constructed prototype object
Class.prototype = proto;
// Enforce the constructor to be what we expect
Class.constructor = Class;
// And make this class extendable
Class.subClass = arguments.callee;
return Class;
};
})();
//
// HTML5 PivotViewer
//
// Original Code:
// Copyright (C) 2011 LobsterPot Solutions - http:https://www.lobsterpot.com.au/
//
// Enhancements:
// Copyright (C) 2012-2013 OpenLink Software - http:https://www.openlinksw.com/
//
// This software is licensed under the terms of the
// GNU General Public License v2 (see COPYING)
//
PivotViewer.Models.Collection = Object.subClass({
init: function () {
var xmlns = "http:https://schemas.microsoft.com/collection/metadata/2009",
xmlnsp = "http:https://schemas.microsoft.com/livelabs/pivot/collection/2009";
this.CollectionName = "";
this.BrandImage = "";
this.FacetCategories = [];
this.Items = [];
this.CXMLBase = "";
this.ImageBase = "";
this.CopyrightName = "";
this.CopyrightHref = "";
},
GetItemById: function (Id) {
for (var i = 0; i < this.Items.length; i++) {
if (this.Items[i].Id == Id)
return this.Items[i];
}
return null;
},
GetFacetCategoryByName: function (categoryName) {
for (var i = 0; i < this.FacetCategories.length; i++) {
if (this.FacetCategories[i].Name == categoryName)
return this.FacetCategories[i];
}
return null;
}
});
//PivotViewer.Models
PivotViewer.Models.FacetCategory = Object.subClass({
init: function (Name, Format, Type, IsFilterVisible, IsMetaDataVisible, IsWordWheelVisible, CustomSort) {
this.Name = Name;
this.Format = Format;
this.Type = Type != null && Type != undefined ? Type : PivotViewer.Models.FacetType.String;
this.IsFilterVisible = IsFilterVisible != null && IsFilterVisible != undefined ? IsFilterVisible : true;
this.IsMetaDataVisible = IsMetaDataVisible != null && IsMetaDataVisible != undefined ? IsMetaDataVisible : true;
this.IsWordWheelVisible = IsWordWheelVisible != null && IsWordWheelVisible != undefined ? IsWordWheelVisible : true;
this.CustomSort;
}
});
PivotViewer.Models.FacetCategorySort = Object.subClass({
init: function (Name) {
this.Name = Name;
this.SortValues = [];
}
});
PivotViewer.Models.Item = Object.subClass({
init: function (Img, Id, Href, Name) {
this.Img = parseInt(Img),
this.Id = Id,
this.Href = Href,
this.Name = Name,
this.Description,
this.Facets = [];
}
});
PivotViewer.Models.Facet = Object.subClass({
init: function (Name) {
this.Name = Name;
this.FacetValues = [];
},
AddFacetValue: function (facetValue) {
this.FacetValues.push(facetValue);
}
});
PivotViewer.Models.FacetValue = Object.subClass({
init: function (Value) {
this.Value = Value;
this.Href = "";
}
});
PivotViewer.Models.FacetType = {
String: "String",
LongString: "LongString",
Number: "Number",
DateTime: "DateTime",
Link: "Link"
};
//
// HTML5 PivotViewer
//
// Original Code:
// Copyright (C) 2011 LobsterPot Solutions - http:https://www.lobsterpot.com.au/
//
// Enhancements:
// Copyright (C) 2012-2013 OpenLink Software - http:https://www.openlinksw.com/
//
// This software is licensed under the terms of the
// GNU General Public License v2 (see COPYING)
//
/// Collection loader interface - used so that different types of data sources can be used
PivotViewer.Models.Loaders.ICollectionLoader = Object.subClass({
init: function () { },
LoadCollection: function (collection) {
if (!collection instanceof PivotViewer.Models.Collection) {
throw "collection not an instance of PivotViewer.Models.Collection.";
}
}
});
//CXML loader
PivotViewer.Models.Loaders.CXMLLoader = PivotViewer.Models.Loaders.ICollectionLoader.subClass({
init: function (CXMLUri, proxy) {
this.CXMLUriNoProxy = CXMLUri;
if (proxy)
this.CXMLUri = proxy + CXMLUri;
else
this.CXMLUri = CXMLUri;
},
LoadCollection: function (collection) {
var collection = collection;
this._super(collection);
collection.CXMLBaseNoProxy = this.CXMLUriNoProxy;
collection.CXMLBase = this.CXMLUri;
$.ajax({
type: "GET",
url: this.CXMLUri,
dataType: "xml",
success: function (xml) {
Debug.Log('CXML loaded');
var collectionRoot = $(xml).find("Collection")[0];
//get namespace local name
var namespacePrefix = "P";
if (collectionRoot == undefined) {
//Make sure throbber is removed else everyone thinks the app is still running
$('.pv-loading').remove();
//Display message so the user knows something is wrong
var msg = '';
msg = msg + 'Error parsing CXML Collection<br>';
msg = msg + '<br>Pivot Viewer cannot continue until this problem is resolved<br>';
$('.pv-wrapper').append("<div id=\"pv-parse-error\" class=\"pv-modal-dialog\"><div><a href=\"#pv-modal-dialog-close\" title=\"Close\" class=\"pv-modal-dialog-close\">X</a><h2>HTML5 PivotViewer</h2><p>" + msg + "</p></div></div>");
var t=setTimeout(function(){window.open("#pv-parse-error","_self")},1000)
throw "Error parsing CXML Collection";
}
for (var i = 0; i < collectionRoot.attributes.length; i++) {
if (collectionRoot.attributes[i].value == "http:https://schemas.microsoft.com/livelabs/pivot/collection/2009") {
namespacePrefix = collectionRoot.attributes[i].localName != undefined ? collectionRoot.attributes[i].localName : collectionRoot.attributes[i].baseName;
break;
}
}
collection.CollectionName = $(collectionRoot).attr("Name");
collection.BrandImage = $(collectionRoot).attr(namespacePrefix + ":BrandImage") != undefined ? $(collectionRoot).attr(namespacePrefix + ":BrandImage") : "";
//FacetCategory
var facetCategories = $(xml).find("FacetCategory");
for (var i = 0; i < facetCategories.length; i++) {
var facetElement = $(facetCategories[i]);
var facetCategory = new PivotViewer.Models.FacetCategory(
facetElement.attr("Name"),
facetElement.attr("Format"),
facetElement.attr("Type"),
facetElement.attr(namespacePrefix + ":IsFilterVisible") != undefined ? (facetElement.attr(namespacePrefix + ":IsFilterVisible").toLowerCase() == "true" ? true : false) : true,
facetElement.attr(namespacePrefix + ":IsMetaDataVisible") != undefined ? (facetElement.attr(namespacePrefix + ":IsMetaDataVisible").toLowerCase() == "true" ? true : false) : true,
facetElement.attr(namespacePrefix + ":IsWordWheelVisible") != undefined ? (facetElement.attr(namespacePrefix + ":IsWordWheelVisible").toLowerCase() == "true" ? true : false) : true
);
//Add custom sort order
var sortOrder = facetElement.find(namespacePrefix + "\\:SortOrder");
var sortValues = sortOrder.find(namespacePrefix + "\\:SortValue");
if (sortOrder.length == 0) {
//webkit doesn't seem to like the P namespace
sortOrder = facetElement.find("SortOrder");
sortValues = sortOrder.find("SortValue");
}
if (sortOrder.length == 1) {
var customSort = new PivotViewer.Models.FacetCategorySort(sortOrder.attr("Name"));
for (var j = 0; j < sortValues.length; j++) {
customSort.SortValues.push($(sortValues[j]).attr("Value"));
}
facetCategory.CustomSort = customSort;
}
collection.FacetCategories.push(facetCategory);
}
//Items
var facetItems = $(xml).find("Items");
if (facetItems.length == 1) {
collection.ImageBase = $(facetItems[0]).attr("ImgBase");
var facetItem = $(facetItems[0]).find("Item");
if (facetItem.length == 0) {
//Make sure throbber is removed else everyone thinks the app is still running
$('.pv-loading').remove();
//Display a message so the user knows something is wrong
var msg = '';
msg = msg + 'There are no items in the CXML Collection<br><br>';
$('.pv-wrapper').append("<div id=\"pv-empty-collection-error\" class=\"pv-modal-dialog\"><div><a href=\"#pv-modal-dialog-close\" title=\"Close\" class=\"pv-modal-dialog-close\">X</a><h2>HTML5 PivotViewer</h2><p>" + msg + "</p></div></div>");
var t=setTimeout(function(){window.open("#pv-empty-collection-error","_self")},1000)
} else {
for (var i = 0; i < facetItem.length; i++) {
var item = new PivotViewer.Models.Item(
$(facetItem[i]).attr("Img").replace("#", ""),
$(facetItem[i]).attr("Id"),
$(facetItem[i]).attr("Href"),
$(facetItem[i]).attr("Name")
);
var description = $(facetItem[i]).find("Description");
if (description.length == 1 && description[0].childNodes.length)
item.Description = PivotViewer.Utils.HtmlSpecialChars(description[0].childNodes[0].nodeValue);
var facets = $(facetItem[i]).find("Facet");
for (var j = 0; j < facets.length; j++) {
var f = new PivotViewer.Models.Facet(
$(facets[j]).attr("Name")
);
var facetChildren = $(facets[j]).children();
for (var k = 0; k < facetChildren.length; k++) {
if (facetChildren[k].nodeType == 1) {
var v = $.trim($(facetChildren[k]).attr("Value"));
if (v == null || v == "") {
var fValue = new PivotViewer.Models.FacetValue($(facetChildren[k]).attr("Name"));
fValue.Href = $(facetChildren[k]).attr("Href");
f.AddFacetValue(fValue);
} else {
//convert strings to numbers so histogram can work
if (facetChildren[k].nodeName == "Number") {
var fValue = new PivotViewer.Models.FacetValue(parseFloat(v));
f.AddFacetValue(fValue);
} else {
var fValue = new PivotViewer.Models.FacetValue(PivotViewer.Utils.HtmlSpecialChars(v));
f.AddFacetValue(fValue);
}
}
}
}
item.Facets.push(f);
}
collection.Items.push(item);
}
}
}
//Extensions
var extension = $(xml).find("Extension");
if (extension.length == 1) {
var collectionCopyright = $(extension[0]).find('d1p1\\:Copyright, Copyright');
if (collectionCopyright != undefined) {
collection.CopyrightName = $(collectionCopyright[0]).attr("Name");
collection.CopyrightHref = $(collectionCopyright[0]).attr("Href");
}
}
$.publish("/PivotViewer/Models/Collection/Loaded", null);
},
error: function (jqXHR, textStatus, errorThrown) {
//Make sure throbber is removed else everyone thinks the app is still running
$('.pv-loading').remove();
//Display a message so the user knows something is wrong
var msg = '';
msg = msg + 'Error loading CXML Collection<br><br>';
msg = msg + 'URL : ' + this.url + '<br>';
msg = msg + 'Status : ' + jqXHR.status + ' ' + errorThrown + '<br>';
msg = msg + 'Details : ' + jqXHR.responseText + '<br>';
msg = msg + '<br>Pivot Viewer cannot continue until this problem is resolved<br>';
$('.pv-wrapper').append("<div id=\"pv-loading-error\" class=\"pv-modal-dialog\"><div><a href=\"#pv-modal-dialog-close\" title=\"Close\" class=\"pv-modal-dialog-close\">X</a><h2>HTML5 PivotViewer</h2><p>" + msg + "</p></div></div>");
var t=setTimeout(function(){window.open("#pv-loading-error","_self")},1000)
}
});
}
});
//
// HTML5 PivotViewer
//
// Collection loader interface - used so that different types of data sources can be used
//
// Original Code:
// Copyright (C) 2011 LobsterPot Solutions - http:https://www.lobsterpot.com.au/
//
// Enhancements:
// Copyright (C) 2012-2013 OpenLink Software - http:https://www.openlinksw.com/
//
// This software is licensed under the terms of the
// GNU General Public License v2 (see COPYING)
//
///Views interface - all views must implement this
PivotViewer.Views.IPivotViewerView = Object.subClass({
init: function () {
this.isActive = false;
this.init = true;
this.selected = "";
this.tiles = [];
},
Setup: function (width, height, offsetX, offsetY, tileMaxRatio) { },
Filter: function (dzTiles, currentFilter, sortFacet) { },
GetUI: function () { return ''; },
GetButtonImage: function () { return ''; },
GetButtonImageSelected: function () { return ''; },
GetViewName: function () { return ''; },
Activate: function () { this.isActive = true; },
Deactivate: function () { this.isActive = false; }
});
//
// HTML5 PivotViewer
//
// Collection loader interface - used so that different types of data sources can be used
//
// Original Code:
// Copyright (C) 2011 LobsterPot Solutions - http:https://www.lobsterpot.com.au/
//
// Enhancements:
// Copyright (C) 2012-2013 OpenLink Software - http:https://www.openlinksw.com/
//
// This software is licensed under the terms of the
// GNU General Public License v2 (see COPYING)
//
PivotViewer.Views.TileBasedView = PivotViewer.Views.IPivotViewerView.subClass({
OffsetTiles: function (offsetX, offsetY) {
for (var i = 0; i < this.tiles.length; i++) {
var filterindex = $.inArray(this.tiles[i].facetItem.Id, this.currentFilter);
//set outer location for all tiles not in the filter
if (filterindex >= 0) {
this.tiles[i]._locations[0].destinationx += offsetX;
this.tiles[i]._locations[0].destinationy += offsetY;
}
}
},
SetInitialTiles: function (dzTiles, canvasWidth, canvasHeight) {
var initx = canvasWidth / 2;
var inity = canvasHeight / 2;
for (var i = 0; i < dzTiles.length; i++) {
dzTiles[i]._locations[0].x = initx;
dzTiles[i]._locations[0].y = inity;
dzTiles[i].velocityx = 0;
dzTiles[i].velocityy = 0;
dzTiles[i]._locations[0].startx = initx;
dzTiles[i]._locations[0].starty = inity;
dzTiles[i]._locations[0].destinationx = 0;
dzTiles[i]._locations[0].destinationy = 0;
dzTiles[i].width = 1;
dzTiles[i].height = 1;
}
},
GetRowsAndColumns: function (canvasWidth, canvasHeight, tileMaxRatio, tileCount) {
// look into creating a series of calcs that will try multiple times changing the gap
var gap = 0.7;
var a = tileMaxRatio * (tileCount - Math.pow(gap, 2));
var b = (canvasHeight + (canvasWidth * tileMaxRatio)) * gap;
var c = -1 * (canvasHeight * canvasWidth);
var tileMaxWidth = ((-1 * b) + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
var tileHeight = Math.floor(tileMaxWidth * tileMaxRatio);
var canvasRows = Math.ceil(canvasHeight / tileHeight);
var canvasColumns = Math.floor(canvasWidth / tileMaxWidth);
var paddingX = canvasWidth - (canvasColumns * tileMaxWidth);
return { Rows: canvasRows, Columns: canvasColumns, TileMaxWidth: tileMaxWidth, TileHeight: tileHeight, PaddingX : paddingX };
},
SetOuterTileDestination: function (canvasWidth, canvasHeight, tile) {
//http:https://mathworld.wolfram.com/Circle-LineIntersection.html
//http:https://stackoverflow.com/questions/6091728/line-segment-circle-intersection
//Get adjusted x and y
// as x2 and y2 are the origin
var dx = tile._locations[0].x - (canvasWidth / 2);
var dy = tile._locations[0].y - (canvasHeight / 2);
var M = dy / dx;
var theta = Math.atan2(dy, dx)
tile._locations[0].destinationx = canvasWidth * Math.cos(theta) + (canvasWidth / 2);
tile._locations[0].destinationy = canvasHeight * Math.sin(theta) + (canvasHeight / 2);
},
//http:https://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects
SortBy: function (field, reverse, primer, filterValues) {
var key = function (x, filterValues) {
if (primer) {
for (var i = x.facetItem.Facets.length - 1; i > -1; i -= 1) {
if (x.facetItem.Facets[i].Name == field && x.facetItem.Facets[i].FacetValues.length > 0) {
// If a numeric value could check if value is within filter
// bounds but will have been done already
if ($.isNumeric(x.facetItem.Facets[i].FacetValues[0].Value) )
return primer(x.facetItem.Facets[i].FacetValues[0].Value);
// If a string facet then could have a number of values. Only
// sort on values in the filter
else {
for (var j = 0; j < x.facetItem.Facets[i].FacetValues.length; j++) {
for (var k = 0; k < filterValues.length; k++) {
if (filterValues[k].facet == field)
for (var l = 0; l < filterValues[k].facetValue.length; l++) {
if ( x.facetItem.Facets[i].FacetValues[j].Value == filterValues[k].facetValue[l])
return primer(x.facetItem.Facets[i].FacetValues[j].Value);
}
}
}
}
}
}
}
return null;
};
return function (a, b) {
var A = key(a, filterValues), B = key(b, filterValues);
return (A < B ? -1 : (A > B ? 1 : 0)) * [1, -1][+!!reverse];
}
}
});
//
// HTML5 PivotViewer
//
// Collection loader interface - used so that different types of data sources can be used
//
// Original Code:
// Copyright (C) 2011 LobsterPot Solutions - http:https://www.lobsterpot.com.au/
//
// Enhancements:
// Copyright (C) 2012-2013 OpenLink Software - http:https://www.openlinksw.com/
//
// This software is licensed under the terms of the
// GNU General Public License v2 (see COPYING)
//
///
/// Grid view
///
PivotViewer.Views.GridView = PivotViewer.Views.TileBasedView.subClass({
init: function () {
this.Scale = 1;
this._super();
var that = this;
//Event Handlers
$.subscribe("/PivotViewer/Views/Canvas/Click", function (evt) {
if (!that.isActive)
return;
var selectedItem = null;
var selectedTile = null;
for (var i = 0; i < that.tiles.length; i++) {
var loc = that.tiles[i].Contains(evt.x, evt.y);
if ( loc >= 0 ) {
selectedTile = that.tiles[i];
selectedItem = that.tiles[i].facetItem.Id;
} else {
that.tiles[i].Selected(false);
}
}
that.handleSelection (selectedItem, selectedTile);
});
$.subscribe("/PivotViewer/Views/Canvas/Hover", function (evt) {
if (!that.isActive || that.selected.length > 0)
return;
for (var i = 0; i < that.tiles.length; i++) {
var loc = that.tiles[i].Contains(evt.x, evt.y);
if ( loc >= 0 )
that.tiles[i].Selected(true);
else
that.tiles[i].Selected(false);
}
});
$.subscribe("/PivotViewer/Views/Canvas/Zoom", function (evt) {
if (!that.isActive)
return;
var oldScale = that.Scale;
var preWidth = that.currentWidth;
var preHeight = that.currentHeight;
//Set the zoom time - the time it takes to zoom to the scale
//if on a touch device where evt.scale != undefined then have no delay
var zoomTime = evt.scale != undefined ? 0 : 1000;
if (evt.scale != undefined) {
if (evt.scale >= 1)
that.Scale += (evt.scale - 1);
else {
that.Scale -= evt.scale;
that.Scale = that.Scale < 1 ? 1 : that.Scale;
}
} else if (evt.delta != undefined)
that.Scale = evt.delta == 0 ? 1 : (that.Scale + evt.delta - 1);
if (that.Scale == NaN)
that.Scale = 1;
var newWidth = (that.width - that.offsetX) * that.Scale;
var newHeight = (that.height - that.offsetY) * that.Scale;
//if trying to zoom out too far, reset to min
if (newWidth < that.width || that.Scale == 1) {
that.currentOffsetX = that.offsetX;
that.currentOffsetY = that.offsetY;
that.currentWidth = that.width;
that.currentHeight = that.height;
that.Scale = 1;
} else {
//adjust position to base scale - then scale out to new scale
var scaledPositionX = ((evt.x - that.currentOffsetX) / oldScale) * that.Scale;
var scaledPositionY = ((evt.y - that.currentOffsetY) / oldScale) * that.Scale;
//Move the scaled position to the mouse location
that.currentOffsetX = evt.x - scaledPositionX;
that.currentOffsetY = evt.y - scaledPositionY;
that.currentWidth = newWidth;
that.currentHeight = newHeight;
}
var rowscols = that.GetRowsAndColumns(that.currentWidth - that.offsetX, that.currentHeight - that.offsetY, that.maxRatio, that.currentFilter.length);
that.SetVisibleTilePositions(rowscols, that.currentFilter, that.currentOffsetX, that.currentOffsetY, true, true, zoomTime);
//deselect tiles if zooming back to min size
if (that.Scale == 1 && oldScale != 1) {
for (var i = 0; i < that.tiles.length; i++) {
that.tiles[i].Selected(false);
}
that.selected = "";
$.publish("/PivotViewer/Views/Item/Selected", [{id: that.selected, bkt: 0}]);
}
});
$.subscribe("/PivotViewer/Views/Canvas/Drag", function (evt) {
if (!that.isActive)
return;
var dragX = evt.x;
var dragY = evt.y;
var noChangeX = false, noChangeY = false;
that.currentOffsetX += dragX;
that.currentOffsetY += dragY;
//LHS bounds check
if (dragX > 0 && that.currentOffsetX > that.offsetX) {
that.currentOffsetX -= dragX;
noChangeX = true;
}
//Top bounds check
if (dragY > 0 && that.currentOffsetY > that.offsetY) {
that.currentOffsetY -= dragY;
noChangeY = true;
}
//RHS bounds check
//if the current offset is smaller than the default offset and the zoom scale == 1 then stop drag
if (that.currentOffsetX < that.offsetX && that.currentWidth == that.width) {
that.currentOffsetX -= dragX;
noChangeX = true;
}
if (dragX < 0 && (that.currentOffsetX) < -1 * (that.currentWidth - that.width)) {
that.currentOffsetX -= dragX;
noChangeX = true;
}
//bottom bounds check
if (that.currentOffsetY < that.offsetY && that.currentHeight == that.height) {
that.currentOffsetY -= dragY;
noChangeY = true;
}
if (dragY < 0 && (that.currentOffsetY - that.offsetY) < -1 * (that.currentHeight - that.height)) {
that.currentOffsetY -= dragY;
noChangeY = true;
}
if (noChangeX && noChangeY)
return;
if (noChangeX)
that.OffsetTiles(0, dragY);
else if (noChangeY)
that.OffsetTiles(dragX, 0);
else
that.OffsetTiles(dragX, dragY);
});
},
Setup: function (width, height, offsetX, offsetY, tileMaxRatio) {
this.width = width;
this.height = height;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.maxRatio = tileMaxRatio;
this.currentWidth = this.width;
this.currentHeight = this.height;
this.currentOffsetX = this.offsetX;
this.currentOffsetY = this.offsetY;
},
Filter: function (dzTiles, currentFilter, sortFacet, stringFacets, changingView, changeViewSelectedItem) {
var that = this;
var changingFromTableView = false;
if (!Modernizr.canvas)
return;
Debug.Log('Grid View Filtered: ' + currentFilter.length);
this.changingView = false;
if (changingView) {
if ($('.pv-tableview-table').is(':visible')){
changingFromTableView = true;
$('.pv-tableview-table').fadeOut();
//this.selected = changeViewSelectedItem;
this.selected = "";
$('.pv-viewarea-canvas').fadeIn(function(){
$.publish("/PivotViewer/Views/ChangeTo/Grid", [{Item: changeViewSelectedItem}]);
});
}
}
this.tiles = dzTiles;
if (this.init) {
this.SetInitialTiles(this.tiles, this.width, this.height);
}
// Clear all the multiple images that are used in the grid view
for (var l = 0; l < this.tiles.length; l++) {
while (this.tiles[l]._locations.length > 1)
this.tiles[l]._locations.pop();
}
// Ensure any selected location is zero
for (var i = 0; i < this.tiles.length; i++) {
this.tiles[i].selectedLoc = 0;
}
//Sort
this.tiles = this.tiles.sort(this.SortBy(sortFacet, false, function (a) {
return $.isNumeric(a) ? a : a.toUpperCase();
}, stringFacets));
this.currentFilter = currentFilter;
// Don't calculate positions if changing from table view with item already selected
if (!changingFromTableView || (changeViewSelectedItem == "")) {
var pt1Timeout = 0;
//zoom out first
Debug.Log("this.currentWidth: " + this.currentWidth + " this.width: " + this.width);
var value = $('.pv-toolbarpanel-zoomslider').slider('option', 'value');
if (value > 0) {
this.selected = selectedItem = "";
//zoom out
this.currentOffsetX = this.offsetX;
this.currentOffsetY = this.offsetY;
// Zoom using the slider event
$('.pv-toolbarpanel-zoomslider').slider('option', 'value', 1);
var rowscols = this.GetRowsAndColumns(this.currentWidth - this.offsetX, this.currentHeight - this.offsetY, this.maxRatio, this.tiles.length);
var clearFilter = [];
for (var i = 0; i < this.tiles.length; i++) {
this.tiles[i].origwidth = rowscols.TileHeight / this.tiles[i]._controller.GetRatio(this.tiles[i].facetItem.Img);
this.tiles[i].origheight = rowscols.TileHeight;
clearFilter.push(this.tiles[i].facetItem.Id);
}
this.SetVisibleTilePositions(rowscols, clearFilter, this.currentOffsetX, this.currentOffsetY, true, false, 1000);
pt1Timeout = 1000;
}
setTimeout(function () {
for (var i = 0; i < that.tiles.length; i++) {
//setup tiles
that.tiles[i]._locations[0].startx = that.tiles[i]._locations[0].x;
that.tiles[i]._locations[0].starty = that.tiles[i]._locations[0].y;
that.tiles[i].startwidth = that.tiles[i].width;
that.tiles[i].startheight = that.tiles[i].height;
var filterindex = $.inArray(that.tiles[i].facetItem.Id, currentFilter);
//set outer location for all tiles not in the filter
if (filterindex < 0) {
that.SetOuterTileDestination(that.width, that.height, that.tiles[i]);
that.tiles[i].start = PivotViewer.Utils.Now();
that.tiles[i].end = that.tiles[i].start + 1000;
}
}
// recalculate max width of images in filter
that.maxRatio = that.tiles[0]._controller.GetRatio(that.tiles[0].facetItem.Img);
for (var i = 0; i < that.tiles.length; i++) {
var filterindex = $.inArray(that.tiles[i].facetItem.Id, currentFilter);
if (filterindex >= 0) {
if (that.tiles[i]._controller.GetRatio(that.tiles[i].facetItem.Img) < that.maxRatio)
that.maxRatio = that.tiles[i]._controller.GetRatio(that.tiles[i].facetItem.Img);
}
}
var pt2Timeout = currentFilter.length == that.tiles.length ? 0 : 500;
//Delay pt2 animation
setTimeout(function () {
var rowscols = that.GetRowsAndColumns(that.width - that.offsetX, that.height - that.offsetY, that.maxRatio, that.currentFilter.length);
for (var i = 0; i < that.tiles.length; i++) {
that.tiles[i].origwidth = rowscols.TileHeight / that.tiles[i]._controller.GetRatio(that.tiles[i].facetItem.Img);
that.tiles[i].origheight = rowscols.TileHeight;
}
that.SetVisibleTilePositions(rowscols, that.currentFilter, that.offsetX, that.offsetY, false, false, 1000);
}, pt2Timeout);
}, pt1Timeout);
}
this.init = false;
},
GetUI: function () {
if (Modernizr.canvas)
return "";
else
return "<div class='pv-viewpanel-unabletodisplay'><h2>Unfortunately this view is unavailable as your browser does not support this functionality.</h2>Please try again with one of the following supported browsers: IE 9+, Chrome 4+, Firefox 2+, Safari 3.1+, iOS Safari 3.2+, Opera 9+<br/><a href='http:https://caniuse.com/#feat=canvas'>http:https://caniuse.com/#feat=canvas</a></div>";
},
GetButtonImage: function () {
return 'Content/images/GridView.png';
},
GetButtonImageSelected: function () {
return 'Content/images/GridViewSelected.png';
},
GetViewName: function () {