Archive | Frontend RSS feed for this section

CSS Cheat Sheet

CSS Selector

  1. *  //universal selector
  2. #container *  //everything under #container element
  3. X  //all elements according to their type like ul, a, li, etc
  4. X:visited, X:link  //pseudo-class targeting – eg. input[type=radio]:checked
  5. #X : id selector    //target single element
  6. .X : class selector //target multiple elements
  7. X Y: descendant selector //select only a set of elements under X
  8. X > Y  //only direct children Y of X
  9. X + Y //first adjacent sibling selector: select only the Y that is preceded by X (eg. ul + p)
  10. X ~ Y //all sibling selector
  11. X[Y]  //attribute selector (select X if it has Y as attribute)
  12. X[Y="Z"] //conditional attribute selector : exact
  13. X[Y*="Z"] //conditional attribute selector: contains
  14. X[Y^="Z"] //conditional attribute selector: begins with
  15. X[Y$="Z"] //conditional attribute selector: end with
  16. X[Y~="Z"] //target an attribute which has a spaced-separated list of values
  17. X:not(selector)

Use those tag to help you generated content around the selected elements

  1. X:after
  2. X:before
  3. X:hover (div:hover, a:hover)

Use pseudo-elements

  1. p::first-line
  2. p::first-letter

Use hierarchical notation

  1. X:nth-child(n)   -  eg. li:nth-child(100)
  2. X:nth-last-child(n)
  3. X:nth-of-type(n)
  4. X:nth-of-last-of-type(n)
  5. X:first-child
  6. X:last-child
  7. X:only-child
  8. X:first-of-type (eg. ul:first-of-type)
  9. X:only-of-type //no sibling from its parent container

Absolute positioning inside relative positioning

Without a relative positioned direct parent, the absolutely positioned children elements are positioning themselves in relation to the body element instead of their direct parent.

Reference: Absolute positing inside relative positioning

CSS positioning

The Box Model

To understand positioning in CSS you must first understand the box model. For display purposes, every element in a document is considered to be a rectangular box which has a content area surrounded by padding, a border and margins.

There are two basic types of boxes, block and inline.

  • inline: follow the flow without add new line.
  • block: always starts an new line before and after. (override via display: inline)

Default inline elements

  • <span>, <a>, <img>, <input>

Default block element

  • <div>, <p>, <ul>, <li>, <table>, <pre>, <blockquote>, <form>

Rules

  1. Block elements may contain both inline elements and other block elements, but inline elements may contain only other inline elements.
  2. CSS features a property called display, which allows you to change a block element into an inline one and vice versa.

 

Float

Float

  • A common problem with float-based layouts is that the floats’ container doesn’t want to stretch up to accomodate the floats. To do that, you need:

div.container {
  border: 1px solid #000000;
<strong> overflow: hidden; width: 100%;</strong>
}

div.left {
  width: 45%;
  float: left;
}

div.right {
  width: 45%;
  float: right;
}

 

Leave a comment Continue Reading →

Hacking CSS

Recently, I have built some of the websites from church and friends via Wordpress, a popular blogging/ CMS system. Being very impressed by the quality of the themes and rich plug-ins, I decide to take a step further to learn about the Wordpress programming model. I can do that via writing my simple Hello world plugin. However, it doesn't allow me to grasp the full power of Wordpress framework. So, I decide to study about a sophisticated Wordpress theme and find out how it integrated with Wordpress and leveraged its plug-ins. To do that, I need to ramp up CSS, PHP in few days. This article is the quick note that summarized what I have learnt about CSS. Hope it is helpful to you as well.

Class and ID Selector

/* The difference between an ID and a class is that an ID can be used to identify
one element, whereas a class can be used to identify more than one. */

/* class selector */
.intro

/* id selector*/
#top

/* built-in html element (no need to put symbol) */
p

/* locate specific element – paragraph with class=jam
   (for example <p class=jam>xxx</p>) */
p.jam {xxxx} 

/* apply grouping */
h2, .thisClass, .thatClass {…}

