Sunday, 26 July 2015

JQUERY UI EVENTS CHEATSHEET

While creating the accessible multi-level dropdown navigation, I had to think a lot about which jQuery events were applicable for desktop vs mobile and mouse vs keyboard. I created a little cheatsheet for myself to reference, which turned out to be quite useful and so I thought I would share it.

THE UI EVENTS

The “UI” events I was looking to were events which invole the user interacting with an element. Here is an overview of the events -

EventDescription
.focusWhen an actionable element, e.g. a, input or button, gains focus
.blurWhen an actionable element loses focus
.focusinWhen an actionable element, or its children, gains focus
.focusoutWhen an actionable element, or its children, loses focus
.clickWhen an element is clicked
.dblclickWhen an element is double-clicked
.mousedownWhen a pointer presses on an element
.mouseupWhen a pointer releases press on an element
.mouseenterWhen a pointer enters the area of an element
.mouseleaveWhen a pointer leaves the area of an element
.mousemoveWhen a pointer moves within the area of an element
.mouseoutWhen a pointer is registered outside the area of an element after previously being registered within it
.mouseoverWhen a pointer enters the area of an element
.hoverA shorthand for both .mouseenter and .mouseleave events
.keydownWhen a key is pressed while on an element
.keyupWhen a key is released while on an element
.keypressWhen a key (not including modifier and non-printing keys such as Esc) is pressed while on an element
RESPONDING USER INTERACTIONS

The way these events are organised in the jQuery documentation can be a bit confusing. For example, the only events listed under keyboard events are .keydown, .keyup, and .keypress. However, other events can also be triggered by the keyboard.

Here is a list of the UI events and which types of user interaction each can be triggered by -

EventMouse?Keyboard?Screen Tap?
.focus✓✓✗
.blur✓✓✗
.focusin✓✓✗
.focusout✓✓✗
.click✓✓✓
.dblclick✓✗✗
.mousedown✓✗✓
.mouseup✓✗✓
.mouseenter✓✗✓
.mouseleave✓✗✓
.mousemove✓✗✓
.mouseout✓✗✓
.mouseover✓✗✓
.keydown✗✓✗
.keyup✗✓✗
.keypress✗✓✗

SCREEN TAP INTERACTIONS

Screen taps are treated very similarly to clicks. Each screen tap is almost like each time the mouse moves on screen. This is why a doubleclick cannot be registered through tapping. However, because of this, we can get some events blocking the registering of other events.
As a test, I chained all the events which can be triggered by a screen tap. When each event is registered, it is appended to a list on screen.
$('.test').on('click mousedown mouseup mouseenter mouseleave mousemove mouseout mouseover',
 function(event) {

  $('#output').append('<li>'+event.type+'</li>');
  return false;

})

When chaining all the events, the click, mousedown, and mouseup events are never registered.
But when the mousemove event was removed, the other, previosuly unregistered, events show up -
KEYBOARD INTERACTIONS When dealing with keyboard interactions, which key pressed becomes pertinent. Some events are triggered by the pressing of a particular key, while, with others, you can access the particular key that was pressed.
EventWhich Key?
.focustab key released while on target element
.blurtab key pressed while on target element
.focusintab key released while on target element or its children
.focusouttab key pressed while on target element or its children
.clickreturn key pressed while on target element
.keydownany key pressed while on target element
.keyupany key released while on target element
.keypressany key (not including modifier keys) pressed while on target element
For the events which respond to any key, we can find out which key was pressed simply -

$('.element').on('keydown keyup keypress', function(e) {
  
  // log the code of the key pressed to the console
  console.log(e.keyCode);

  if ( e.keyCode === 9 ) {
    // tab key pressed
  }
  if ( e.keyCode === 13 ) {
    // return key pressed
  }
})
 

These are some of the nuances that it helps to be aware of when chaining multiple events and targetting different interactions. I hope you find this useful!

Thursday, 19 March 2015

Convert HTML to PDF in PHP with fpdf


