-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
2643 lines (2287 loc) · 76.4 KB
/
plugin.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
/*!
* Aloha Editor
* Author & Copyright (c) 2010 Gentics Software GmbH
* Licensed unter the terms of http://www.aloha-editor.com/license.html
*/
/********************************
+---------------------------+
| GENTICS.Aloha.TablePlugin |
+---------------------------+
********************************/
/**
* Register the TablePlugin as GENTICS.Aloha.Plugin
*/
GENTICS.Aloha.TablePlugin = new GENTICS.Aloha.Plugin('com.gentics.aloha.plugins.Table');
/* -- ATTRIBUTES -- */
/**
* The Create-Layer Object of the TablePlugin
*
* @see GENTICS.Aloha.Table.CreateLayer
*/
GENTICS.Aloha.TablePlugin.createLayer = undefined;
/**
* Configure the available languages
*/
GENTICS.Aloha.TablePlugin.languages = ['en', 'de', 'fr', 'eo', 'fi', 'ru', 'it', 'pl'];
/**
* default button configuration
*/
GENTICS.Aloha.TablePlugin.config = [ 'table' ];
/**
* An Array which holds all newly created tables contains DOM-Nodes of
* table-objects
*/
GENTICS.Aloha.TablePlugin.TableRegistry = new Array();
/**
* Holds the active table-object
*/
GENTICS.Aloha.TablePlugin.activeTable = undefined;
/**
* parameters-objects for tables
*
* @param className
* The class of activated tables
*/
GENTICS.Aloha.TablePlugin.parameters = {
className : 'GENTICS_Aloha_Table', // class of editable tables
classSelectionRow : 'GENTICS_Aloha_Table_selectColumn', // class for the upper table-row to select columns
classSelectionColumn : 'GENTICS_Aloha_Table_selectRow', // class for the left bound table-cells to select rows
classLeftUpperCorner : 'GENTICS_Aloha_Table_leftUpperCorner', // class for the left upper corner cell
classTableWrapper : 'GENTICS_Aloha_Table_wrapper', // class of the outest table-wrapping div
classCellSelected : 'GENTICS_Aloha_Cell_selected', // class of cell which are selected (row/column selection)
waiRed : 'GENTICS_WAI_RED', // class that shows wai of div
waiGreen : 'GENTICS_WAI_GREEN', // class that shows wai of div
selectionArea : 10 // width/height of the selection rows (in pixel)
};
/* -- END ATTRIBUTES -- */
/* -- METHODS -- */
/**
* Init method of the Table-plugin transforms all tables in the document
*
* @return void
*/
GENTICS.Aloha.TablePlugin.init = function() {
// add reference to the create layer object
this.createLayer = new GENTICS.Aloha.Table.CreateLayer();
var that = this;
// subscribe for the 'editableActivated' event to activate all tables in the editable
GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, 'editableCreated', function(event, editable) {
// add a mousedown event to all created editables to check if focus leaves a table
editable.obj.bind('mousedown', function(jqEvent) {
GENTICS.Aloha.TablePlugin.setFocusedTable(undefined);
});
editable.obj.find('table').each(function() {
// only convert tables which are editable
if (that.isEditableTable(this)) {
// instantiate a new table-object
var table = new GENTICS.Aloha.Table(this);
table.parentEditable = editable;
// activate the table
// table.activate();
// add the activated table to the TableRegistry
GENTICS.Aloha.TablePlugin.TableRegistry.push(table);
}
});
});
// initialize the table buttons
this.initTableButtons();
GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, 'selectionChanged', function(event, rangeObject) {
if (GENTICS.Aloha.activeEditable) {
// get Plugin configuration
var config = that.getEditableConfig( GENTICS.Aloha.activeEditable.obj );
// show hide buttons regarding configuration and DOM position
if ( jQuery.inArray('table', config) != -1 && GENTICS.Aloha.Selection.mayInsertTag('table') ) {
that.createTableButton.show();
} else {
that.createTableButton.hide();
}
GENTICS.Aloha.TableHelper.unselectCells();
var table = rangeObject.findMarkup(function() {
return this.nodeName.toLowerCase() == 'table';
}, GENTICS.Aloha.activeEditable.obj);
// check wheater we are inside a table
if ( table ) {
// set the scope if either columns or rows are selected
GENTICS.Aloha.FloatingMenu.setScope(that.getUID(GENTICS.Aloha.TableHelper.selectionType));
} else {
if ( that.activeTable ) {
that.activeTable.focusOut();
}
}
// TODO this should not be necessary here!
GENTICS.Aloha.FloatingMenu.doLayout();
}
});
// subscribe for the 'editableActivated' event to activate all tables in the editable
GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, 'editableActivated', function(event, props) {
props.editable.obj.find('table').each(function() {
// shortcut for TableRegistry
var tr = GENTICS.Aloha.TablePlugin.TableRegistry;
for (var i = 0; i < tr.length; i++){
if (tr[i].obj.attr('id') == jQuery(this).attr('id')) {
// activate the table
tr[i].activate();
// and continue with the next table tag
return true;
}
}
// if we come here, we did not find the table in our registry, so we need to create a new one
// only convert tables which are editable
if (that.isEditableTable(this)) {
// instantiate a new table-object
var table = new GENTICS.Aloha.Table(this);
table.parentEditable = props.editable;
// activate the table
table.activate();
// add the activated table to the TableRegistry
GENTICS.Aloha.TablePlugin.TableRegistry.push(table);
}
});
});
// subscribe for the 'editableDeactivated' event to deactivate all tables in the editable
GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, 'editableDeactivated', function(event, properties) {
GENTICS.Aloha.TablePlugin.setFocusedTable(undefined);
GENTICS.Aloha.TableHelper.unselectCells();
// shortcut for TableRegistry
var tr = GENTICS.Aloha.TablePlugin.TableRegistry;
for (var i = 0; i < tr.length; i++){
// activate the table
tr[i].deactivate();
}
});
};
/**
* test if the table is editable
* @return boolean true if the table's parent element is contentEditable, false otherwise
*/
GENTICS.Aloha.TablePlugin.isEditableTable = function(table) {
var parent = jQuery(table.parentNode);
if (parent.contentEditable() == 'true') {
return true;
} else {
return false;
}
};
/**
* initialize the buttons and register them on floating menu
*/
GENTICS.Aloha.TablePlugin.initTableButtons = function () {
var that = this;
// generate the new scopes
GENTICS.Aloha.FloatingMenu.createScope(this.getUID('row'), 'GENTICS.Aloha.global');
GENTICS.Aloha.FloatingMenu.createScope(this.getUID('column'), 'GENTICS.Aloha.global');
GENTICS.Aloha.FloatingMenu.createScope(this.getUID('cell'), 'GENTICS.Aloha.continuoustext');
// the 'create table' button
this.createTableButton = new GENTICS.Aloha.ui.Button({
'iconClass' : 'GENTICS_button GENTICS_button_table',
'size' : 'small',
'tooltip' : this.i18n('button.createtable.tooltip'),
'onclick' : function (element, event) {
GENTICS.Aloha.TablePlugin.createDialog(element.btnEl.dom);
}
});
// add to floating menu
GENTICS.Aloha.FloatingMenu.addButton(
'GENTICS.Aloha.continuoustext',
this.createTableButton,
GENTICS.Aloha.i18n(GENTICS.Aloha, 'floatingmenu.tab.insert'),
1
);
// now the specific table buttons
// for columns
GENTICS.Aloha.FloatingMenu.addButton(
this.getUID('column'),
new GENTICS.Aloha.ui.Button({
'iconClass' : 'GENTICS_button GENTICS_button_addColumnLeft',
'size' : 'small',
'tooltip' : this.i18n('button.addcolleft.tooltip'),
'onclick' : function () {
if (that.activeTable) {
that.activeTable.addColumnsLeft();
}
}
}),
GENTICS.Aloha.i18n(this, 'floatingmenu.tab.table'),
1
);
GENTICS.Aloha.FloatingMenu.addButton(
this.getUID('column'),
new GENTICS.Aloha.ui.Button({
'iconClass' : 'GENTICS_button GENTICS_button_addColumnRight',
'size' : 'small',
'tooltip' : this.i18n('button.addcolright.tooltip'),
'onclick' : function () {
if (that.activeTable) {
that.activeTable.addColumnsRight();
}
}
}),
GENTICS.Aloha.i18n(this, 'floatingmenu.tab.table'),
1
);
GENTICS.Aloha.FloatingMenu.addButton(
this.getUID('column'),
new GENTICS.Aloha.ui.Button({
'iconClass' : 'GENTICS_button GENTICS_button_deleteColumns',
'size' : 'small',
'tooltip' : this.i18n('button.delcols.tooltip'),
'onclick' : function () {
if (that.activeTable) {
var aTable = that.activeTable;
GENTICS.Aloha.showMessage(new GENTICS.Aloha.Message({
title : GENTICS.Aloha.i18n(that, 'Table'),
text : GENTICS.Aloha.i18n(that, 'deletecolumns.confirm'),
type : GENTICS.Aloha.Message.Type.CONFIRM,
callback : function (sel) {
if (sel == 'yes') {
aTable.deleteColumns();
}
}
}));
}
}
}),
GENTICS.Aloha.i18n(this, 'floatingmenu.tab.table'),
1
);
// for rows
GENTICS.Aloha.FloatingMenu.addButton(
this.getUID('row'),
new GENTICS.Aloha.ui.Button({
'iconClass' : 'GENTICS_button GENTICS_button_addRowBefore',
'size' : 'small',
'tooltip' : this.i18n('button.addrowbefore.tooltip'),
'onclick' : function () {
if (that.activeTable) {
that.activeTable.addRowsBefore(true);
}
}
}),
GENTICS.Aloha.i18n(this, 'floatingmenu.tab.table'),
1
);
GENTICS.Aloha.FloatingMenu.addButton(
this.getUID('row'),
new GENTICS.Aloha.ui.Button({
'iconClass' : 'GENTICS_button GENTICS_button_addRowAfter',
'size' : 'small',
'tooltip' : this.i18n('button.addrowafter.tooltip'),
'onclick' : function () {
if (that.activeTable) {
that.activeTable.addRowsAfter(true);
}
}
}),
GENTICS.Aloha.i18n(this, 'floatingmenu.tab.table'),
1
);
GENTICS.Aloha.FloatingMenu.addButton(
this.getUID('row'),
new GENTICS.Aloha.ui.Button({
'iconClass' : 'GENTICS_button GENTICS_button_deleteRows',
'size' : 'small',
'tooltip' : this.i18n('button.delrows.tooltip'),
'onclick' : function () {
if (that.activeTable) {
var aTable = that.activeTable;
GENTICS.Aloha.showMessage(new GENTICS.Aloha.Message({
title : GENTICS.Aloha.i18n(that, 'Table'),
text : GENTICS.Aloha.i18n(that, 'deleterows.confirm'),
type : GENTICS.Aloha.Message.Type.CONFIRM,
callback : function (sel) {
if (sel == 'yes') {
aTable.deleteRows();
}
}
}));
}
}
}),
GENTICS.Aloha.i18n(this, 'floatingmenu.tab.table'),
1
);
this.captionButton = new GENTICS.Aloha.ui.Button({
'iconClass' : 'GENTICS_button GENTICS_button_table_caption',
'size' : 'small',
'tooltip' : this.i18n('button.caption.tooltip'),
'toggle' : true,
'onclick' : function () {
if (that.activeTable) {
// look if table object has a child caption
if ( that.activeTable.obj.children("caption").is('caption') ) {
that.activeTable.obj.children("caption").remove();
// select first cell of table
} else {
var captionText = that.i18n('empty.caption');
var c = jQuery('<caption></caption>');
that.activeTable.obj.append(c);
that.makeCaptionEditable(c, captionText);
// get the editable span within the caption and select it
var cDiv = c.find('div').eq(0);
var captionContent = cDiv.contents().eq(0);
if (captionContent.length > 0) {
var newRange = new GENTICS.Utils.RangeObject();
newRange.startContainer = newRange.endContainer = captionContent.get(0);
newRange.startOffset = 0;
newRange.endOffset = captionContent.text().length;
// blur all editables within the table
that.activeTable.obj.find('div.GENTICS_Table_Cell_editable').blur();
cDiv.focus();
newRange.select();
GENTICS.Aloha.Selection.updateSelection();
}
}
}
}
});
GENTICS.Aloha.FloatingMenu.addButton(
this.getUID('cell'),
this.captionButton,
GENTICS.Aloha.i18n(this, 'floatingmenu.tab.table'),
1
);
// for cells
// add summary field
this.summary = new GENTICS.Aloha.ui.AttributeField({
'width':350
});
this.summary.addListener('keyup', function(obj, event) {
that.activeTable.checkWai();
});
GENTICS.Aloha.FloatingMenu.addButton(
this.getUID('cell'),
this.summary,
GENTICS.Aloha.i18n(this, 'floatingmenu.tab.table'),
1
);
};
/**
* Helper method to make the caption editable
* @param caption caption as jQuery object
* @param captionText default text for the caption
*/
GENTICS.Aloha.TablePlugin.makeCaptionEditable = function(caption, captionText) {
var that = this;
var cSpan = caption.children('div').eq(0);
if (cSpan.length == 0) {
// generate a new div
cSpan = jQuery('<div></div>');
if (caption.contents().length > 0) {
// when the caption has content, we wrap it with the new div
caption.contents().wrap(cSpan);
} else {
// caption has no content, so insert the default caption text
if (captionText) {
cSpan.text(captionText);
}
// and append the div into the caption
caption.append(cSpan);
}
}
// make the div editable
cSpan.contentEditable(true);
cSpan.unbind('mousedown');
// focus on click
cSpan.bind('mousedown', function(jqEvent) {
cSpan.focus();
// stop bubble, otherwise the mousedown of the table is called ...
jqEvent.preventDefault();
jqEvent.stopPropagation();
return false;
});
};
/**
* This function adds the createDialog to the calling element
*
* @param callingElement
* The element, which was clicked. It's needed to set the right
* position to the create-table-dialog.
*/
GENTICS.Aloha.TablePlugin.createDialog = function(callingElement) {
// set the calling element to the layer the calling element mostly will be
// the element which was clicked on it is used to position the createLayer
this.createLayer.set('target', callingElement);
// show the createLayer
this.createLayer.show();
};
/**
* Creates a normal html-table, "activates" this table and inserts it into the
* active Editable
*
* @param cols
* number of colums for the created table
* @param cols
* number of rows for the created table
* @return void
*/
GENTICS.Aloha.TablePlugin.createTable = function(cols, rows) {
// Check if there is an active Editable and that it contains an element (= .obj)
if (GENTICS.Aloha.activeEditable != null && typeof GENTICS.Aloha.activeEditable.obj != 'undefined') {
// create a dom-table object
var table = document.createElement('table');
var tableId = table.id = GENTICS.Aloha.TableHelper.getNewTableID();
var tbody = document.createElement('tbody');
// create "rows"-number of rows
for (var i = 0; i < rows; i++) {
var tr = document.createElement('tr');
// create "cols"-number of columns
for (var j = 0; j < cols; j++) {
var text = document.createTextNode('\u00a0');
var td = document.createElement('td');
td.appendChild(text);
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
// insert at current cursor position
GENTICS.Utils.Dom.insertIntoDOM(jQuery(table), GENTICS.Aloha.Selection.getRangeObject(), jQuery(GENTICS.Aloha.activeEditable.obj));
// if the table is inserted
var tableReloadedFromDOM = document.getElementById(tableId);
var tableObj = new GENTICS.Aloha.Table(tableReloadedFromDOM);
tableObj.parentEditable = GENTICS.Aloha.activeEditable;
// transform the table to be editable
tableObj.activate();
// after creating the table, trigger a click into the first cell to
// focus the content
// for IE set a timeout of 10ms to focus the first cell, other wise it
// won't work
if (jQuery.browser.msie) {
window.setTimeout(function() { tableObj.cells[0].wrapper.get(0).focus(); }, 20);
} else {
tableObj.cells[0].wrapper.get(0).focus();
}
GENTICS.Aloha.TablePlugin.TableRegistry.push(tableObj);
// no active editable => error
}else{
this.error('There is no active Editable where the table can be inserted!');
}
};
GENTICS.Aloha.TablePlugin.setFocusedTable = function(focusTable) {
var that = this;
for (var i = 0; i < GENTICS.Aloha.TablePlugin.TableRegistry.length; i++) {
GENTICS.Aloha.TablePlugin.TableRegistry[i].hasFocus = false;
}
if (typeof focusTable != 'undefined') {
this.summary.setTargetObject(focusTable.obj, 'summary');
if ( focusTable.obj.children("caption").is('caption') ) {
// set caption button
that.captionButton.setPressed(true);
var c = focusTable.obj.children("caption");
that.makeCaptionEditable(c);
}
focusTable.hasFocus = true;
}
GENTICS.Aloha.TablePlugin.activeTable = focusTable;
};
/**
* Calls the GENTICS.Aloha.log function with 'error' level
*
* @see GENTICS.Aloha.log
* @param msg
* The message to display
* @return void
*/
GENTICS.Aloha.TablePlugin.error = function(msg) {
GENTICS.Aloha.Log.error(this, msg);
};
/**
* Calls the GENTICS.Aloha.log function with 'debug' level
*
* @see GENTICS.Aloha.log
* @param msg
* The message to display
* @return void
*/
GENTICS.Aloha.TablePlugin.debug = function(msg) {
GENTICS.Aloha.Log.debug(this, msg);
};
/**
* Calls the GENTICS.Aloha.log function with 'info' level
*
* @see GENTICS.Aloha.log
* @param msg
* The message to display
* @return void
*/
GENTICS.Aloha.TablePlugin.info = function(msg) {
GENTICS.Aloha.Log.info(this, msg);
};
/**
* Calls the GENTICS.Aloha.log function with 'info' level
*
* @see GENTICS.Aloha.log
* @param msg
* The message to display
* @return void
*/
GENTICS.Aloha.TablePlugin.log = function(msg) {
GENTICS.Aloha.log('log', this, msg);
};
/**
* The "get"-method returns the value of the given key.
* First it searches in the config for the property.
* If there is no property with the given name in the
* "config"-object it returns the entry associated with
* in the parameters-object
*
* @param property
* @return void
*
*/
GENTICS.Aloha.TablePlugin.get = function (property) {
if (this.config[property]) {
return this.config[property];
}
if (this.parameters[property]) {
return this.parameters[property];
}
return undefined;
};
/**
* The "set"-method takes a key and a value. It checks if there is a
* key-value pair in the config-object. If so it saves the data in the
* config-object. If not it saves the data in the parameters-object.
*
* @param key the key which should be set
* @param value the value which should be set for the associated key
*/
GENTICS.Aloha.TablePlugin.set = function (key, value) {
if (this.config[key]) {
this.config[key] = value;
}else{
this.parameters[key] = value;
}
};
/**
* Make the given jQuery object (representing an editable) clean for saving
* Find all tables and deactivate them
* @param obj jQuery object to make clean
* @return void
*/
GENTICS.Aloha.TablePlugin.makeClean = function (obj) {
// find all table tags
obj.find('table').each(function() {
// instantiate a new table-object
var table = new GENTICS.Aloha.Table(this);
// deactivate the table
table.deactivate();
});
};
/**
* String representation of the Table-object
*
* @return The plugins namespace (string)
*/
GENTICS.Aloha.TablePlugin.toString = function() {
return this.prefix;
};
/* -- END METHODS -- */
/**************************
+---------------------+
| GENTICS.Aloha.Table |
+---------------------+
***************************/
/**
* Constructor of the table object
*
* @param table
* the dom-representation of the held table
* @return void
*/
GENTICS.Aloha.Table = function(table) {
// set the table attribut "obj" as a jquery represenation of the dom-table
this.obj = jQuery(table);
if ( !this.obj.attr('id') ) {
this.obj.attr('id', GENTICS.Utils.guid());
}
// find the dimensions of the table
var rows = this.obj.find("tr");
var firstRow = jQuery(rows.get(0));
this.numCols = firstRow.children("td, th").length;
this.numRows = rows.length;
// init the cell-attribute with an empty array
this.cells = new Array();
// iterate over table cells and create Cell-objects
var rows = this.obj.find('tr');
for (var i = 0; i < rows.length; i++) {
var row = jQuery(rows[i]);
var cols = row.children();
for (var j = 0; j < cols.length; j++) {
var col = cols[j];
var Cell = new GENTICS.Aloha.Table.Cell(col, this);
this.cells.push(Cell);
}
}
};
/* -- ATTRIBUTES -- */
/**
* Attribute holding the jQuery-table-represenation
*/
GENTICS.Aloha.Table.prototype.obj = undefined;
/**
* The DOM-element of the outest div-container wrapped around the cell
*/
GENTICS.Aloha.Table.prototype.tableWrapper = undefined;
/**
* An array of all Cells contained in the Table
*
* @see GENTICS.Aloha.Table.Cell
*/
GENTICS.Aloha.Table.prototype.cells = undefined;
/**
* Number of rows of the table
*/
GENTICS.Aloha.Table.prototype.numRows = undefined;
/**
* Number of rows of the table
*/
GENTICS.Aloha.Table.prototype.numCols = undefined;
/**
* Flag wether the table is active or not
*/
GENTICS.Aloha.Table.prototype.isActive = false;
/**
* Flag wether the table is focused or not
*/
GENTICS.Aloha.Table.prototype.hasFocus = false;
/**
* The editable which contains the table
*/
GENTICS.Aloha.Table.prototype.parentEditable = undefined;
/**
* Flag to check if the mouse was pressed. For row- and column-selection.
*/
GENTICS.Aloha.Table.prototype.mousedown = false;
/**
* ID of the column which was pressed when selecting columns
*/
GENTICS.Aloha.Table.prototype.clickedColumnId = -1;
/**
* ID of the row which was pressed when selecting rows
*/
GENTICS.Aloha.Table.prototype.clickedRowId = -1;
/**
* collection of columnindexes of the columns which should be selected
*/
GENTICS.Aloha.Table.prototype.columnsToSelect = new Array();
/**
* collection of rowindexes of the rows which should be selected
*/
GENTICS.Aloha.Table.prototype.rowsToSelect = new Array();
/**
* contains the plugin id used for interaction with the floating menu
*/
GENTICS.Aloha.Table.prototype.fmPluginId = undefined;
/* -- END ATTRIBUTES -- */
/* -- METHODS -- */
/**
* Wrapper-Mehotd to return a property of GENTICS.Aloha.TablePlugin.get
*
* @see GENTICS.Aloha.TablePlugin.get
* @param property
* the property whichs value should be return
* @return the value associated with the property
*/
GENTICS.Aloha.Table.prototype.get = function(property) {
return GENTICS.Aloha.TablePlugin.get(property);
};
/**
* Wrapper-Method for GENTICS.Aloha.TablePlugin.set
*
* @see GENTICS.Aloha.TablePlugin.set
* @param key
* the key whichs value should be set
* @param value
* the value for the key
* @return void
*/
GENTICS.Aloha.Table.prototype.set = function(key, value) {
GENTICS.Aloha.TablePlugin.set(key, value);
};
/**
* Transforms the existing dom-table into an editable aloha-table. In fact it
* replaces the td-elements with equivalent GENTICS.Aloha.Table.Cell-elements
* with attached events.
* Furthermore it creates wrapping divs to realize a click-area for row- and
* column selection and also attaches events.
*
* @return void
*/
GENTICS.Aloha.Table.prototype.activate = function() {
if (this.isActive) {
return;
}
var that = this;
// alter the table attributes
this.obj.addClass(this.get('className'));
this.obj.contentEditable(false);
// set an id to the table if not already set
if (this.obj.attr('id') == '') {
this.obj.attr('id', GENTICS.Aloha.TableHelper.getNewTableID());
}
// unset the selection type
GENTICS.Aloha.TableHelper.selectionType = undefined;
this.obj.bind('keydown', function(jqEvent){
if (!jqEvent.ctrlKey && !jqEvent.shiftKey) {
if (GENTICS.Aloha.TableHelper.selectedCells.length > 0 && GENTICS.Aloha.TableHelper.selectedCells[0].length > 0) {
GENTICS.Aloha.TableHelper.selectedCells[0][0].firstChild.focus();
}
}
});
// handle click event of the table
// this.obj.bind('click', function(e){
// // stop bubbling the event to the outer divs, a click in the table
// // should only be handled in the table
// e.stopPropagation();
// return false;
// });
this.obj.bind('mousedown', function(jqEvent) {
// focus the table if not already done
if (!that.hasFocus) {
that.focus();
}
// DEACTIVATED by Haymo prevents selecting rows
// // if a mousedown is done on the table, just focus the first cell of the table
// setTimeout(function() {
// var firstCell = that.obj.find('tr:nth-child(2) td:nth-child(2)').children('div[contenteditable=true]').get(0);
// GENTICS.Aloha.TableHelper.unselectCells();
// jQuery(firstCell).get(0).focus();
// // move focus in first cell
// that.obj.cells[0].wrapper.get(0).focus();
// }, 0);
// stop bubbling and default-behaviour
jqEvent.stopPropagation();
jqEvent.preventDefault();
return false;
});
// ### create a wrapper for the table (@see HINT below)
// wrapping div for the table to suppress the display of the resize-controls of
// the editable divs within the cells
var tableWrapper = jQuery('<div class="' + this.get('classTableWrapper') + '"></div>');
tableWrapper.contentEditable(false);
// wrap the tableWrapper around the table
this.obj.wrap(tableWrapper);
// :HINT The outest div (Editable) of the table is still in an editable
// div. So IE will surround the the wrapper div with a resize-border
// Workaround => just disable the handles so hopefully won't happen any ugly stuff.
// Disable resize and selection of the controls (only IE)
// Events only can be set to elements which are loaded from the DOM (if they
// were created dynamically before) ;)
var htmlTableWrapper = this.obj.parents('.' + this.get('classTableWrapper'));
htmlTableWrapper.get(0).onresizestart = function(e) { return false; };
htmlTableWrapper.get(0).oncontrolselect = function(e) { return false; };
this.tableWrapper = this.obj.parents('.' + this.get('classTableWrapper')).get(0);
jQuery(this.cells).each(function () {
this.activate();
});
// after the cells where replaced with contentEditables ... add selection cells
// first add the additional columns on the left side
this.attachSelectionColumn();
// then add the additional row at the top
this.attachSelectionRow();
// attach events for the last cell
this.attachLastCellEvents();
// make the caption editable
this.makeCaptionEditable();
// check WAI status
this.checkWai();
// set flag, that the table is activated
this.isActive = true;
// throw a new event when the table has been activated
GENTICS.Aloha.EventRegistry.trigger(
new GENTICS.Aloha.Event(
'tableActivated',
GENTICS.Aloha,
[ this ]
)
);
};
/**
* Make the table caption editable (if present)
*/
GENTICS.Aloha.Table.prototype.makeCaptionEditable = function() {
var caption = this.obj.find('caption').eq(0);
if (caption) {
GENTICS.Aloha.TablePlugin.makeCaptionEditable(caption);
}
};
/**
* check the WAI conformity of the table and sets the attribute.
*/
GENTICS.Aloha.Table.prototype.checkWai = function() {
var w = this.wai;
w.removeClass(this.get('waiGreen'));
w.removeClass(this.get('waiRed'));
if (this.obj[0].summary.length > 5 ) {
w.addClass(this.get('waiGreen'));
} else {
w.addClass(this.get('waiRed'));
}
};
/**
* Add the selection-column to the left side of the table and attach the events
* for selection rows
*
* @return void
*/
GENTICS.Aloha.Table.prototype.attachSelectionColumn = function() {
// create an empty cell
var emptyCell = jQuery('<td>');
// set the unicode ' ' code
emptyCell.html('\u00a0');
var that = this;
var rows = this.obj.context.rows;
// add a column before each first cell of each row
for (var i = 0; i < rows.length; i++) {
var rowObj = jQuery(rows[i]);
var columnToInsert = emptyCell.clone();
columnToInsert.addClass(this.get('classSelectionColumn'));
columnToInsert.css('width', this.get('selectionArea') + 'px');
rowObj.find('td:first').before(columnToInsert);
// rowIndex + 1 because an addtional row is still added
var rowIndex = i + 1;
// this method sets the selection-events to the cell
this.attachRowSelectionEventsToCell(columnToInsert);
}
};
/**
* Binds the needed selection-mouse events to the given cell
*
* @param cell
* The jquery object of the table-data field
* @return void
*/
GENTICS.Aloha.Table.prototype.attachRowSelectionEventsToCell = function(cell){
var that = this;
// unbind eventually existing events of this cell
cell.unbind('mousedown');
cell.unbind('mouseover');
// prevent ie from selecting the contents of the table
cell.get(0).onselectstart = function() { return false; };
cell.bind('mousedown', function(e){
// set flag that the mouse is pressed
that.mousedown = true;
return that.rowSelectionMouseDown(e);
});
cell.bind('mouseover', function(e){
// only select more crows if the mouse is pressed
if ( that.mousedown ) {
return that.rowSelectionMouseOver(e);
}
});
};
/**
* Mouse-Down event for the selection-cells on the left side of the table