Home 
 
 
 
 
 Blog 
 
 
 
Sytling Image

Archive for February, 2010

WordPress – MCEComments and Threaded Comments Fix

February 23rd, 2010 - Be the First Comment!


Edit: After further investigation it looks like the only real problem was problem number 2 (hidden field and iframe named the same). All you have to do is implement the fix for problem 2 discussed below and it should work fine.
Problem number 2 was more of a conflict and wasn’t really anyones fault, but was rather troublesome to fix. We thought it would be easier to change the TinyMCEComments plugin then modify the install of WordPress. We also had already modified the plugin to remove/insert TinyMCE boxes correctly so we might as well keep working with that. To fix the problem before TinyMCE is initialized we change the hidden field with id=”comment_parent” so it is id=”comment_parent_hf”. This doesn’t effect WordPress comment submission because form submissions use the name field server side to identify an attribute.

Change in tinyMCEComments.php

Before (start line 295):

if (subBtn != null) {
	subBtn.onclick=function() {
		var inst = tinyMCE.getInstanceById("comment");
		document.getElementById("comment").value = inst.getContent();
		document.getElementById("commentform").submit();
		return false;
	}
}
tinyMCEPreInit.go();
tinyMCE.init(tinyMCEPreInit.mceInit);

After:

var subBtn = document.getElementById("submit");
	if (subBtn != null) {
	subBtn.onclick=function() {
		var inst = tinyMCE.getInstanceById("comment");
		document.getElementById("comment").value = inst.getContent();
		document.getElementById("commentform").submit();
		return false;
	}
}
tinyMCEPreInit.go();
tinymce.dom.Event.add(window, "load", function() {
	// Change the hidden fields id
	var pId = document.getElementById("comment_parent");
	if(pId) pId.id = "comment_parent_hf";
	tinyMCE.init(tinyMCEPreInit.mceInit);
});

The last thing that was changed in the script was a hard coded name of the comment_parent hidden filed inside comment-reply.dev.js in the TinyMCEComments plugin folder. Change parent = t.I(‘comment_parent’) to parent = t.I(‘comment_parent_hf’) to reflect the change we make on the load of TinyMCEComments:

Before (line 3):

var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID');

After:

var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent_hf'), post = t.I('comment_post_ID');

Now it works! Woohoo! I’m going to try to contact the other and have these changes included in the next release so everyone can enjoy threaded comments with TinyMCEComments. Thanks for reading and hopefully someone will find this helpful.


Today at work I was trying to convert our blog theme to a wider stance for an upcoming template switch, I thought this would be a very simple task. Little did I know the long day that would ensue. Our blog system at work used to use a plugin to create threaded functionality for posts. My boss decided that since WordPress now supports threaded comments we would switch over to that. I thought this would be a great idea (the less code, the less complex, the better). I began to convert our comments template to the new version required for Threaded Comments. After this was done I tried it out. It wouldn’t work with TinyMCEComments (plugin) enabled, but worked fine with it off. When TinyMCEComments was enabled it created two TinyMCE textareas whenever you tried to do a threaded reply. This seemed very odd to me but I had a feeling it had to do with moving the comment box below the comment that you’re replying too. After quite awhile and a little bit of help from my boss I was able to figure out all the issues and get TinyMCEComments working with the new threaded comments feature. There were two main issues:

  1. Double insertion of TinyMCE editors.
  2. TinyMCE has a bug where if create an editor with an id, then destroy that editor, and then try to create a new editor with that same id it will not work.
  3. TinyMCE names its iframe the id of the textarea concatenated with “_parent”. So when you add TinyMCE functionality to a textare with id=”comments” you get an iframe with id=”comments_parent”. Unfortunately, WordPress uses “comment_parent” as the id for a hidden field used to transfer information about where your new comment should be added in the threading (or to the base comments). The hidden fields are output by the function comment_id_fields(). Also WordPress says “the comment textarea, it MUST have an id=”comment”. The JavaScript expects it for focus purposes. If you used anything else, change it. Note that because of this, no other element on your page can have the “comment” ID”.