HTML to PDF conversion is always a problem for PHP Programmers and all the time they search for suitable solutions so after reviewing this article you will not take more than 10 minutes to configure HTML to PDF, I have used a library fpdf open source and very useful library for developers here is a simple tutorial on how to convert How to Convert HTML to PDF with fpdf.

You have to download fpdf library and  include it in your PHP file below settings and how to show tags, fonts and images in your pdf file. With fpdf library we used HTMLparser library contributed by programmers and all other libraries available here you can download and use as per your requirement.

Index.html

In this file I have created a simple contact form data on submit it show that submitted data on PDF format:

 
<html>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<title>Form</title>
</head>
 
<body>
<h2>How to create Contact Form using Bootstrap example.&nbsp;&nbsp;&nbsp;=> <a href="http://www.phpgang.com/">Home</a> | <a href="http://demo.phpgang.com/">More Demos</a></h2>
<div class="container">
      <form class="contact-us form-horizontal" action="actionpdf.php" method="post">
        <legend>Fill Form and submit to generate PDF</legend>        
        <div class="control-group">
            <label class="control-label">Name</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-user"></i></span>
                    <input type="text" class="input-xlarge" name="name" placeholder="Name">
                </div>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">Email</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-envelope"></i></span>
                    <input type="text" class="input-xlarge" name="email" placeholder="Email">
                </div>
            </div>    
        </div>
        <div class="control-group">
            <label class="control-label">Url</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-globe"></i></span>
                    <input type="text" id="url" class="input-xlarge" name="url" placeholder="http://www.example.com">
                </div>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">Comment</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-pencil"></i></span>
                    <textarea name="comment" class="span4" rows="4" cols="80" placeholder="Comment (Max 200 characters)"></textarea>
                </div>
            </div>
        </div>
        <div class="control-group">
          <div class="controls">
            <button type="submit" class="btn btn-primary">Submit</button>
            <button type="button" class="btn">Cancel</button>
          </div>    
        </div>
      </form>
</div>
</body>
</html>
 

 Actionpdf.php

This file contain PHP code to generate pdf file and show your submitted data on that file.
 
<?php
require('WriteHTML.php');
 
$pdf=new PDF_HTML();
 
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(true, 15);
 
$pdf->AddPage();
$pdf->Image('logo.png',18,13,33);
$pdf->SetFont('Arial','B',14);
$pdf->WriteHTML('<para><h1>PHPGang Programming Blog, Tutorials, jQuery, Ajax, PHP, MySQL and Demos</h1><br>
Website: <u>www.phpgang.com</u></para><br><br>How to Convert HTML to PDF with fpdf example');
 
$pdf->SetFont('Arial','B',7); 
$htmlTable='<TABLE>
<TR>
<TD>Name:</TD>
<TD>'.$_POST['name'].'</TD>
</TR>
<TR>
<TD>Email:</TD>
<TD>'.$_POST['email'].'</TD>
</TR>
<TR>
<TD>URl:</TD>
<TD>'.$_POST['url'].'</TD>
</TR>
<TR>
<TD>Comment:</TD>
<TD>'.$_POST['comment'].'</TD>
</TR>
</TABLE>';
$pdf->WriteHTML2("<br><br><br>$htmlTable");
$pdf->SetFont('Arial','B',6);
$pdf->Output(); 
?>
 
In this file we add page and auto page break true if your content increase single page area then it will automatically add 2nd page and process.

$pdf->Image('logo.png',18,13,33);
$pdf->SetFont('Arial','B',14);
 
These lines used to add a logo and select font size for heading.

$pdf->SetFont('Arial','B',7);
 
Select small font then heading for inner content.

$pdf->WriteHTML2("<br><br><br>$htmlTable");
$pdf->Output();
 
Write HTML to pdf file and output that file on the web browser.


Thursday, 12 March 2015

HTML5 Cheat Sheet




Download .png file (Right click, “Save as”) / Size: 3508 x 2717

Download .pdf file (Right click, “Save as”) / Size: 5,8Mb