/* nested selector */
#top{…}
#top h1{…}

/* state of an element (ex. link) */
a.snowman:link
a.snowman:visited
a.snowman:active
a.snowman:hover

/* shorthand : margin, padding, border-width where th
p {
    border-top-width: 1px;
    border-right-width: 5px;
    border-bottom-width: 10px;
    border-left-width: 20px;
}

//can be
p {
    border-width: 1px 5px 10px 20px;
}

//border-width, border-color and border-style shorthand
p {
    border: 1px red solid;
}

margin: 1em 10em; // top/bottom = 1em, left/right = 10em

//background-color, background-image, background-repeat, background-position
body {
    background: white url(http://www.htmldog.com/images/bg.gif) no-repeat top right;
}

 

Box Model

All html tags/ elements in html can be think of within its own box/ container. And it can be aligned on the screen in accordance with the CSS box model. Here is the box model in diagram:

I keep forgetting where is margin and padding. So, I use the word "MBP" to help me out where M=margin, B=border, P=padding. ;)

At the first glance, it is all about spacing between an element and its neighbors. However, when I see how creative people leverage negative margin. I think it is good to write about it. For margin like "margin: 0px -30px 0px 0px;",

 

Display Flow

Once you understand how the spacing of an element, you need to under the display flow. Block element (div, p, h1) will display in an newline while Inline element (span, a, img) will display without open up an newline. You can take an element out from its flow via explicitly setting its display properties: inline, block and none. "none" here means not showing it at all. The difference between "display:none" and "visibility:hidden" is "display:none" will take the element out from the flow, so it will not leave an empty space on screen. For example when you want to print an article, you want to just print its content. So, you will do:

#navigation, #seeAlso, #comments, #standards {
    display: none;
}

To take an element out from the display flow but still showing it, there are 2 common ways:

  1. Floating
  2. Positioning

I will cover these in the layout section.

Layout

margin: -30px 0 15px 0; (clockwise: top, right, bottom, left)
Imagine margin is like a photo white border that make your photo bigger in size and it is often enforced.
So, if a photo has margin inside a div like "right 50" and it is
floating right as well. Because external container doesn't moved by internal element, margin-right 50
will push the photo leftward by 50px instead. And -ve margin here will do the opposite.

When static element (ie. no float) is given a -ve margin on top, left, it pulls the element in that specified direction.
But for bottom and left, it doesn't move but move the content surrounding towards it (ie. overlap with it).

Possibility:
(1) use list to build a table
(2) Overlap to add emphasis
(3) Smashing 3D effect for Bevel text
(4) Layout – Flexible Document Structure

Using negative margins with floats sometimes pisses off older browsers. Some symptoms include:

    * Making links unclickable
    * Text becomes difficult to select
    * Tabbing any links disappears when you lose focus

Solution: Just add position:relative and it works!

Layout

Positioning

position has 4 options: absolute, relative, static and fixed

  • absolute will take the element out from the flow and use top, left, buttom and left coordinate to locate it
  • fixed did the same as absolute but it will not move even u scroll the browser.

Floating

floating an element will shift it to the right or left of a line, with surrounding content flowing around it. If you don't want the next element to wrap around the floating object, you can apply the clear property like: clear:left, clear:right and clear:both

@ Rules

/* attach a set of css to another css file */
@import url(xyz.css); 

/* apply its content to a specified media (ex. all, aural, handheld, print,
   projection, screen): IE just takes all, screen, print */
@media print{
    body {xxxx}
    …
}

/* embedded external font (not all browser supports it) */
@font-face {
    font-family: somerandomfontname;
    src: url(somefont.eot);
    font-weight: bold;
}

p {
    font-family: somerandomfontname;
    font-weight: bold;
}
 

Reference

ref: http://www.smashingmagazine.com/2009/07/27/the-definitive-guide-to-using-negative-margins/
ref: http://www.htmldog.com/guides/cssbeginner/
ref: http://www.htmldog.com/reference/cssproperties/
http://devzone.zend.com/article/627

