1+ ( function ( $ ) {
2+ $ . Redactor . prototype . alphalist = function ( ) {
3+ return {
4+
5+ init : function ( ) {
6+ var self = this ;
7+
8+ var button = this . button . addAfter ( 'orderedlist' , 'alphalist' , this . lang . get ( 'alphalist' ) ) ;
9+ if ( ! button || ! button . length ) {
10+ button = this . button . add ( 'alphalist' , this . lang . get ( 'alphalist' ) ) ;
11+ }
12+
13+ self . $alphalistButton = button ;
14+
15+ button . on ( 'click' , function ( e ) {
16+ e . preventDefault ( ) ;
17+ self . alphalist . toggle . call ( self ) ;
18+ } ) ;
19+
20+ $ ( this . $editor ) . on ( 'keyup mouseup' , function ( ) {
21+ self . alphalist . updateButtonState . call ( self ) ;
22+ } ) ;
23+ } ,
24+
25+ isAlphaList : function ( ) {
26+ var current = this . selection . getBlock ( ) ;
27+ if ( ! current ) return false ;
28+ var parent = current . parentNode ;
29+ return $ ( parent ) . is ( 'ol.alpha-list' ) ;
30+ } ,
31+
32+ updateButtonState : function ( ) {
33+ if ( this . alphalist . isAlphaList . call ( this ) ) {
34+ this . $alphalistButton . addClass ( 'redactor-act' ) ;
35+ } else {
36+ this . $alphalistButton . removeClass ( 'redactor-act' ) ;
37+ }
38+
39+ var $olButton = this . button . get ( 'orderedlist' ) ;
40+ if ( $olButton ) {
41+ if ( this . alphalist . isAlphaList . call ( this ) ) {
42+ $olButton . removeClass ( 'redactor-act' ) ;
43+ }
44+ }
45+ } ,
46+
47+ focusEnd : function ( $element ) {
48+ var $target = $element . is ( 'ol, ul' ) ? $element . find ( 'li' ) . last ( ) : $element ;
49+ if ( ! $target . length ) return ;
50+ var range = document . createRange ( ) ;
51+ var sel = window . getSelection ( ) ;
52+ range . selectNodeContents ( $target [ 0 ] ) ;
53+ range . collapse ( false ) ;
54+ sel . removeAllRanges ( ) ;
55+ sel . addRange ( range ) ;
56+ this . alphalist . updateButtonState . call ( this ) ;
57+ } ,
58+
59+ toggle : function ( ) {
60+ var current = this . selection . getBlock ( ) ;
61+ if ( ! current ) return ;
62+
63+ var $editor = $ ( this . $editor ) ;
64+ var selection = this . selection . get ( ) ;
65+ var selectedBlocks = [ ] ;
66+ var parent = current . parentNode ;
67+
68+ if ( $ ( parent ) . hasClass ( 'alpha-list' ) ) {
69+ var $ol = $ ( parent ) ;
70+ var $items = $ol . find ( 'li' ) ;
71+ var blocks = [ ] ;
72+ $items . each ( function ( ) {
73+ blocks . push ( $ ( '<p>' ) . html ( $ ( this ) . html ( ) ) ) ;
74+ } ) ;
75+ $ol . replaceWith ( blocks ) ;
76+
77+ this . $alphalistButton . removeClass ( 'redactor-act' ) ;
78+
79+ this . alphalist . focusEnd . call ( this , $ ( blocks [ blocks . length - 1 ] ) ) ;
80+ return ;
81+ }
82+
83+ if ( parent . nodeName === 'UL' || parent . nodeName === 'OL' ) {
84+ if ( ! $ ( parent ) . hasClass ( 'alpha-list' ) ) {
85+ var $newOl = $ ( '<ol>' ) . addClass ( 'alpha-list' ) . html ( $ ( parent ) . html ( ) ) ;
86+ $ ( parent ) . replaceWith ( $newOl ) ;
87+ }
88+
89+ this . alphalist . focusEnd . call ( this , $newOl ) ;
90+ this . alphalist . updateButtonState . call ( this ) ;
91+ return ;
92+ }
93+
94+ if ( selection && ! selection . isCollapsed ) {
95+ var range = selection . getRangeAt ( 0 ) ;
96+ $editor . children ( ) . each ( function ( ) {
97+ if ( range . intersectsNode ( this ) ) {
98+ selectedBlocks . push ( this ) ;
99+ }
100+ } ) ;
101+ }
102+
103+ if ( selectedBlocks . length === 0 ) {
104+ selectedBlocks . push ( current ) ;
105+ }
106+
107+ var $ol = $ ( '<ol>' ) . addClass ( 'alpha-list' ) ;
108+ $ ( selectedBlocks ) . each ( function ( ) {
109+ var content = $ ( this ) . html ( ) ;
110+ $ol . append ( $ ( '<li>' ) . html ( content ) ) ;
111+ } ) ;
112+
113+ $ ( selectedBlocks [ 0 ] ) . replaceWith ( $ol ) ;
114+ for ( var i = 1 ; i < selectedBlocks . length ; i ++ ) {
115+ $ ( selectedBlocks [ i ] ) . remove ( ) ;
116+ }
117+ this . alphalist . focusEnd . call ( this , $ol ) ;
118+
119+ this . $alphalistButton . addClass ( 'redactor-act' ) ;
120+ this . alphalist . updateButtonState . call ( this ) ;
121+ }
122+ } ;
123+ } ;
124+ } ) ( jQuery ) ;
0 commit comments