99 var button , widget ;
1010
1111 widget = this ;
12+
13+ getEnclosingEmbed = function ( ) {
14+ var node ;
15+
16+ node = widget . options . editable . getSelection ( ) . commonAncestorContainer ;
17+ return $ ( node ) . parents ( 'div[data-embedtype="snippet"]' ) . get ( 0 ) ;
18+ } ;
19+
1220 button = $ ( '<span></span>' ) ;
1321 button . hallobutton ( {
1422 uuid : this . options . uuid ,
1523 editable : this . options . editable ,
1624 label : 'Snippets' ,
1725 icon : 'icon-snippet' ,
18- command : null
26+ queryState : function ( event ) {
27+ var refreshedButton = button . hallobutton ( 'checked' , getEnclosingEmbed ( ) ) ;
28+ if ( $ ( refreshedButton ) . hasClass ( 'ui-state-active' ) ) {
29+ $ ( toolbar ) . find ( 'button' ) . not ( $ ( button ) . children ( ) [ 0 ] ) . removeClass ( 'ui-state-active' ) ;
30+ }
31+ return refreshedButton ;
32+ }
1933 } ) ;
2034 toolbar . append ( button ) ;
2135 return button . on ( "click" , function ( event ) {
22- var insertionPoint , lastSelection ;
23-
24- lastSelection = widget . options . editable . getSelection ( ) ;
25- insertionPoint = $ ( lastSelection . endContainer ) . parentsUntil ( '.richtext' ) . last ( ) ;
26- return ModalWorkflow ( {
27- url : window . embedderChooserUrls . embedderChooser ,
28- responses : {
29- snippetChosen : function ( embedData ) {
30- var elem ;
31-
32- elem = $ ( embedData ) . get ( 0 ) ;
33- lastSelection . insertNode ( elem ) ;
34- // if (elem.getAttribute('contenteditable') === 'false') {
35- // insertRichTextDeleteControl(elem);
36- // }
37- return widget . options . editable . element . trigger ( 'change' ) ;
36+ var enclosingLink , insertionPoint , lastSelection ;
37+
38+ enclosingLink = getEnclosingEmbed ( ) ;
39+ if ( ! enclosingLink ) {
40+
41+ lastSelection = widget . options . editable . getSelection ( ) ;
42+ insertionPoint = $ ( lastSelection . endContainer ) . parentsUntil ( '.richtext' ) . last ( ) ;
43+ return ModalWorkflow ( {
44+ url : window . embedderChooserUrls . embedderChooser ,
45+ responses : {
46+ snippetChosen : function ( embedData ) {
47+ var elem = $ ( embedData ) . get ( 0 ) ;
48+ lastSelection . insertNode ( elem ) ;
49+ if ( elem . getAttribute ( 'contenteditable' ) === 'false' ) {
50+ insertRichTextDeleteControl ( elem ) ;
51+ }
52+
53+ var parentElement = $ ( lastSelection . endContainer ) ;
54+ var eol ;
55+ if ( $ ( parentElement ) . hasClass ( 'richtext' ) ) {
56+ eol = $ ( elem ) . parent ( ) . append ( "<br/>" ) ;
57+ } else {
58+ parentElement = $ ( parentElement ) . parent ( ) ;
59+ eol = $ ( "<br/>" ) . insertAfter ( parentElement ) ;
60+ }
61+ placeCaretAtEnd ( eol [ 0 ] ) ;
62+
63+ return widget . options . editable . element . trigger ( 'change' ) ;
64+ }
3865 }
39- }
40- } ) ;
66+ } ) ;
67+ }
4168 } ) ;
4269 }
4370 } ) ;
4471 } ) ( jQuery ) ;
4572
46- } ) . call ( this ) ;
47-
48-
49-
50-
51-
52-
53- // (function() {
54- // (function($) {
55- // return $.widget("IKS.halloembedder", {
56- // options: {
57- // uuid: '',
58- // editable: null
59- // },
60- // populateToolbar: function(toolbar) {
61- // var button, getEnclosingLink, widget;
62-
63- // widget = this;
64- // getEnclosingLink = function() {
65- // var node;
66-
67- // node = widget.options.editable.getSelection().commonAncestorContainer;
68- // return $(node).parents('a').get(0);
69- // };
70- // button = $('<span></span>');
71- // button.hallobutton({
72- // uuid: this.options.uuid,
73- // editable: this.options.editable,
74- // label: 'Snippets',
75- // icon: 'icon-snippet',
76- // command: null
77- // /*queryState: function(event) {
78- // return button.hallobutton('checked', !!getEnclosingLink());
79- // }*/
80- // });
81- // toolbar.append(button);
82- // return button.on("click", function(event) {
83- // var insertionPoint, lastSelection;
84-
85- // lastSelection = widget.options.editable.getSelection();
86- // insertionPoint = $(lastSelection.endContainer).parentsUntil('.richtext').last();
87-
88- // return ModalWorkflow({
89- // url: window.embedderChooserUrls.embedderChooser,
90- // responses: {
91- // embedChosen: function(embedData) {
92- // var elem;
93- // elem = $(embedData).get(0);
94- // lastSelection.insertNode(elem);
95- // // if (elem.getAttribute('contenteditable') === 'false') {
96- // // insertRichTextDeleteControl(elem);
97- // // }
98- // return widget.options.editable.element.trigger('change');
99- // }
100- // }
101- // });
102- // // enclosingLink = getEnclosingLink();
103- // /*if (enclosingLink) {
104- // $(enclosingLink).replaceWith(enclosingLink.innerHTML);
105- // button.hallobutton('checked', false);
106- // return widget.options.editable.element.trigger('change');
107- // } else {
108- // lastSelection = widget.options.editable.getSelection();
109- // if (lastSelection.collapsed) {
110- // url = window.chooserUrls.pageChooser + '?allow_external_link=true&allow_email_link=true&prompt_for_link_text=true';
111- // } else {
112- // url = window.chooserUrls.pageChooser + '?allow_external_link=true&allow_email_link=true';
113- // }
114- // return ModalWorkflow({
115- // url: url,
116- // responses: {
117- // pageChosen: function(pageData) {
118- // var a;
119-
120- // a = document.createElement('a');
121- // a.setAttribute('href', pageData.url);
122- // if (pageData.id) {
123- // a.setAttribute('data-id', pageData.id);
124- // a.setAttribute('data-linktype', 'page');
125- // }
126- // if ((!lastSelection.collapsed) && lastSelection.canSurroundContents()) {
127- // lastSelection.surroundContents(a);
128- // } else {
129- // a.appendChild(document.createTextNode(pageData.title));
130- // lastSelection.insertNode(a);
131- // }
132- // return widget.options.editable.element.trigger('change');
133- // }
134- // }
135- // });
136- // }*/
137- // });
138- // }
139- // });
140- // })(jQuery);
141-
142- // }).call(this);
73+ } ) . call ( this ) ;
0 commit comments