-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.summary-04.js
1201 lines (1020 loc) · 37.8 KB
/
jquery.summary-04.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
/** @file
*
* A simple jQuery widget to present an article along with summarization
* information about that article.
*
* Requires:
* jquery.js
* jquery-ui.js
* jquery.delegateHoverIntent.js
* rangy.js
* rangy/seializer.js
*/
(function($) {
/** @brief Summary widget */
$.fn.summary = function(options) {
options = options || {};
return this.each(function() {
var $el = $(this);
$el.data('summary', new $.Summary( $el, options ));
});
};
/** @brief The Summary class */
$.Summary = function($el, options) {
return this.init($el, options);
};
$.Summary.prototype = {
options: {
src: null, // The URL of the original source
metadata: null, /* The URL of the
* summarization/characterization metadata
*/
showSentences: 5, /* The minimum number of sentences to
* present
*/
lineHeight: -1, /* The height of a single line
* (measured in renderXml() if -1);
*/
useColor: false, // Color sentences based upon rank?
rankOpacity: 0.3, // The default opacity for rank items
animSpeed: 200 // Speed (in ms) of animations
},
/** @brief Initialize this new instance.
* @param el The jQuery DOM element.
* @param options Initialization options.
*
* @return this for a fluent interface.
*/
init: function(el, options) {
var self = this;
var opts = $.extend(true, {}, self.options, options);
self.element = el;
self.options = opts;
self.metadata = null;
var $gp = self.element.parent().parent();
self.$control = $gp.find('.control-pane');
self.$threshold = self.$control.find('.threshold');
self.$thresholdValues = self.$threshold.find('.values');
rangy.init();
self.cssTag = rangy.createCssClassApplier(
'ui-state-default tagged',
true);
self.cssSelect = rangy.createCssClassApplier('selected', true);
self._bindEvents();
// Kick off the retrieval of the metadata
var getMetadata = $.get(opts.metadata);
self.element.addClass('loading');
getMetadata.done(function( data ) {
self.metadata = data;
self.render();
self.element.removeClass('loading');
});
getMetadata.fail(function() {
alert("Cannot retrieve metadata '"+ opts.metadata +"'");
});
},
/** @brief Invoked to cleanup this widget. */
destroy: function() {
self._unbindEvents();
},
/** @brief Render the summary information over the main article.
*/
render: function() {
var self = this;
var opts = self.options;
if (self.metadata === null) { return; }
// Renter the XML
self.renderXml( self.metadata );
// Find all sentences and bucket them based upon 'rank'
self.$p = self.element.find('p');
self.$s = self.$p.find('.sentence');
self.$kws = self.element.find('.keyword');
self.ranks = [];
self.$s.each(function() {
var $el = $(this);
var rank = parseFloat($el.attr('rank'));
if (isNaN(rank)) { return; }
// Treat the rank as an integer percentile (0 .. 100).
rank = parseInt(rank * 100, 10);
if (self.ranks[rank] === undefined) { self.ranks[rank] = []; }
if (opts.useColor !== false)
{
// Adjust the color of the sentence based upon the rank
var base = (opts.useColor === true
? 221
: opts.useColor);
var red = base - Math.ceil( base * (rank / 100) );
var green = red;
var blue = red;
var color = 'rgb('+ red +','+ green +','+ blue +')';
$el.css({color:color});
}
// Include the rank within the sentence
$('#tmpl-sentence-rank')
.tmpl({rank:rank})
.css('opacity', opts.rankOpacity)
.prependTo($el);
// And sentence controls
$('#tmpl-sentence-controls')
.tmpl()
.appendTo($el);
self.ranks[rank].push($el);
});
/* Find the highest rank that will include at least opts.showSentences
* sentences.
*/
var num = 0;
var minThreshold = -1;
var maxThreshold = 100;
for (var idex = self.ranks.length - 1; idex > 0; idex--)
{
var ar = self.ranks[idex];
if (ar === undefined) { continue; }
num += ar.length;
if (num > opts.showSentences)
{
minThreshold = Math.floor(idex / 10) * 10;
break;
}
}
self.renderControl( minThreshold, maxThreshold );
},
/** @brief Given XML content, convert it to stylable HTML.
* @param xml The XML to render.
*
*/
renderXml: function(xml) {
var self = this;
var opts = self.options;
var $xml = $( xml );
/* Convert the XML to HTML that can be styled.
*
* First, handle any <header>, adding a <header> element BEFORE this
* element.
*/
var $header = $('<header />').appendTo( self.element );
$xml.find('document').children().each(function() {
var $el = $(this);
switch (this.nodeName)
{
case 'title':
var $h1 = $('<h1 />');
if (opts.src !== null)
{
var $a = $('<a />')
.attr('href', opts.src)
.text( $el.text() )
.appendTo($h1);
}
else
{
$h1.text( $el.text() );
}
$header.append( $h1 );
break;
case 'published':
var str = $el.find('date').text() +' '
+ $el.find('time').text();
var date = new Date(str);
var $time = $('<time />')
.attr('datetime', date.toISOString())
.attr('pubdate', true)
.text(str)
.appendTo($header);
break;
case 'keywords':
// Process any XML <keyword> elements
$('#tmpl-header-keywords')
.tmpl({ keywords: $el.find('keyword') })
.appendTo($header);
break;
case 'body':
// Process any XML <section> elements
$el.find('section').each(function() {
// Leave this for later
var $div = $('<section />')
.appendTo( self.element );
// Convert the XML <p> to an HTML <p>
$(this).find('p').each(function() {
var $p = $('<p />')
.appendTo($div);
// Convert the XML <s> to an HTML <div>
$(this).find('s').each(function() {
var $s = $(this);
var $sEl = $('<div />')
.addClass('sentence')
.attr('rank', $s.attr('rank'))
.appendTo($p);
// Assemble the HTML from the XML
$.each(this.childNodes, function() {
var $node = $(this);
switch (this.nodeName)
{
case '#text':
$('#tmpl-sentence-text')
.tmpl( {node:$node} )
.appendTo( $sEl );
break;
case 'keyword':
$('#tmpl-sentence-keyword')
.tmpl( {node:$node} )
.appendTo( $sEl );
break;
}
});
if (opts.lineHeight < 1)
{
opts.lineHeight = $sEl.height();
}
//$p.append( $sEl );
});
//$div.append($p);
});
//self.element.append( $div );
});
break;
default:
$header.append( $el );
break;
}
});
},
/** @brief Render the summary dialog with controls.
* @param minThreshold The threshold minimum.
* @param maxThreshold The threshold maximum.
*/
renderControl: function( minThreshold, maxThreshold ) {
var self = this;
var opts = self.options;
self.$control.find('.show,.buttons').buttonset();
self.threshold( minThreshold, maxThreshold);
},
/** @brief Change the rank threshold.
* @param min The minimum threshold.
* @param max The maximum threshold.
*
*/
threshold: function( min, max) {
var self = this;
var opts = self.options;
var isExpand = (min < self.minThreshold);
// Update the threshold and threshold value presentation
self.minThreshold = min;
self.maxThreshold = max;
var str = min +' - ' + max;
self.$thresholdValues.text( str );
/* Initially mark all sentences as 'NOT highlighted' and all
* paragraphs as 'NOT shown'
*/
self.$s.addClass('noHighlight');
// Mark all sentences within the threshold range 'toHighlight'
for (var idex = self.maxThreshold; idex >= self.minThreshold; idex--)
{
var ar = self.ranks[idex];
if (ar === undefined) { continue; }
var nItems = ar.length;
for (var jdex = 0; jdex < nItems; jdex++)
{
var $s = ar[jdex];
if ( ((opts.view !== 'all') && ($s.data('isHidden'))) ||
((opts.view === 'starred') && (! $s.data('isStarred'))) )
{
continue;
}
// Mark this sentence as TO BE highlighted
$s.addClass('toHighlight')
.removeClass('noHighlight');
}
}
/* Hide expansion controls for sentences that have no siblings that
* would be expanded.
*/
self.$s.removeClass('hide-expand');
self.$p.each(function() {
var $p = $(this);
var $ss = $p.find('.sentence');
if ($ss.length === 1)
{
// Only one sentence period -- Always hide
$ss.addClass('hide-expand');
return;
}
$ss.each(function() {
var $s = $(this);
var $pS = $s.prev();
var $nS = $s.next();
if ($pS.length < 1)
{
// First sentence
if ($s.hasClass('noHighlight') || // is NOT highlighted
($nS.length < 1) || // No next
$nS.hasClass('toHighlight')) // Next is highlighted
{
$s.addClass('hide-expand');
}
}
else if ($nS.length < 1)
{
// Last sentence
if ($s.hasClass('noHighlight') || // is NOT highlighted
($pS.length < 1) || // No previous
$pS.hasClass('toHighlight')) // Prev is highlighted
{
$s.addClass('hide-expand');
}
}
else if ( $pS.hasClass('toHighlight') &&// Prev is highlighted
$nS.hasClass('toHighlight') ) // Next is highlighted
{
$s.addClass('hide-expand');
}
});
});
// Hide sentences
self.$s.filter('.noHighlight')
.removeClass('noHighlight expanded expansion')
.filter('.highlight')
.removeClass('highlight', opts.animSpeed * 2, function() {
var $s = $(this);
$s.younger()
.removeData('isHighlighted');
});
// Show sentences
self.$s.filter('.toHighlight')
.removeClass('toHighlight')
.addClass('highlight', opts.animSpeed * 2, function() {
var $s = $(this);
// Remove any per-sentence expansion indicators.
$s.removeClass('expanded expansion');
// If the current senntence was already highlighted...
if ($s.data('isHighlighted'))
{
// Already highlighted so we need to adjust the age
// expanding (older)
// contracting (younger)
if (isExpand)
{
// older
$s.older();
}
else
{
// younger
$s.younger();
}
}
$s.data('isHighlighted', true);
});
},
/******************************************************************
* "Private" methods
*
*/
/** @brief Given a jQuery DOM sentence element (.sentence),
* toggle the 'star' setting.
* @param $s The jQuery DOM sentence element
*
*/
_toggleStar: function($s) {
var self = this;
var opts = self.options;
var $el = $s.find('.controls .star');
// (un)Star this sentence
if ($s.data('isStarred'))
{
$s.removeClass('starred')
.removeData('isStarred');
$el.removeClass('su-state-active');
}
else
{
$s.addClass('starred')
.data('isStarred', true);
$el.addClass('su-state-active');
}
return this;
},
/** @brief Given a jQuery DOM sentence element (.sentence),
* toggle the visibility setting.
* @param $s The jQuery DOM sentence element
*
*/
_toggleHide: function($s) {
var self = this;
var opts = self.options;
var $el = $s.find('.controls .expand');
// (un)Hide this sentence
if ($s.data('isHidden'))
{
$s.removeClass('hidden')
.removeData('isHidden');
}
else
{
/* If this sentence was presented because of an expansion
* of a nearby sentence, then hide should not hide it
* permanently but rather hide this part of the expansion.
*/
var expansion = $s.hasClass('expansion');
var hideDone = function() {
/*
if ($s.parent().find(':not([class*="hidden"])').length < 1)
{
$s.parent().addClass('hidden', opts.animSpeed / 4);
}
// */
};
if (! expansion)
{
$s.data('isHidden', true);
if (opts.view !== 'all')
{
$s.addClass('hidden', opts.animSpeed, hideDone);
}
else
{
// Make the change immediately
$s.addClass('hidden');
hideDone();
}
}
else
{
$s.removeClass('expansion', opts.animSpeed, hideDone);
}
}
return this;
},
/** @brief Given a jQuery DOM sentence (.sentence), expand it.
* @param $s The jQuery DOM sentence element
*/
_expand: function($s) {
var self = this;
var opts = self.options;
var $el = $s.find('.controls .expand');
var $prev = $s.prev();
var $next = $s.next();
var expandDone = function() {
var $this = $(this);
$this.addClass('expansion');
$this.css('display', '');
$el.attr('title', 'collapse');
};
if ($s.data('isExpanding') || $s.hasClass('expanded'))
{
// Already (being) expanded
return;
}
// Mark this sentence as being expanded
$s.data('isExpanding', true);
$s.addClass('expanded', opts.animSpeed);
// If the previous sibling is NOT visible...
if ( ! $prev.hasClass('hidden highlight expanded expansion') )
{
$prev.addClass('expansion', opts.animSpeed);
}
// If the next sibling NOT is visible...
if ( ! $next.hasClass('hidden highlight expanded expansion') )
{
$next.addClass('expansion', opts.animSpeed, expandDone);
}
// Remove our marker indicating that this sentence is being expanded
$s.removeData('isExpanding');
return this;
},
/** @brief Given a jQuery DOM sentence (.sentence), collapse it.
* @param $s The jQuery DOM sentence element
*/
_collapse: function($s) {
var self = this;
var opts = self.options;
var $el = $s.find('.controls .expand');
var $prev = $s.prev();
var $next = $s.next();
var completionsNeeded = 1;
var collapseDone = function() {
if ( --completionsNeeded > 0) { return; }
if (! $s.hasClass('highlight'))
{
/* The target sentence is NOT highlighted, so ensure that
* sentence controls are hidden and NOT in "hover mode" and any
* selection controls are removed.
*/
$s.removeClass('ui-hover');
$s.find('.controls .su-icon').css('opacity', '');
$s.find('.selection-controls').remove();
}
// Change the expand/contract title back to "expand"
$el.attr('title', 'expand');
// Mark collapse completed
$s.removeData('isCollapsing');
};
var collapseExpansion = function($sib) {
completionsNeeded++;
$sib.removeClass('expansion', opts.animSpeed, collapseDone);
if ($sib.hasClass('expanded'))
{
self._collapse($sib);
}
};
if ($s.data('isCollapsing'))
{
// This sentence is already being collapsed
return;
}
if ($s.hasClass('expanded'))
{
$s.removeClass('expanded', opts.animSpeed);
}
else if (! $s.hasClass('expansion'))
{
return;
}
// Mark this sentence as being collapsed
$s.data('isCollapsing', true);
// If the previous sibling is visible...
if ($prev.hasClass('expansion'))
{
collapseExpansion($prev);
}
// If the next sibling is visible...
if ($next.hasClass('expansion'))
{
collapseExpansion($next);
}
collapseDone();
return this;
},
/** @brief Given a jQuery DOM sentence element (.sentence),
* toggle the expand/collapse setting.
* @param $s The jQuery DOM sentence element
*
*/
_toggleExpand: function($s) {
var self = this;
var opts = self.options;
var $el = $s.find('.controls .expand');
if ($s.hasClass('expanded'))
{
// Collapse
self._collapse($s);
}
else
{
// Expand
self._expand($s);
}
return this;
},
/** @brief Set the caret/cursor position within the given element
* @param $el The jQuery/DOM element representing the input control;
* @param pos The desired caret/cursor position;
*/
_bindEvents: function() {
var self = this;
var opts = self.options;
var $parent = self.element.parent();
var $gp = $parent.parent();
/*************************************************************
* Handle toggling the primary controls
*
*/
$gp.delegate('.control-pane .toggle-controls', 'click',
function() {
var $ctl = $(this).siblings('.controls');
if ($ctl.is(":visible"))
{
$ctl.hide(opts.animSpeed/ 4);
}
else
{
$ctl.show(opts.animSpeed / 4);
}
});
/*************************************************************
* Handle clicks on control buttons.
*
*/
$gp.delegate('.controls input, .controls button', 'click',
function() {
var $el = $(this);
var name = $el.attr('name');
var newMin = self.minThreshold;
switch (name)
{
case 'view':
var val = $el.val();
opts.view = val;
self.element.removeClass('all starred normal')
.addClass(val);
self.threshold(self.minThreshold, self.maxThreshold);
break;
case 'threshold-down':
if (newMin > 9) { newMin -= 10; }
break;
case 'threshold-up':
if (newMin < (self.maxThreshold - 9)) { newMin += 10; }
}
if ((name === 'threshold-up') || (name === 'threshold-down'))
{
if (newMin === self.minThreshold)
{
$el.anim('flash');
}
else
{
self.threshold(newMin, self.maxThreshold);
}
}
});
/*************************************************************
* Hover over a keyword changes the color of all keywords
*
*/
$gp.delegateHoverIntent('.keyword', function(e) {
var $kw = $(this);
if ((e.type === 'hover-in') && $kw.hasClass('ui-state-highlight'))
{
// Ignore hover over keywords that are already highlighted
return;
}
var name = $kw.attr('name');
var $kws = self.$kws.filter('[name='+ name +']');
switch (e.type)
{
case 'hover-out':
$kws.removeClass('keyword-hover');
break;
case 'hover-in':
$kws.addClass('keyword-hover');
break;
}
});
/*************************************************************
* Hover over a rank increases the opacity of them all
*
*/
$parent.delegateHoverIntent('.rank', function(e) {
var $el = $(this);
//console.log('.rank hover: '+ e.type);
switch (e.type)
{
case 'hover-out':
// Remove the opacity change for all ranks
self.$s.find('.rank').css('opacity', opts.rankOpacity);
/* Don't let the 'hover-out' propagate so other hover-based
* events won't be inadvertantly triggered
* (e.g. sentence controls hidden).
*/
e.stopPropagation();
break;
case 'hover-in':
self.$s.find('.rank').css('opacity', 1.0);
break;
}
});
/*************************************************************
* Hover over a sentence to show the sentence controls
*
*/
$parent.delegateHoverIntent('.sentence', function(e) {
var $s = $(this);
var $p = $s.parents('p:first');
if ( $s.data('isCollapsing') ||
((! $s.hasClass('highlight')) &&
(! $s.hasClass('expanded')) &&
(! $s.hasClass('expansion')) &&
(! $s.hasClass('keyworded'))) )
{
return;
}
//console.log('.sentence hover: '+ e.type);
switch (e.type)
{
case 'hover-in':
$s.addClass('ui-hover');
break;
case 'hover-out':
$s.removeClass('ui-hover');
break;
}
});
/*************************************************************
* Mouse over for sentence controls
*
*/
$parent.delegate('.sentence .controls .su-icon',
'mouseenter mouseleave',
function(e) {
var $el = $(this);
switch (e.type)
{
case 'mouseenter':
$el.css('opacity', 1.0);
break;
case 'mouseleave':
$el.css('opacity', '');
break;
}
});
/*************************************************************
* Click handler for sentence controls
*
*/
$parent.delegate('.sentence .controls .su-icon', 'click',
function(e) {
var $el = $(this);
var $s = $el.parents('.sentence:first');
console.log('control click: '+ $el.attr('class'));
if ($el.hasClass('star'))
{
self._toggleStar($s);
}
else if ($el.hasClass('hide'))
{
self._toggleHide($s);
}
else if ($el.hasClass('expand'))
{
self._toggleExpand($s);
}
});
/*************************************************************
* Click handler for non-highlighted sentences
*
*/
$parent.delegate('.sentence:not(.highlight,.expanded,.expansion)',
'click', function(e) {
var $s = $(this);
if ($s.hasClass('hide-expand'))
{
// Is there a highlighted neighbor near by?
var $sib = $s.siblings('.highlight:first');
if ($sib.length < 1)
{
/* No highlighted neighbor. Use the nearest neighbor that
* does NOT have '.hide-expand'
*/
$sib = $s.siblings(':not(.hide-expand):first');
if ($sib.length < 1)
{
/* NO sentences without '.hide-expand'
* Remove 'hide-expand' from the target and toggle
* it.
*/
$s.removeClass('hide-expand');
$sib = $s;
console.log('un-highlighted sentence click - '
+ 'NO sentences without hide-expand');
}
else
{
console.log('un-highlighted sentence click - '
+ 'sibling without hide-expand');
}
}
else
{
console.log('un-highlighted sentence click - '
+ 'toggle nearest highlight');
}
self._toggleExpand($sib);
}
else
{
console.log('un-highlighted sentence click - toggle');
self._toggleExpand($s);
}
});
/*************************************************************
* Clicking on a keyword shows all sentences with that keyword
*
*/
$parent.delegate('header .keyword', 'click', function() {
var $kw = $(this);
var toggleOn = (! $kw.hasClass('ui-state-highlight'));
var name = $kw.attr('name');
var $kws = $parent.find('article p .keyword');
var $hl = $kws.filter('[name='+ name +']');
if (toggleOn)
{
// Make any sentence currently visible "older"
self.$s.filter('.highlight,.expansion').older();
// Highlight the keyword control
$kw.addClass('ui-state-highlight');
/* For each keyword that should be highlighted, highlight it
* and ensure that it's containing sentence and paragraph are
* visible.
*/
$hl.each(function() {
var $el = $(this);
var $s = $el.parent();
var $p = $s.parent();
$el.addClass('ui-state-highlight');
$s.addClass('keyworded', opts.animSpeed);
});
}
else
{
/* For each keyword that should be un-highlighted, un-highlight
* it and then re-apply threshold to ensure a proper view
*/
$hl.each(function() {
var $el = $(this);
var $s = $el.parent();
var $p = $s.parent();
$el.removeClass('ui-state-highlight');
var nLeft = $s.find('.keyword.ui-state-highlight').length;
if (nLeft < 1)
{
// No more keywords in this sentence
$s.removeClass('keyworded', opts.animSpeed);
}
});
// Remove any 'old' class
self.$s.filter('[class*=" old"]').younger();
// Remove the highlight from the keyword control
$kw.removeClass('ui-state-highlight');
}
});
/*************************************************************
* Click handler for selection controls
*
* This is broken into mousedown/mouseup in order to squelch
* any mouse events if necessary in order to avoid modifying
* the current selection.
*
*/
$parent.delegate('.sentence .selection-controls .su-icon',
'mousedown mouseup',
function(e) {
var $el = $(e.target);
var $ctl = $el.parents('.selection-controls:first');
console.log('selection-controls '+ e.type
+': '+ $el.attr('class'));
switch (e.type)
{
case 'mousedown':
$ctl.data('mousedown', true);