<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>JQuery on Abdullah Diab&#39;s Blog</title>
    <link>https://mpcabd.xyz/tags/jquery/</link>
    <description>Recent content in JQuery on Abdullah Diab&#39;s Blog</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 15 Feb 2010 15:15:48 +0000</lastBuildDate>
    <atom:link href="https://mpcabd.xyz/tags/jquery/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Simple AJAX Comment Preview in Drupal 6</title>
      <link>https://mpcabd.xyz/simple-ajax-comment-preview-in-drupal-6/</link>
      <pubDate>Mon, 15 Feb 2010 15:15:48 +0000</pubDate>
      <guid>https://mpcabd.xyz/simple-ajax-comment-preview-in-drupal-6/</guid>
      <description>&lt;p&gt;As I said in my previous post, I’m working on upgrading &lt;a href=&#34;http://www.csc-sy.net&#34;&gt;Computer Science in Syria&lt;/a&gt; website to Drupal 6.&lt;/p&gt;
&lt;p&gt;I wanted also to have AJAX comment preview in nodes, but I wanted it to be simple without lots of JavaScript loaded. So as usual I got pissed off and decided to create my own code to do it. 8)&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>As I said in my previous post, I’m working on upgrading <a href="http://www.csc-sy.net">Computer Science in Syria</a> website to Drupal 6.</p>
<p>I wanted also to have AJAX comment preview in nodes, but I wanted it to be simple without lots of JavaScript loaded. So as usual I got pissed off and decided to create my own code to do it. 8)</p>
<p>I used <a href="http://www.ericmmartin.com/projects/simplemodal/">Simple Modal</a> by Eric Martin to display the preview in a model instead of a new page.</p>
<p>The code is easy too like the previous post’s code, I edited also the drupal.js file and added the following code:</p>
<pre class="lang:javascript decode:true">if (location.pathname.match('^' + Drupal.settings.basePath + 'node/\\d')) {
		$('#edit-preview').click(function () {
			$.modal('&lt;div&gt;&lt;h1&gt;Preview&lt;/h1&gt;&lt;div id="previewContent"&gt;&lt;img style="width: 126px; height: 22px;" class="previewing" src="' + Drupal.settings.basePath + 'misc/previewing.gif" alt="Loading ..." /&gt;&lt;/div&gt;&lt;/div&gt;');
			var comment = $('#edit-comment').val();
			if ($('#wysiwyg-toggle-edit-comment').text() == Drupal.settings.wysiwyg.disable) {
				$('#wysiwyg-toggle-edit-comment').click();
				comment = $('#edit-comment').val();
				$('#wysiwyg-toggle-edit-comment').click();
			}
			$.post(
				$('form#comment-form').attr('action'),
				{
					'comment' : comment,
					'form_build_id' : $('form#comment-form input[name="form_build_id"]').val(),
					'form_id' : 'comment_form',
					'form_token' : $('form#comment-form input[name="form_token"]').val(),
					'op' : $('form#comment-form input#edit-preview').val(),
				},
				function (html) {
					$('div#previewContent img').remove();
					$('div#previewContent').append($(html).children().find('div.preview div.content').html());
					$('form#comment-form input[name="form_token"]').val($(html).children().find('form#comment-form input[name="form_token"]').val());
					$('form#comment-form input[name="form_build_id"]').val($(html).children().find('form#comment-form input[name="form_build_id"]').val());
				}
			);
			return false;
		});
	}
</pre>
<p>The process is too simple:</p>
<ol>
<li>@line 01: I check to see if the current page is a node page, I just want to make the preview code working on node page.</li>
<li>@line 03: I show the user a modal with a loading image <img alt="Loading &hellip;" loading="lazy" src="http://www.csc-sy.net/misc/previewing.gif"></li>
<li>@lines 04~09: I check the editor, if it is enabled I disable it to get the HTML from the comment. If it is not enabled I get the HTML directly without disabling it.</li>
<li>@lines 10~18: I gather the data of the form and post them.</li>
<li>@line 20: I remove the loading image.</li>
<li>@line 21: I append the HTML data from the response to the modal content.</li>
<li>@lines 22~23: I set the form hidden values (form_build_id and form_token) to the values from the response so I don’t get a Validation Error.</li>
<li>@line 26: I return false so the browser doesn’t post the data again to the new page.</li>
</ol>
<p>Here is an example:</p>
<p><a href="http://mpcabd.xyz/wp-content/uploads/2010/02/1.png" rel="lightbox[207]" title="1"><img src="http://mpcabd.xyz/wp-content/uploads/2010/02/1-300x138.png" alt="" title="1" width="300" height="138" class="alignnone size-medium wp-image-211" srcset="https://mpcabd.xyz/wp-content/uploads/2010/02/1-300x138.png 300w, https://mpcabd.xyz/wp-content/uploads/2010/02/1.png 668w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p><a href="http://mpcabd.xyz/wp-content/uploads/2010/02/2.png" rel="lightbox[207]" title="2"><img src="http://mpcabd.xyz/wp-content/uploads/2010/02/2-300x138.png" alt="" title="2" width="300" height="138" class="alignnone size-medium wp-image-211" /></a></p>
<p>Simple 😉</p>]]></content:encoded>
    </item>
    <item>
      <title>Simple AJAX Quote in Drupal 6</title>
      <link>https://mpcabd.xyz/simple-ajax-quote-in-drupal-6/</link>
      <pubDate>Fri, 12 Feb 2010 23:02:34 +0000</pubDate>
      <guid>https://mpcabd.xyz/simple-ajax-quote-in-drupal-6/</guid>
      <description>&lt;p&gt;I’ve been upgrading the forum of &lt;a href=&#34;http://www.csc-sy.net/&#34; target=&#34;_blank&#34;&gt;Computer Science in Syria&lt;/a&gt; for a week now.&lt;/p&gt;