Before I explain the fixes let me first say that TinyMCEComments overrides the addComment.moveForm function that is used by WordPress to move the comment box under the comment you want to reply too. It has the same code except that it has the added functionality for enabling TinyMCE on the comment box. Unfortunately it doesn’t work out of the box with WordPress’ thread comments.

Problem 1 was an issue with the TinyMCEComments plugin and TinyMCE. Using the remove method doesn’t remove the the TinyMCE’s iframe from the DOM. This was fixed with Problem 2. The removing and adding method being used doesn’t work correctly for Problem number 2 is a TinyMCE problem and we can’t really do to much about it other then implementing a name change when we reinsert the comment box. So we modified the TinyMCEComments plugin to rename the text area on reinsertion.

Before

Remove TinyMCE Comment box:

try {
   tinyMCE.triggerSave();
   tinyMCE.execCommand('mceFocus', false,'comment');
   tinyMCE.execCommand('mceRemoveControl', false,'comment');
} catch(el) { console.error(el); }

Add TinyMCE Comment box code:

addTiny : function() {
   try { tinyMCE.execCommand('mceAddControl', false, 'comment');
   } catch (e) { console.error(e); }
}

After:

Remove TinyMCE Comment box:

// **** Remove the TinyMCE Editor
var editor = tinymce.EditorManager.activeEditor;
var editorId = editor.getParam('elements');
editor.remove();
tinymce.DOM.remove(editor.getContainer());
editor.destroy();
tinymce.EditorManager.activeEditor = null;

Add TinyMCE Comment box code:

var commentElement = document.getElementById(editorId);
if (editorId == "comment")
	editorId = "comment_1";
else {
	var count = parseInt(editorId.replace(/comment_/, ""), 10) + 1;
	editorId = "comment_" + count;
}
commentElement.id = editorId;
tinyMCEPreInit.mceInit.elements = editorId;
tinyMCE.init(tinyMCEPreInit.mceInit);

Problem number 2 was more of a conflict and wasn’t really anyones fault, but was rather troublesome to fix. We thought it would be easier to change the TinyMCEComments plugin then modify the install of WordPress. We also had already modified the plugin to remove/insert TinyMCE boxes correctly so we might as well keep working with that. To fix the problem before TinyMCE is initialized we change the hidden field with id=”comment_parent” so it is id=”comment_parent_hf”. This doesn’t effect WordPress comment submission because form submissions use the name field server side to identify an attribute.

Change in tinyMCEComments.php

Before (start line 295):

if (subBtn != null) {
	subBtn.onclick=function() {
		var inst = tinyMCE.getInstanceById("comment");
		document.getElementById("comment").value = inst.getContent();
		document.getElementById("commentform").submit();
		return false;
	}
}
tinyMCEPreInit.go();
tinyMCE.init(tinyMCEPreInit.mceInit);

After:

var subBtn = document.getElementById("submit");
	if (subBtn != null) {
	subBtn.onclick=function() {
		var inst = tinyMCE.getInstanceById("comment");
		document.getElementById("comment").value = inst.getContent();
		document.getElementById("commentform").submit();
		return false;
	}
}
tinyMCEPreInit.go();
tinymce.dom.Event.add(window, "load", function() {
	// Change the hidden fields id
	var pId = document.getElementById("comment_parent");
	if(pId) pId.id = "comment_parent_hf";
	tinyMCE.init(tinyMCEPreInit.mceInit);
});

The last thing that was changed in the script was a hard coded name of the comment_parent hidden filed inside comment-reply.dev.js in the TinyMCEComments plugin folder. Change parent = t.I(‘comment_parent’) to parent = t.I(‘comment_parent_hf’) to reflect the change we make on the load of TinyMCEComments:

Before (line 3):

var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID');

After:

var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent_hf'), post = t.I('comment_post_ID');

Now it works! Woohoo! I’m going to try to contact the other and have these changes included in the next release so everyone can enjoy threaded comments with TinyMCEComments. Thanks for reading and hopefully someone will find this helpful.

