CKeditor : Wrap ul list in a div with class - ckeditor

I wish to customize a new style in ckeditor (under liferay 6.2).
So far, I'm able to create styles like this, in the ckconfig.jsp :
{name: 'Floating style', element: 'div', attributes: {'class': 'floating-list'}}
This is a style I would like to apply to a wrapping div parent to a the desired ul list. It would look like that :
<div class="floating-list">
<ul>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>
</div>
But the problem is that when I apply the style to my list, it of course applies to my ul li contents like this :
<ul>
<li><div class="floating-list">item</div></li>
<li><div class="floating-list">item</div></li>
<li><div class="floating-list">item</div></li>
<li><div class="floating-list">item</div></li>
</ul>
To avoid this behaviour, I wrote a little piece of jquery code :
$('.floating-list').closest('ul').each(function(){
var list = $(this);
var item = list.find('li');
item.each(function(){
$(this).html( $(this).find('.floating-list').html() );
});
list.replaceWith('<div class="floating-list"><ul>'+list.html()+'</ul></div>');
});
This works actually, but this is really dirty.
I'm wondering if there was a way to make it out. Thanks.

can try this:
CKEDITOR.instances.idofyourCKEditor.on('change', function() {
//for apply to all li's of ckeditor
jQuery(".cke_reset").contents().find("li").each(function(){
if(jQuery(this).parent().is("ul")){
jQuery(this).wrap("<div></div>"); //or .wrapInner( "<div class='new'></div>"); if you want do inside
}
});
});
with the event change can update the list when you are creating or editing
with the method contents() of jquery can update the elements inside of iframe .cke_reset, or the id if you can find him.
with the method parent() or children().eq(0) of jquery can get the element for check if is or not wrap
with the method wrap("<div class='chimichanga'></div>") and wrapInner( "<div class='new'></div>") can update the code html fast and easy.

Related

CKEditor automatically nesting double ul

I wrote a plugin for CKEditor 4 that uses widget and dialog. My widget, simplifying a little bit, consists of a div with a nested ul and a number of li's. For some reason, when I switch from WYSIWYG mode to SOURCE mode, the ul is turned into a double nested ul.
I have defined which elements in the widget should be editables and I have defined which elements should be allowedContent for those editables.
My original structure in WYSIWYG mode (after the dialog closes and the widget is created) is like this:
<div class="mycustombox">
<div class="conditions-box">
<div class="conditions-services">
<span class="rwd-line-title">TITLE FOR THE UNORDERED LIST</span>
<ul class="services-list">
<li>an example list item</li>
<li>another example list item</li>
</ul>
</div>
</div>
</div>
I have double checked that this is the actual html by inspecting the source of the page in the Chrome Developer Console. But when I switch to SOURCE mode, the structure then becomes:
<div class="mycustombox">
<div class="conditions-box">
<div class="conditions-services">
<span class="rwd-line-title">TITLE FOR THE UNORDERED LIST</span>
<ul class="services-list">
<ul>
<li>an example list item</li>
<li>another example list item</li>
</ul>
</ul>
</div>
</div>
</div>
The original ul with the class I gave it is there, but there is an extra nested ul wrapping the li elements.
I have defined my allowed widget content in the plugin.js:
allowedContent: 'div(!mycustombox); div(!conditions-box); div(!conditions-services); span(!rwd-line); span(!rwd-line-title); ul(!services-list); li; p; div',
requiredContent: 'div(mycustombox)',
upcast: function( element ) {
return element.name == 'div' && element.hasClass( 'mycustombox' );
},
And I have defined the ul element as an editable like so:
editables: {
priceincludes: {
selector: 'div.conditions-box div.conditions-services ul',
allowedContent: 'li em strong'
},
}
I have also allowed ul's to be editable by the general CKEditor instance as so:
CKEDITOR.dtd.$editable[ 'ul' ] = 1;
Is there some setting in the CKEditor configuration which could be causing this behaviour?
Well I don't know if this is the best solution, but it works.
Tell CKEDitor to stop trying to automatically wrap li elements with a ul tag. For some reason it's treating them as though they weren't already wrapped in a ul tag.
Using this at the beginning of my plugin.js fixes the problem:
delete CKEDITOR.dtd.$listItem['li'];
delete CKEDITOR.dtd.$intermediate['li'];
I got the idea from here:
http://margotskapacs.com/2014/11/ckeditor-stop-altering-elements/
Seems kind of hackish to me, but until I find a better solution I'll just use this.

Scroll to <a> on page load

I have a div with an un-ordered navigation list in it. The div it is in has a set height and the overflow set to auto to give me a scroll bar. What I want is to have the "current" navigation link scrolled to the top of the div on page load. Each list items has the class 'current-menu-item' when current. Here is what the code looks like.
<div class="menu-portfolio-container">
<ul id="menu-portfolio" class="menu">
<li id="menu-item-174" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item menu-item-174 has-image">
<a href="http://dnb.khcreativemedia.com/hinton/">
</li>
Here is the jQuery I tried.
<script>
$(document).ready(function () {
// Handler for .ready() called.
$('html, body').animate({
scrollTop: $('.current-menu-item').offset().top
}, 'slow');
});
</script>
Here's the link to jQuery in the head:
<script src="http://dnb.khcreativemedia.com/wp-includes/js/jquery/jquery.js?ver=1.11.1" type="text/javascript">
Can anyone tell me what I would need to do to get this working? This is a wordpress site using the genesis framework.
Thanks to Tim's help, I've got it working. You can check the js fiddle page to see it in action. jsfiddle.net/KHCreative/2rn7otax
Thanks again for your help Tim.