&lt;p&gt;The forum uses Drupal, and it ran a beta version of Drupal 4, and I upgraded it to the latest Drupal version now.&lt;/p&gt;
&lt;p&gt;I wanted to have AJAX quoting in the forum, but I wanted it to be simple without lots of JavaScript loaded. So as usual I got pissed off and decided to create my own code to do it. 8)&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>I’ve been upgrading the forum of <a href="http://www.csc-sy.net/" target="_blank">Computer Science in Syria</a> for a week now.</p>
<p>The forum uses Drupal, and it ran a beta version of Drupal 4, and I upgraded it to the latest Drupal version now.</p>
<p>I wanted to have AJAX quoting in the forum, but I wanted it to be simple without lots of JavaScript loaded. So as usual I got pissed off and decided to create my own code to do it. 8)</p>
<p>The code was very simple, knowing that the Quote link will get you to a page where the textarea has the quoted text, and knowing that Drupal 6 has <a href="http://jquery.com/">jQuery</a> loaded in it 🙂</p>
<p>I added the following code to /misc/drupal.js file:</p>
<pre class="lang:javascript decode:true">$('a.quote, li.quote a').click(function () {
	$(this).after($('&nbsp;&lt;img style="width: 16px; height: 16px;" class="quoting" src="/misc/quoting.gif" alt="loading" /&gt;'));
	$.ajax({
		url: $(this).attr('href'),
		cache: false,
		success: function(html){
			$('img.quoting').remove();
			var changed = false;
			if ($('#wysiwyg-toggle-edit-comment').text() == Drupal.settings.wysiwyg.disable) {
				$('#wysiwyg-toggle-edit-comment').click();
				changed = true;
			}
			$('#edit-comment').val($('#edit-comment').val() + ($('#edit-comment').val() == '' ? '' : '\n') + $(html).children().find('#edit-comment').val());
			var $target = $('#edit-comment');
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
					var targetOffset = $target.offset().top;
					$('html,body').animate({scrollTop: targetOffset}, 1000);
			}
			if (changed)
				$('#wysiwyg-toggle-edit-comment').click();
			$('#edit-comment').scrollTop($('#edit-comment')[0].scrollHeight);
		}
	});
	return false;
});
</pre>
<p>You can see that I used this image to be displayed while loading the quote into the textarea  <img alt="Quoting" loading="lazy" src="http://www.csc-sy.net/misc/quoting.gif">because it matches the colors of the forum style.</p>
<p>The process is too simple:</p>
<ol>
<li>@line 02: Display a loading image.</li>
<li>@line 03: Request the quoting page which is located in the href attribute of the quote link.</li>
<li>@line 07: Remove the image.</li>
<li>@lines 08 ~ 12: Checking if the HTML editor is enabled, and disabling it so I can assign the value directly to the textarea.</li>
<li>@line 13: Assigning the value of the textarea in the quote page to the textarea in this page.</li>
<li>@lines 14 ~ 19: Scrolling the page’s vertical scroll to reach the textarea.</li>
<li>@lines 20 ~ 21: Re-enabling the editor if I disabled it.</li>
<li>@line 22: Scrolling the textarea’s vertical scroll to reach the end of the text.</li>
<li>@line 25: Returning false to the browser so it doesn’t redirect the page to the quoting page.</li>
</ol>
<p>You see? it is as simple as this 🙂</p>
<p>I’ll soon post some other modifications that made the forum more simple 🙂</p>]]></content:encoded>
    </item>
  </channel>
</rss>