Completed Files in Zip: DevTinyMCEComments

Physics – Climber Model Update

February 18th, 2010 - 3 Comments

Worked on the climber today in class some more. Here are the new results.

Pdf Version

WordPress Media Plugin and Very Custom Theme

February 17th, 2010 - Be the First Comment!

Recently at work I have been working on a new website, http://magazine.missouristate.edu. It wasn’t until recently that it was finished and became live. As part of the transfer from development to in the wild the department I work in made a couple of posts on our blog regarding my projects. This one is about the magazine.missouristate.edu website and what it is meant to do. There was a lot of behind the scenes custom code on these pages because they do things in such a non standard way.

The second post is in regards to a plugin and a widget I made. The plugin allows you to insert a few media elements into your post and can provide custom CSS styling for the inserted media. On the magazine.missouristate.edu site some custom styles are applied, but we also implemented this plugin on the main wordpress blogs. They have no custom styling enabled. I have been developing a version for myself to release to the public with a few extra nifty features, but I’m not sure if there is really a demand. I might create it anyway and see where it goes. It can insert YouTube videos with custom height/width (not possible with using just the link in a wordpress post, to my knowledge), Flickr Slideshows (based of sets), mp3 files (via direct url), and mp4 files (via direct url). If anyone is interested in a plugin in that does this kind of stuff, just let me know and I can develop it.

easySlider for jQuery

February 11th, 2010 - 1 Comment

Yesterday my supervisor and I used EasySlider (a jQuery plugin) created by Alen Grakalic to make a Flickr slideshow that uses external controls to control the slideshow. It uses the Flickr feed to return a photoset in JSON to get the photos then a slightly modified version of EasySlider to do this. All that was added was a way to have a callback function on slide changes. The function is used by passing the function to trigger with the rest of options by setting onChangedCallBack to the function name. The rest of the script is unchanged. I am sending the code to author for possible future releases. The page it was used on is not currently public, but later I might update this post with a link to it. This is the page that it was used on: http://www.missouristate.edu/presidentialsearch/missouristate.htm Here is the code and at the end I will link the actual .js file.