Leave a comment Continue Reading →

Wiring up Flex, Mate, BlazeDS, Spring, Hibernate and MySQL with Maven 2 – Part 1

Introduction

This article is written on top of the great work that Sébastien Arbogast has done. He has written 3 articles that showed you how to wire up Flex, BlazeDS, Spring, Hibernate and MySQL with Maven as build process. I have included his articles below as your reference.

  1. The Flex, Spring, and BlazeDS full stack – Part 1: Creating a Flex module
  2. The Flex, Spring and BlazeDS full stack – Part 2: Writing the to-do list server
  3. The Flex, Spring and BlazeDS full stack – Part 3: Putting the application together

I have found Sebastien’s work as a good foundation for my own project. To contribute back to the community, I will write a series of articles to show you how can customize and extend the todolist sample.

What is in the Part 1 of the series…

  1. Enhancements on the Maven build process
    • Leverage RSL to factor our the framework swc, so the size of the application swf will be reduced. Apart from that, I also take advantage of Flash Player Cache that is available after version 9 update 3 to cache the framework libraries.
    • Clean up the Flex and BlazeDS dependencies in POM as the latest version of the sdk is available and the BlazeDS dependencies are officially available.
    • Include some common reports for maven site generation
    • Embed Jetty web server in the build process for quick deployment and testing
  2. Document how to get the sample up on Eclipse for development
  3. Use Mate as Flex framework
    • Restructure ToDoList sample to leverage Mate framework
    • Factor out Mate as RSL and integrate it with Maven build process via Flex-mojo plugin.

What are in the coming articles…

  1. In part 2 of this series, I will show you how to use flex-mojo to build a modular Flex application.
  2. In part 3 of this series, I will show you how to test your flex app via FlexUnit (Unit test) and FlexMonkey (Functional test)
  3. In part 4 or this series, I will work on server side. I am planning to add monitoring, caching and security to the server side.

Review “ToDoList” sample

Before I start my journey, let me highlight what Sebastien has done first:

  1. Sebastien’s sample demonstrates how to use Maven as a build process. There are 3 parts or subprojects in his sample. They are:
    • todolist-config (configuration files shared by other subprojects)
    • todolist-ria (Flex frontend)
    • todolist-web (Server side that supports the Frontend)
  2. All these subprojects are considered as modules of the main project (root POM). Finally, they are combined together into war artifact and ready to deploy to Tomcat or other J2EE webapp server.
  3. Flex frontend and backend communicate through a binary RPC protocol – AMF. AMF is considered to be the simplest and fastest remoting approach available in Flex. Recently, Adobe has released BlazeDS as an open source implementation of AMF spec. In this sample, BlazeDS is used. To use BlazeDS, there are few things you need to do:
    • Externalize your POJO service via BlazeDS. This sample shows you how to integrate BlazeDS with Spring
    • Make BlazeDS endpoints availabe to the Net via Servlet.
    • Have frontend and backend shared the same BlazeDS configuration files.
  4. In this sample, you can also find out how to use flex-mojo maven plugin to compile the Flex frontend code into swf. Apart from flex-mojo plugin, there are other two good plugins worth to mention:
    • maven-assembly-plugin - can be used to bundle all the files under a directory into a zip file. It is used by todolist-config to bundle all the configuration files (service-config.xml and remoting-config.xml) into a zip during the package phase.
    • maven-dependency-plugincan be used to unpack the zip file and move to the place you want. It is used by todolist-web to unpack the config zip during the generate-resources phase.

Enhancements on maven POM

