-
Notifications
You must be signed in to change notification settings - Fork 3
/
tile.js
3312 lines (2982 loc) · 89.6 KB
/
tile.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
/*
* TILE 1.0
*
* Copyright 2009-2011 MITH (http://mith.umd.edu) and authors:
* dougreside, jdickie, tdbowman, jgsmith, davelester
* Licensed under the MIT license
*
* http://mith.umd.edu/tile/
*/
/*
* tile.js is the base code for all of the main TILE interface objects
*
* Objects (In order of where they are in file):
* ErrorBox
* - Simple object for displaying errors (For debug mode only)
* Floating Div
* - Object for saving and managing metadata (Labels, transcript lines) attached to
* highlights and shapes
* TILE_ENGINE
* - Main API for TILE
* Plugin Controller
* - Handles core JSON data. Linking, deleting, adding data goes through
* plugin controller
* Mode
* - Object for creating collections of plugins that fit under one
* category. Activates all plugins in collection that the same time.
* Save Dialog
* - Object for saving JSON or XML data back to the user's hard drive
* Load Dialog
* - Object for loading in JSON/XML data - uses CoreData plugin
* TileToolBar
* - Object for adding buttons to the top global button area
*/
/**
* GLOBAL VARIABLES
* Keep track of Image scale
* Large, global variable that
* stores data for other plugins
*/
var TILE=[];
TILE.experimental=false;
TILE.activeItems=[];
TILE.url='';
// ENGINE allows access to global API
TILE.engine={};
TILE.formats='';
TILE.preLoad=null;
TILE.scale=1;
(function($){
var tile=this;
/** Private variables used within TILE_ENGINE
* that can be accessed only in the TILE()
* local level
*/
var pluginControl=null; // instance of plugincontroller
var json=null; // Global JSON session
var _tileBar=null;
// Error box
var errorbox=null;
// used to import data into TILE
var importDialog=null;
var curPage=null;
// stores layouts of different modes
var pluginModes=[];
// array of all plugins
var plugins=[];
// Load Screen
var showLoad=function(){
$("#loadlight").show();
$("#loadDialogSplash").show();
$("#loadblack").show();
};
var removeLoad=function(){
$("#loadblack").fadeTo(1200,0.1,function(){
$("#loadblack").hide();
$("#loadlight").hide();
$("#loadDialogSplash").hide();
});
};
var mouseWait=function(){
// change the document mouse style to wait
document.body.style.cursor='wait';
};
var mouseNormal=function(){
// change the mouse body style back
setTimeout(function(){
document.body.style.cursor='default';
},800);
};
// private methods go here
var deepcopy=function(oldObject){
var tempClone = {};
if((oldObject==null)||(oldObject=='undefined')) return tempClone;
if(typeof(oldObject) == 'object'){
for (var prop in oldObject){
// for array use private method getCloneOfArray
if((typeof(oldObject[prop]) == 'object') && ($.isArray(oldObject[prop]))){
tempClone[prop] = cloneArray(oldObject[prop]);
}
// for object make recursive call to getCloneOfObject
else if (typeof(oldObject[prop]) == 'object'){
tempClone[prop] = deepcopy(oldObject[prop]);
}
// normal (non-object type) members
else {
tempClone[prop] = oldObject[prop];
}
}
}
return tempClone;
};
var cloneArray=function(oldArray){
var tempClone = [];
for (var arrIndex = 0; arrIndex <= oldArray.length; arrIndex++){
if (typeof(oldArray[arrIndex]) == 'object'){
tempClone.push(deepcopy(oldArray[arrIndex]));
} else if((oldArray[arrIndex]!=null)&&(oldArray[arrIndex]!='undefined')){
tempClone.push(oldArray[arrIndex]);
}
}
return tempClone;
};
/**
* Called to see if there is a JSON object stored in the PHP session()
* OR: in a GET request
*/
var checkJSON=function(){
/**
* set up load screen html, which
* will show up to protect more events
* from firing when data is loading
*/
var html = '<div id="loadlight" class="white_content"><div id="loadDialogSplash" class="dialog">'+
'<div class="body"></div></div></div><div id="loadblack" class="black_overlay"></div>';
$(html).appendTo($("body"));
// have black overlay eat all mouse events
$("#loadDark").live('mousedown click mouseup mouseout',function(e){
e.stopPropagation();
return;
});
// start load screen
showLoad();
var self=this;
var file=null;
// check to see if something is pre-loaded
if(TILE.preLoad){
if(typeof(TILE.preLoad) == 'object'){
file=TILE.preLoad;
} else {
file=$.ajax({
url:TILE.preLoad,
dataType:'json',
type:'GET',
success:function(result){
json=result;
setUp();
}
});
return;
}
} else {
json=$.ajax({
url:TILE.engine.serverStateUrl,
accepts: "application/json",
dataType:"text",
async:false
}).responseText;
setUp();
return;
}
if(file){
json=file;
setUp();
return;
// TILE.engine.parseJSON(file);
} else if((window.location.href.search(/\?json\=/i))>=0){
/**
* grab the GET parameter only -
* user defines this by putting ?json= followed
* by the URI of their JSON/XML/TXT file
*/
var n=window.location.href.search(/\?json\=/i);
var str=window.location.href.slice((n+6));
// send to the PHP dev library for importing files
$.ajax({
url:('plugins/CoreData/importExternalFiles.php?file='+str),
dataType:'text',
// set up status codes for false returns/500 etc.
statusCode:{
404: function(){
// do nothing
},
415:function(){
alert("File not supported currently by TILE. Loading default data.");
},
500:function(){
alert("Error parsing data. Loading default data.");
// do nothing
}
},
success:function(txt){
json=txt;
setUp();
// TILE.engine.parseJSON(txt);
// remove the welcome dialog in case it's present
if($("#light").length){
$("#light").remove();
$("#dark").remove();
}
}
});
}
};
/**
* called after getBase(); creating main TILE interface objects and
* setting up the HTML
* d : {Object} - contains columns.json data
*/
var setUp=function(){
var self=this;
// set initial formats
TILE.formats=_tileBar.formatstr;
// take away load screen
removeLoad();
if(json){
TILE.engine.parseJSON(json);
}
};
/*
* Error Dialog Box
*
* Displays errors about experimental features. Experimental features and this dialog
* are unlocked by setting TILE.experimental to true
*/
var ErrorBox = function(){
var self=this;
var html= '<div id="errorlightbox" class="white_content">'+
'<div id="errormessagebox" class="dialog">'+
'<div class="header"><h2 class="">Error Report</h2><h2><a id="errorReportClose" class="btnIconLarge close" href="#"></a></h2></div>'+
'<div class="body"><div class="option"><h3>To report this error, copy and paste the text in the red box and send it to [email protected]</h3>'+
'<div id="error_message" class="rederrorbox"><p></p></div>'+
'</div></div>'+
'</div></div>'+
'<div id="errorfadebox" class="black_overlay"></div>';
$("body").append(html);
$("#errorReportClose").click(function(){
$("#errorlightbox").hide();
$("#errorfadebox").hide();
});
};
ErrorBox.constructor=ErrorBox;
ErrorBox.prototype={
displayError:function(text){
// show text in rederrorbox div
$("#error_message > p").text(text);
$("#errorlightbox").show();
$("#errorfadebox").show();
}
};
/*
* Floating Dialog Box
*
* Usage:
* new FloatingDiv();
*/
var FloatingDiv = function(){
var self = this;
this._color = "#FDFF00";
this._labels = [];
this._curLink=null;
// simple array for all of the names of the
// labels to look up duplicates
this.labelNames=[];
self.defaultColor="000000";
};
FloatingDiv.constructor = FloatingDiv;
FloatingDiv.prototype = {};
$.extend(FloatingDiv.prototype, {
// Convert RGB color value to hexidecimal: Returns: Hexidecimal number format '#'+number
// rgb : {String} RGB value in (xxx,xxx,xxx) format
_rgb2hex: function(rgb) {
// from http://stackoverflow.com/questions/638948/background-color-hex-to-javascript-variable-jquery
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return ("0" + parseInt(x,10).toString(16)).slice(-2);
}
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
},
/**
* stores local variables and initializes HTML
* Does not attach HTML
* @params
* myID : {String}
* labels : {Object} - array of label data to store in FloatingDiv
*/
init: function(myID, labels) {
// remove any dups
$('#'+myID).empty().remove();
var self=this;
var htmlString;
htmlString = '<fieldset class="label_formFieldSet">' +
'<ol class="label_formOL">' +
'<li class="label_formLI cloneMe" id="formField1">' +
'<span class="label_formLabel">Label:</span>' +
'<input id="formLabel1" name="Label1" type="text" class="tagComplete" />' +
' <img src="skins/default/floatingDivIcons/add.png" title="Add up to 5 Labels" name="Add up to 5 Labels" id="btnAddLabel">' +
'<img style="margin-left: 1px; visibility: hidden;" src="skins/default/floatingDivIcons/delete.png" title="Delete last label" name="Delete last label" id="btnDeleteLabel">' +
' <span style="" class="addRemove">(Add/Remove Label Fields)</span><br />' +
'</li>' +
'</ol>' +
'<input type="submit" class="submit" value="Apply" id="submitFloatingDiv">' +
'<input name="hlID" type="hidden" id="TILEid" value="" />'+
'<input name="hlHEX" type="hidden" id="TILEcolor" value="" />' +
'</fieldset>' +
'<br />'+
'<fieldset><ol class="label_formOL">'+
'<li class="label_formLI">'+
'<span class="label_formLabel">Data already attached: </span><br/><div id="labelListFloat" class="az"></div>'+
'</ol></fieldset>';
$('<form></form>')
.attr({
'id':myID+'_floatingDiv',
'name':'TILE Label',
'class':'addLabelForm',
'method':'post'
})
.html(htmlString)
.appendTo('body')
.hide();
$("#"+myID+'_floatingDiv').live("dblclick",function(e){
e.stopPropagation();
return false;
});
$("#liformField1 input.tagComplete").autocomplete({
source:self.labelNames
});
// this.addAutoComplete('li#formField1 input.tagComplete', self._labels);
// INSERTING BUTTON BEHAVIORS //
// FLOATING DIV DIALOG //
// on form submit in floating div
$('input#submitFloatingDiv').live('click', function(e) {
e.preventDefault();
self.sendLabels();
return false;
});
// button click for adding more label fields
$('img#btnAddLabel').live('click', function() {
var num, newNum, newElem, eStuff;
// get number of fields
num = $('li.cloneMe').length;
newNum = (num + 1);
// create the new element via clone(), and manipulate it's ID using newNum value
newElem = $('#formField'+num).clone().attr('id', 'formField' + newNum);
// manipulate the name/id values of the input inside the new element
newElem.find('input:first').attr('id', 'formLabel'+newNum).attr('name', 'Label'+newNum);
newElem.find('img').remove();
newElem.find('.addRemove').remove();
// insert the new element after the last "duplicatable" input field
$('li#formField'+num).after(newElem);
// make things invisible and visible
$('#btnDeleteLabel').css('visibility','visible');
// only allow up to 5 labels
if (newNum == 5) { $('#btnAddLabel').css('visibility','hidden'); }
// add Autocomplete to the new Element
$("input#formLabel"+newNum).autocomplete({source:self.labelNames});
// self.addAutoComplete();
});
// button click for deleting label fields
$('img#btnDeleteLabel').live('click', function() {
var num;
num = $('li.cloneMe').length;
$('#formField' + num).empty().remove();
// if only one element remains, disable the "remove" button
if (num-1 == 1) {
$('#btnDeleteLabel').css('visibility','hidden');
$('#btnAddLabel').css('visibility','visible');
}
});
// set up listener for deleting items in attachDataList
$(".button.shape.delete.formLink").live("click",function(e){
var id=$(this).parent().attr('id');
self.deleteLinkHandle(id);
});
},
// Attaches HTML to DOM
// @params myID : {String}
createDialog:function(myID) {
var self=this;
// get id from object
var elem = '#'+myID+'_floatingDiv';
//create dialog from passed element with passed title
$(elem).dialog({
autoOpen: true,
bgiframe: true,
resizable: false,
title: 'Attach Metadata to Object',
position: 'top',
persist: false,
width: 450,
closeOnEscape: true,
close: function(event, ui) {
$(elem).hide();
return null;
}
});
// overhaul the close function for dialog
$("a.ui-dialog-titlebar-close").unbind('click');
$("a.ui-dialog-titlebar-close").live('click',function(e){
$(".ui-dialog").hide();
});
// get list for metadata and adjust size from default CSS
// this._attachDataList=$("#"+myID+"_floatingDiv > fieldset > ol > li > #labelList");
$("#labelListFloat").css({"position":"relative","height":"100px"});
self.addColorSelector(myID,self.defaultColor);
},
/**
* insert new labels/tags and restart
* the metadata list and autocomplete
* @params lbls : {Object array}
*/
insertNewLabels:function(lbls){
var self=this;
// collate with the current _labels
// go through passed data and extract names, too
for(var x in lbls){
if(!lbls[x]||!lbls[x].obj) continue;
if(!lbls[x].name){
lbls[x].name=(lbls[x].obj.name)?lbls[x].obj.name:lbls[x].id;
}
if($.inArray(lbls[x].name,self.labelNames)>=0) continue;
var f=null;
for(var l in self._labels){
if(l==lbls[x].name){
f=true;
break;
}
}
if(f) continue;
// Insert complete TILE object (id, type, jsonName, obj, and now name) into
// the stack
self._labels[lbls[x].id]=lbls[x];
}
// get rid of existing autocompletes
$("li > input.tagComplete.ui-autocomplete-input").autocomplete('destroy');
// create new autocompletes with new tags
$("li > input.tagComplete").autocomplete({
source:self.labelNames
});
},
/**
* Creates jQuery autoComplete object and attaches it
* to passed element
* @params
* elem : {Object} - passed jQuery element
* labels : {Object} - array of data that represents automplete data - needs to be parsed
*/
addAutoComplete: function() {
var self=this;
// go through passed data and extract names
if($("li > input.tagComplete.ui-autocomplete-input").length){
$("li > input.tagComplete.ui-autocomplete-input").autocomplete('destroy');
}
// start over
$("li > input.tagComplete").autocomplete({
source: self.labelNames
});
return false;
},
/**
* Creates colorpicker object and attaches to FloatingDiv
* @params
* myID : {String}
* o : {String} - hexidecimal value (without the #)
*/
addColorSelector: function(myID, o) {
var self = this;
var htmlString;
htmlString = '<span id="floatingColorPicker">Change Object Color: <div id="floatingPenColor"><div style="background-color: #FDFF00;"></div></div></span>';
$('<div></div>')
.attr({
'id':myID+'_colorSelect',
'name':'TILE Color Selector',
'class':'addColor'
})
.html(htmlString)
.appendTo('#'+myID+'_floatingDiv'+' > fieldset:eq(0)');
var currColor="#"+o;
// currColor = this._rgb2hex(currColor);
$('#floatingPenColor').ColorPicker({
color: currColor,
livePreview:true,
onShow: function (colpkr) {
$(colpkr).fadeIn(500);
return false;
},
onHide: function (colpkr) {
$(colpkr).fadeOut(500);
return false;
},
onChange: function (hsb, hex, rgb) {
// $('span.'+o.id).css('background-color', '#'+hex);
$('#floatingPenColor div').css('backgroundColor', '#' + hex);
$("body:first").trigger("colorChanged",[hex,self._curLink]);
}
});
},
// o : {Object} - has tool id, object id, object type
setInputObject:function(o,refs){
var self=this;
if(!o) return;
// set the passed TILE object as the
// linked object for this dialog
self._curLink=o;
// reset the attachDataList
$("#labelListFloat").empty();
var html="";
// store refs in here
var lbls=[];
// if refs passed, add each one to curLink's refs
if(refs){
var sh=null;
for(var r in refs){
if(refs[r].id==o.id) continue;
for(var x in self._labels){
if(self._labels[x].id==refs[r].id){
lbls.push(self._labels[x]);
sh=true;
break;
}
}
if(!sh){
// insert new label
var key=refs[r].id;
// didn't find it in labels - need to add new (invisible label)
self._labels[key]=refs[r];
lbls.push(self._labels[key]);
}
}
}
// attach references
for(var prop in lbls){
var name="";
if(lbls[prop].obj&&lbls[prop].obj.name){
name=lbls[prop].obj.name;
} else if($("#"+lbls[prop].id).length){
name=$("#"+lbls[prop].id).text().substring(0,10)+"...";
} else {
name=lbls[prop].type+':'+lbls[prop].id;
}
html+="<div id=\""+lbls[prop].id+"\" class=\"labelItem\">"+name+"<span id=\"del_"+lbls[prop].id+"\" class=\"button shape delete formLink\">Delete</span></div>";
}
// attach to the float div list
$("#labelListFloat").append(html);
// reset autocomplete
self.addAutoComplete();
// change colorpicker
if(!self._curLink.obj.color) return;
$('#floatingPenColor > div').css('backgroundColor',self._curLink.obj.color);
},
/**
* Finds all labels that the user references.
* Puts parsed data into array and passes it out using
* event call floatDivOutput
*/
sendLabels:function(){
var self=this;
if(!self._curLink) return;
var lbls=[];
// loop through form field vals and assign to _labels obj
$('li.cloneMe').each(function(i) {
var n = i+1;
var l=$('input#formLabel'+n).val();
if($.inArray(l,lbls)<0){
lbls[i] = $('input#formLabel'+n).val();
}
});
var refs=[];
var html="";
for(var x in lbls){
if((!lbls[x])||(lbls[x]=='undefined')) continue;
var el=null;
if($.inArray(lbls[x],self.labelNames)<0){
// User typed in something that is not in the array
// Create new label
var id="l_"+(Math.floor(Math.random()*560));
var name=(lbls[x].display)?lbls[x].display:lbls[x];
el={id:id,type:'labels',jsonName:'labels',name:lbls[x],obj:{id:id,name:lbls[x]}};
self._labels[id]=el;
// add name to stack
self.labelNames.push(lbls[x]);
// ADD NEW LABEL TO THE JSON
TILE.engine.insertData(el);
} else {
// find in stack
for(var prop in self._labels){
if(self._labels[prop].name==lbls[x]){
el=self._labels[prop];
break;
}
}
}
// shouldn't happen, but just in case
if(!el) continue;
// create HTML to be added to the list of labels attached to curLink
html+="<div id=\""+el.id+"\" class=\"labelItem\">"+el.name+"<span id=\"del_"+el.id+"\" class=\"button shape delete formLink\">Delete</span></div>";
// attach to global page list only if there isn't already label there
if($("#labelList > #"+el.id).length==0){
// none attach - attach this element
$("#labelList").append("<div id=\""+el.id+"\" class=\"labelItem\">"+el.name+"</div>");
}
// push onto stack to be sent to ENGINE
refs.push(el);
}
// attach references to the attachList
$("#labelListFloat").append(html);
for(var r in refs){
if(!refs[r]) continue;
setTimeout(function(s,r){
TILE.engine.linkObjects(s,r);
},1,self._curLink,refs[r]);
}
// update autocomplete
// create a new autoComplete that includes new labels
if($("li > input.tagComplete.ui-autocomplete-input").length){
$("li > input.tagComplete.ui-autocomplete-input").autocomplete('destroy');
}
$("input.tagComplete").autocomplete({
source:self.labelNames
});
},
/**
* Take passed id, find the data it references,
* then delete from current linked object
* @params
* id : {String},
*/
deleteLinkHandle:function(id){
var self=this;
// remove the matched metadata item from
// the current inputObject
if(!self._curLink) return;
var lb=null;
for(var i in self._labels){
if(self._labels[i].id==id){
lb = self._labels[i];
break;
}
}
if(lb === null) return;
$("#labelListFloat > #"+lb.id).remove();
TILE.engine.deleteObj(self._curLink,lb);
// also need to do reverse in order for link to be severed
TILE.engine.deleteObj(lb,self._curLink);
}
});
/*
* TILE Engine
*
* Author: Grant Dickie
*
* Sets up the TILE toolbar (upper left-hand corner) with the
* tool selection drop-down, buttons for saving and exporting
* data
*
* Objects:
* PluginController
* TILE_ENGINE ()
* Creates an instance of TILE_ENGINE
*
* @constructor
*
* Usage:
* TILE_ENGINE: {Object} main engine for running the LogBar and Layout of TILE interface
* Returns: TILE_ENGINE instance {Object}
* This instance has access to all of the TILE API functions, properties, and events
*
* Note: currently TILE_ENGINE does not read anything from the Object that is fed as a parameter. This
* may be changed in future versions of TILE to change the style, placement, or behavior of the Engine.
*
* Example:
* <script type="text/javascript">
// var tile=new TILE_ENGINE({});
*
* Using the insertMode method to add a interface mode
* tile.insertMode('Mode1');
*
* Attach a plugin to Mode1
* tile.insertModePlugin('Mode1','Image Tagger');
*
* Start TILE
* tile.activate();
* </script>
*
* OR put the code in a .js file and add to the header
*
* Events:
* newJSON
* newPage
* newActive - passes TILE object
* dataAdded - passes TILE object
* dataUploaded - passes TILE object
* dataLinked - passes array of TILE objects
* dataDeleted - passes TILE object
*/
var TILE_ENGINE=function(args){
// set local ENGINE variable so that PluginController + other local
// methods can access this
TILE.engine=this;
//get HTML from PHP script and attach to passed container
this.loc=(args.attach)?args.attach:$("body");
var self=this;
// Options that can be fed into the constructor to switch off PHP
// and use only Javascript
urls = (args.urls ? args.urls : {});
// URL used to check whether saved data already loaded
self.serverStateUrl = (urls.state ? urls.state : "plugins/Session/isJSON.php");
//
self.serverRemoteStateUrl = (urls.remoteState ? urls.remoteState : "plugins/CoreData/parseRemoteJSON.php");
// Images filtered through PHP to prevent cross-domain issues
self.serverRemoteImgUrl = (urls.remoteImg ? urls.remoteImg : "plugins/Session/RemoteImgRedirect.php");
// plugins array
self.plugins=[];
// array of plugins with key being
// plugin name, value mode its in
self.modeplugins=[];
json=null;
self.manifest=null;
self.curUrl=null;
//create log - goes towards left area
_tileBar=new TileToolBar({loc:"tile_toolbar"});
// set up plugin controller and listeners for plugin controller
pluginControl=new PluginController();
// set up error box
errorbox=new ErrorBox();
};
TILE_ENGINE.prototype={
/**
* activates the engine - called after loading all
* plugins into the array through insertPlugin
* or insertModePlugin
* @params mode {Object}
*/
activate:function(mode){
var self=this;
// optional: pass mode to determine
// which mode of name 'mode' gets
// activated first
// start up load screen again
showLoad();
// go through plugins array and attach the
// src elements
setTimeout(function(self){
var count=0;
var recLoad=function(){
count++;
if(count==self.plugins.length){
// check if there is json data
checkJSON();
// see if user defined a mode
if(mode){
// find matching mode and
// open that mode up
for(var y in pluginModes){
if(pluginModes[y].name == mode){
pluginModes[y].setActive();
break;
}
}
removeLoad();
} else {
pluginModes[0].setActive();
}
} else {
$.getScript(self.plugins[count],recLoad);
}
};
$.getScript(self.plugins[count],recLoad);
},1,self);
},
showErrorReport:function(text){
errorbox.displayError(text);
},
/**
* adds a string of HTML to the drop-downs in
* save and load dialogs
* @params
* str {String}
*/
addImportExportFormats:function(str){
var self=this;
_tileBar.addFormats(str);
},
/**
* Called to see if there is a JSON object stored in the PHP session()
* adds a toolbar button to TileToolBar
* @params
* button {Object}
*/
addDialogButton:function(button){
// send to _tileBar
var jobj=_tileBar.addButton(button);
return jobj;
},
/**
* adds a plugin to the main set
* of plugins in TILE
* @params name {String}
*/
insertPlugin:function(name){
var self=this;
// obj is plugin wrapper
// figure out src path
var src='plugins/'+name+'/tileplugin.js';
self.plugins.push(src);
},
/**
* takes a description for a mode
* and creates a new mode object
* @params name {String}
*/
insertMode:function(name){
var self=this;
// search for name in array of modes
for(var prop in pluginModes){
if(pluginModes[prop].name == name){
return;
}
}
// no plugin mode already set - create new
var mode=new Mode(name);
pluginModes.push(mode);
return mode;
},
/**
* add a plugin to a specific mode -
* waits until the mode is called to run
* start() on plugin
* @params
* mode {String}, plugin {String}
*/
insertModePlugin:function(mode,plugin){
var self=this;
var obj=null;
// find mode in current modes
for(var prop in pluginModes){
if(pluginModes[prop].name == mode){
obj = pluginModes[prop];
break;
}
}
if(!obj){
// create and insert into array
obj= new Mode(mode);
pluginModes.push(obj);
}
// figure out src path
var src='plugins/'+plugin+'/tileplugin.js';
self.plugins.push(src);
// var script='<script src="'+src+'" type="text/javascript"></script>';
// // attach script to header
// $("head").append(script);
// obj.appendPlugin(plugin);
// insert into modeplugins array
self.modeplugins[plugin]=obj.name;
},
/**
* either appends html to Mode object of name or
* creates a new mode and inserts html in that mode
* @params
* html {String}, section {String}, name {String}
*/
insertModeHTML:function(html,section,name){
var self=this;
var mode=null;
// search for name in array of modes
for(var prop in pluginModes){
if(pluginModes[prop].name == name){
mode = pluginModes[prop];
break;
}
}
// if no mode found, create new
if(!mode){
mode = new Mode(name);
pluginModes.push(mode);
}
mode.appendPluginHTML(html,section);
},
/**
* insert toolbar buttons to a specific
* plugin in a specific mode
* @params
* html {String}, section {String}, name {String}
*/
insertModeButtons:function(html,section,name){
var self=this;
var mode=null;