/*
 * 	Easy Slider 1.7 - jQuery plugin
 *	written by Alen Grakalic
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

/*
 *	markup example for $("#slider").easySlider();
 *
 * 	<div id="slider">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

    $.fn.easySlider = function(options) {

        // default configuration properties
        var defaults = {
            prevId: 'prevBtn',
            prevText: 'Previous',
            nextId: 'nextBtn',
            nextText: 'Next',
            controlsShow: true,
            controlsBefore: '',
            controlsAfter: '',
            controlsFade: true,
            firstId: 'firstBtn',
            firstText: 'First',
            firstShow: false,
            lastId: 'lastBtn',
            lastText: 'Last',
            lastShow: false,
            vertical: false,
            speed: 800,
            auto: false,
            pause: 2000,
            continuous: false,
            numeric: false,
            numericId: 'controls',
            onChangedCallback: null
        };

        var options = $.extend(defaults, options);

        this.each(function() {
            var obj = $(this);
            var s = $("li", obj).length;
            var w = $("li", obj).width();
            var h = $("li", obj).height();
            var clickable = true;
            obj.width(w);
            obj.height(h);
            obj.css("overflow", "hidden");
            var ts = s - 1;
            var t = 0;
            $("ul", obj).css('width', s * w);

            if (options.continuous) {
                $("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left", "-" + w + "px"));
                $("ul", obj).append($("ul li:nth-child(2)", obj).clone());
                $("ul", obj).css('width', (s + 1) * w);
            };

            if (!options.vertical) $("li", obj).css('float', 'left');

            if (options.controlsShow) {
                var html = options.controlsBefore;
                if (options.numeric) {
                    html += '<ol id="' + options.numericId + '"></ol>';
                } else {
                    if (options.firstShow) html += '<span id="' + options.firstId + '"><a href=\"javascript:void(0);\">' + options.firstText + '</a></span>';
                    html += ' <span id="' + options.prevId + '"><a href=\"javascript:void(0);\">' + options.prevText + '</a></span>';
                    html += ' <span id="' + options.nextId + '"><a href=\"javascript:void(0);\">' + options.nextText + '</a></span>';
                    if (options.lastShow) html += ' <span id="' + options.lastId + '"><a href=\"javascript:void(0);\">' + options.lastText + '</a></span>';
                };

                html += options.controlsAfter;
                $(obj).after(html);
            };

            if (options.numeric) {
                for (var i = 0; i < s; i++) {
                    $(document.createElement("li"))
						.attr('id', options.numericId + (i + 1))
						.html('<a rel=' + i + ' href=\"javascript:void(0);\">' + (i + 1) + '</a>')
						.appendTo($("#" + options.numericId))
						.click(function() {
						    animate($("a", $(this)).attr('rel'), true);
						});
                };
            } else {
                $("a", "#" + options.nextId).click(function() {
                    animate("next", true);
                });
                $("a", "#" + options.prevId).click(function() {
                    animate("prev", true);
                });
                $("a", "#" + options.firstId).click(function() {
                    animate("first", true);
                });
                $("a", "#" + options.lastId).click(function() {
                    animate("last", true);
                });
            };

            function setCurrent(i) {
                i = parseInt(i) + 1;
                $("li", "#" + options.numericId).removeClass("current");
                $("li#" + options.numericId + i).addClass("current");
            };

            function showMeta(i) {
                document.getElementById("fss_meta").style.display = "block";
                debugger;
                var a = $("ul", obj);
                $("#fss_meta h3").html($("#fss ul:nth-child(" + i + ") img").attr("alt"));
                $("#fss_meta p").html(options.metaInfo[i - 1]);
            };

            function adjust() {
                if (t > ts) t = 0;
                if (t < 0) t = ts;
                if (!options.vertical) {
                    $("ul", obj).css("margin-left", (t * w * -1));
                } else {
                    $("ul", obj).css("margin-left", (t * h * -1));
                }
                clickable = true;
                if (options.numeric) setCurrent(t);
                if (options.onChangedCallback) options.onChangedCallback(t);
            };

            function animate(dir, clicked) {
                if (clickable) {
                    clickable = false;
                    var ot = t;
                    switch (dir) {
                        case "next":
                            t = (ot >= ts) ? (options.continuous ? t + 1 : ts) : t + 1;
                            break;
                        case "prev":
                            t = (t <= 0) ? (options.continuous ? t - 1 : 0) : t - 1;
                            break;
                        case "first":
                            t = 0;
                            break;
                        case "last":
                            t = ts;
                            break;
                        default:
                            t = dir;
                            break;
                    };
                    var diff = Math.abs(ot - t);
                    var speed = diff * options.speed;
                    if (!options.vertical) {
                        p = (t * w * -1);
                        $("ul", obj).animate(
							{ marginLeft: p },
							{ queue: false, duration: speed, complete: adjust }
						);
                    } else {
                        p = (t * h * -1);
                        $("ul", obj).animate(
							{ marginTop: p },
							{ queue: false, duration: speed, complete: adjust }
						);
                    };

                    if (!options.continuous && options.controlsFade) {
                        if (t == ts) {
                            $("a", "#" + options.nextId).hide();
                            $("a", "#" + options.lastId).hide();
                        } else {
                            $("a", "#" + options.nextId).show();
                            $("a", "#" + options.lastId).show();
                        };
                        if (t == 0) {
                            $("a", "#" + options.prevId).hide();
                            $("a", "#" + options.firstId).hide();
                        } else {
                            $("a", "#" + options.prevId).show();
                            $("a", "#" + options.firstId).show();
                        };
                    };

                    if (clicked) clearTimeout(timeout);
                    if (options.auto && dir == "next" && !clicked) {
                        ;
                        timeout = setTimeout(function() {
                            animate("next", false);
                        }, diff * options.speed + options.pause);
                    };

                };

            };
            // init
            var timeout;
            if (options.auto) {
                ;
                timeout = setTimeout(function() {
                    animate("next", false);
                }, options.pause);
            };

            if (options.numeric) setCurrent(0);
            if (options.onChangedCallback)
                options.onChangedCallback(0);

            if (!options.continuous && options.controlsFade) {
                $("a", "#" + options.prevId).hide();
                $("a", "#" + options.firstId).hide();
            };

        });

    };

})(jQuery);

File: easySlider1.7-Callback

/*
*     Easy Slider 1.7 – jQuery plugin
*    written by Alen Grakalic
*    http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
*
*    Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
*    Dual licensed under the MIT (MIT-LICENSE.txt)
*    and GPL (GPL-LICENSE.txt) licenses.
*
*    Built for jQuery library
*    http://jquery.com
*
*//*
*    markup example for $(“#slider”).easySlider();
*
*     <div id=”slider”>
*        <ul>
*            <li><img src=”images/01.jpg” alt=”" /></li>
*            <li><img src=”images/02.jpg” alt=”" /></li>
*            <li><img src=”images/03.jpg” alt=”" /></li>
*            <li><img src=”images/04.jpg” alt=”" /></li>
*            <li><img src=”images/05.jpg” alt=”" /></li>
*        </ul>
*    </div>
*
*/