I have modified the sample’s maven pom as follows:

  • Link to new repository “Sonatype Forge” in the root POM. So, I can use the new version of flex-mojo and simplify the todolist-ria adobe framework dependencies. Apart from that, I also take away the private repository from Sebastein because BlazeDS libraries are available in official maven repository (Note: The BlazeDS libraries available in official maven repo are in version 3.0 instead of 3.0.0.544. So, you need to modify the webapp pom correspondingly).

    <repositories>
        <repository>
            <id>flex-mojos-repository</id>
            <url>http://svn.sonatype.org/flexmojos/repository/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>flex-mojos-repository</id>
            <url>http://svn.sonatype.org/flexmojos/repository/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

  • Because I link to Sonatype repository, I can have my todolist-ria depends on one flex-framework pom dependency instead of all the swc dependencies. Note that the pom dependency is a way to factor out all the adobe swc dependencies that makes your pom easier to maintain.

        <dependency>
            <groupId>com.adobe.flex.framework</groupId>
            <artifactId>flex-framework</artifactId>
            <version>3.1.0.2710</version>
            <type>pom</type>
        </dependency>

  • I include mysql driver as dependency in my webapp pom. I think it is cleaner to bundle it in war. I have also added jetty plugin in the POM so you have a web server embedded in the build process. With this, you can run this sample application right after you check it out from svn (assume you have maven 2 installed). To start jetty, you can issue the following maven command under your webapp project.

project_root> mvn clean install
project_root/jp-web> mvn jetty:run-war

  • I have included some reports that will be shown after site generation. You may not be able to do mvn site-deploy because it is linked to my web hosting site. However, you can modify it for your own sake.

Get the sample up on Eclipse

To develop on Eclipse, you can follow the steps below:

  1. Create Eclipse project file via running the command below at the project root. This will create 2 eclipse projects. One for todolist-ria and one for the webapp. You noticed that I use the -Declipse.downloadSource=true to include the source files of my dependencies in my eclipse project. Therefore, I can get to the source code if needed.

mvn -Declipse.downloadSource=true eclipse:eclipse

  1. Import the projects into Eclipse
  2. Add new variable M2_REPO and set it equals to [home]/.m2/repository
  3. If you have installed Flex Builder plugin to your Eclipse, you can Add Flex Project Nature to the todolist-ria project.
    • Select Application Server Type: J2EE
    • Put check on “Use remote object access service” with LiveCycle Data Service selected.
    • Set up the path. I have my tomcat installed under C:\tools with default 8080 as port. You should make the changes if you installed it differently.
    • Remove the generated main.mxml under the src folder.
    • Set index.mxml under src folder as default Flex application file to run.
    • Use Default Flex SDK in Flex Compiler Configuration instead of Server Flex SDK
    • Right click and select Recreate HTML Template if you see error.
    • After all these, you have configured your Flex application pointing to the webapp server and sharing the BlazeDS configuration files. You can verify in Flex Compiler Configuration’s Additional Compiler Parameters. See whether you see this: -services “C:\tools\tomcat-6.0.16\webapps\jp\WEB-INF\flex\services-config.xml” -locale en_US
    • Move the war to your tomcat’s webapp folder and start it under remote debugging setting. If you are using window, set DEBUG_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8787,suspend=n under your bin/catalina.bat.
    • Start your webapp via bin/startup.bat
    • Put breakpoint under TodoServiceImpl save method and start remote debugger on localhost:8787
    • Right click the index.mxml and Run As Flex Application.
    • Add a new entry and save it on the flex app. :razz: You should see your remote debugger halt at the breakpoint for you to debug.
    • Now you can change your flex code and test it out without leaving your Eclipse. However, if you modify the service in webapp, you need to run “mvn clean install” and deploy the war to the tomcat before your flex code can call your server-side code via AMF.

Use Mate as Framework

If you are not familiar with Mate, click the image below that moves you to a nice presentation.

 

What did I do to restructure the todolist sample to make it Mate app?

  1.  

Download

I have made my work available at: www.solutionhacker.com/wp-content/uploads/todolist-jp-modified.zip

Reference

Below are the references I used for the article:

  1. Flex mojo compiler user guide
  2. Flex mojo dependency scope rules
  3. Flex 3 feature introduction: Flex 3 RSL
  4. Improving Flex application performance using Flash Player Cache
  5. FNA archetype projects 

 

Leave a comment Continue Reading →