Is it possible to open CKEditor Insert-Image Component programmatically?

For example, if I have a ready instance of CKEDITOR, and on that same page I have a list of pictures, with this markup:
<div class="galleryPictures">
<a href="#image-id-1" data-id="1" data-img-src="/pictures/image-1.jpg" class="galleryPicture">
<img src="/pictures/image-1.jpg" />
</a>
<a href="#image-id-2" data-id="2" data-img-src="/pictures/image-2.jpg" class="galleryPicture">
<img src="/pictures/image-2.jpg" />
</a>
<a href="#image-id-3" data-id="3" data-img-src="/pictures/image-3.jpg" class="galleryPicture">
<img src="/pictures/image-3.jpg" />
</a>
</div>
And I have following jQuery click handler delegated to these a.galleryPicture items:
$(document).ready(function() {
$('.galleryPictures').on('click', 'a.galleryPicture', function() {
// when this accours I would like to open CKEditor's
// Insert-Image Component, and to get the "url" field filled with
// $(this).data('img-src'); value
});
});
Is it possible to trigger / open Insert-Image programmatically?
Found it. Might be of help to someone else:
With a simple inspection with Firebug I've seen that the <a> which on click triggers the opening of the Insert-Image component contains this function call in it's onclick attribute:
CKEDITOR.tools.callFunction(51,{});
// second argument is {} (an empty js-object), it the original the <a> onclick it was like this:
// CKEDITOR.tools.callFunction(51, this); meaning it was sending the instance of that element
After this call it is possible to do something like this:
CKEDITOR.tools.callFunction(51,{});
// a failsafe if jQuery doesn't find the specified element after the function is executed
setTimeout(function() {
$('#cke_69_textInput').val('someImageUrl');
}, 50);

Need to show / hide a div on clicking on a link

I'd like to do something like this fiddle only this works on buttons instead of links:
http://jsfiddle.net/qNhZj/
I need it to work on links and also i have an intro div which gets hidden after clicking on one of the links.
Can anyone help me out please.
You just have to declare link instead of input.
In the class list, add the id of the div you want to show.
Click a button to make it visible:
<div id="intro-tekst">Intro text here !</div>
<div class="boxes" id="coke">Coke is awesome!</div>
<div class="boxes" id="bubble-tea">Bubble tea is da bomb!</div>
<div class="boxes" id="milk">Milk is healthy!</div>
<br />
<p>
I change my mind:
<ul>
<li>Coke</li>
<li>Bubble Tea</li>
<li>Milk</li>
</ul>
</p>
And bind it like this :
$(document).ready(function() {
var url = window.location.href;
var option = url.match(/option=(.*)/);
if (option !== null) {
$(".link ." . option[1]).trigger('click');
}
$(".link").bind('click', function () {
$('#intro-tekst').hide();
$('.boxes').hide();
$('.link').removeClass('selected');
$(this).removeClass('link');
$('#' + $(this).prop('class')).show();
$(this).addClass('link selected');
});
});
JsFiddle code here : http://jsfiddle.net/Pq5Cv/8/

changing the class name of a div in jquery

When clicked the button I want to get change the name of the class of li under mycontent div. Is there a way to do that? my codes are below.
<div id="mycontent">
<ul id="sortable">
<li id="1" class="myclass">Item 1</li>
<li id="2" class="myclass2">Item 2</li>
<li id="3" class="myclass3">Item 3</li>
<li id="4" class="myclass4">Item 4</li>
</ul>
</div>
$(document).ready(function()
{
$("#submit_prog").click(
function()
{
$.ajax(
{
type: "POST",
url: "sort2.php",
});
});
});
I'm pretty sure the jQuery would be something like:
$("#mycontent li.myclass2").removeClass("myclass2").addClass("myOtherClass2");
...etc for each li element.
if you wanted to change the class of all of li the elements, use this:
$("#mycontent li").removeClass("myclass myclass2 myclass3 myclass4").addClass("myOtherClass2");
you would add that to a click event on the button (in this example with id myClassChangeButton):
$("#myClassChangeButton").click(function(){
$("#mycontent li.myclass").removeClass("myclass").addClass("myOtherClass");
$("#mycontent li.myclass2").removeClass("myclass2").addClass("myOtherClass2");
$("#mycontent li.myclass3").removeClass("myclass3").addClass("myOtherClass3");
$("#mycontent li.myclass4").removeClass("myclass4").addClass("myOtherClass4");
});
Use this to change the class myclass2 from a li element in the div with the id mycontent:
$('#button').click(function(){
$('#mycontent li.myclass2').removeClass().addClass('newClass');
});
Edit:
Code Jockey mentioned that this will remove all classes from the li element with .myclass2
That's right, so for future readers, this will only remove .myclass2:
$('#mycontent li.myclass2').removeClass('myclass2').addClass('newClass');
Yes. Try this:
$('.button').click(function(){
$('li#4') .removeClass();
$('li#4').addClass('your_class')
});
Something along this lines should work

Resources