(function($) {

$.fn.easySlider = function(options) {

// default configuration properties
var defaults = {
prevId: ‘prevBtn’,
prevText: ‘Previous’,
nextId: ‘nextBtn’,
nextText: ‘Next’,
controlsShow: true,
controlsBefore: ”,
controlsAfter: ”,
controlsFade: true,
firstId: ‘firstBtn’,
firstText: ‘First’,
firstShow: false,
lastId: ‘lastBtn’,
lastText: ‘Last’,
lastShow: false,
vertical: false,
speed: 800,
auto: false,
pause: 2000,
continuous: false,
numeric: false,
numericId: ‘controls’,
onChangedCallback: null
};

var options = $.extend(defaults, options);

this.each(function() {
var obj = $(this);
var s = $(“li”, obj).length;
var w = $(“li”, obj).width();
var h = $(“li”, obj).height();
var clickable = true;
obj.width(w);
obj.height(h);
obj.css(“overflow”, “hidden”);
var ts = s – 1;
var t = 0;
$(“ul”, obj).css(‘width’, s * w);

if (options.continuous) {
$(“ul”, obj).prepend($(“ul li:last-child”, obj).clone().css(“margin-left”, “-” + w + “px”));
$(“ul”, obj).append($(“ul li:nth-child(2)”, obj).clone());
$(“ul”, obj).css(‘width’, (s + 1) * w);
};

if (!options.vertical) $(“li”, obj).css(‘float’, ‘left’);

if (options.controlsShow) {
var html = options.controlsBefore;
if (options.numeric) {
html += ‘<ol id=”‘ + options.numericId + ‘”></ol>’;
} else {
if (options.firstShow) html += ‘<span id=”‘ + options.firstId + ‘”><a href=\”javascript:void(0);\”>’ + options.firstText + ‘</a></span>’;
html += ‘ <span id=”‘ + options.prevId + ‘”><a href=\”javascript:void(0);\”>’ + options.prevText + ‘</a></span>’;
html += ‘ <span id=”‘ + options.nextId + ‘”><a href=\”javascript:void(0);\”>’ + options.nextText + ‘</a></span>’;
if (options.lastShow) html += ‘ <span id=”‘ + options.lastId + ‘”><a href=\”javascript:void(0);\”>’ + options.lastText + ‘</a></span>’;
};

html += options.controlsAfter;
$(obj).after(html);
};

if (options.numeric) {
for (var i = 0; i < s; i++) {
$(document.createElement(“li”))
.attr(‘id’, options.numericId + (i + 1))
.html(‘<a rel=’ + i + ‘ href=\”javascript:void(0);\”>’ + (i + 1) + ‘</a>’)
.appendTo($(“#” + options.numericId))
.click(function() {
animate($(“a”, $(this)).attr(‘rel’), true);
});
};
} else {
$(“a”, “#” + options.nextId).click(function() {
animate(“next”, true);
});
$(“a”, “#” + options.prevId).click(function() {
animate(“prev”, true);
});
$(“a”, “#” + options.firstId).click(function() {
animate(“first”, true);
});
$(“a”, “#” + options.lastId).click(function() {
animate(“last”, true);
});
};

function setCurrent(i) {
i = parseInt(i) + 1;
$(“li”, “#” + options.numericId).removeClass(“current”);
$(“li#” + options.numericId + i).addClass(“current”);
};

function showMeta(i) {
document.getElementById(“fss_meta”).style.display = “block”;
debugger;
var a = $(“ul”, obj);
$(“#fss_meta h3″).html($(“#fss ul:nth-child(” + i + “) img”).attr(“alt”));
$(“#fss_meta p”).html(options.metaInfo[i - 1]);
};

function adjust() {
if (t > ts) t = 0;
if (t < 0) t = ts;
if (!options.vertical) {
$(“ul”, obj).css(“margin-left”, (t * w * -1));
} else {
$(“ul”, obj).css(“margin-left”, (t * h * -1));
}
clickable = true;
if (options.numeric) setCurrent(t);
if (options.onChangedCallback) options.onChangedCallback(t);
};

function animate(dir, clicked) {
if (clickable) {
clickable = false;
var ot = t;
switch (dir) {
case “next”:
t = (ot >= ts) ? (options.continuous ? t + 1 : ts) : t + 1;
break;
case “prev”:
t = (t <= 0) ? (options.continuous ? t – 1 : 0) : t – 1;
break;
case “first”:
t = 0;
break;
case “last”:
t = ts;
break;
default:
t = dir;
break;
};
var diff = Math.abs(ot – t);
var speed = diff * options.speed;
if (!options.vertical) {
p = (t * w * -1);
$(“ul”, obj).animate(
{ marginLeft: p },
{ queue: false, duration: speed, complete: adjust }
);
} else {
p = (t * h * -1);
$(“ul”, obj).animate(
{ marginTop: p },
{ queue: false, duration: speed, complete: adjust }
);
};

if (!options.continuous && options.controlsFade) {
if (t == ts) {
$(“a”, “#” + options.nextId).hide();
$(“a”, “#” + options.lastId).hide();
} else {
$(“a”, “#” + options.nextId).show();
$(“a”, “#” + options.lastId).show();
};
if (t == 0) {
$(“a”, “#” + options.prevId).hide();
$(“a”, “#” + options.firstId).hide();
} else {
$(“a”, “#” + options.prevId).show();
$(“a”, “#” + options.firstId).show();
};
};

if (clicked) clearTimeout(timeout);
if (options.auto && dir == “next” && !clicked) {
;
timeout = setTimeout(function() {
animate(“next”, false);
}, diff * options.speed + options.pause);
};

};

};
// init
var timeout;
if (options.auto) {
;
timeout = setTimeout(function() {
animate(“next”, false);
}, options.pause);
};

if (options.numeric) setCurrent(0);
if (options.onChangedCallback)
options.onChangedCallback(0);

if (!options.continuous && options.controlsFade) {
$(“a”, “#” + options.prevId).hide();
$(“a”, “#” + options.firstId).hide();
};

});

};

})(jQuery);

Physics – Space Elevator

February 10th, 2010 - 1 Comment

Update: http://blog.brokenbytes.info/2010/02/physics-climber-model-update/

For my Physics Minor I have been participating in research related to the space elevator. To figure out all the forces present on the climber as is goes up the tether I created a simple diagram to model it. It will be good for visualization purposes and brainstorming. This is the first rendition so im sure there are some issues with it. Here is the image version and pdf of it.

PDF Version: ForceClimberDiagram

Styling Image