What is Modernizr
Modernizr is a small piece of JavaScript code library for feature detection that detects the browser support next-generation web technologies in your user’s browsers. Modernizr uses feature detection to allow you to easily fit your website on user’s experiences based with the actual capabilities of their browser.
There are several JavaScript libraries are available now a days, which can help you detect features and browsers. Using these libraries we can detect whether the browser has support for that particular feature or not.
Modernizr gives the ability to detect the new features in the browsers that can render or utilize
Let’s take a example with plain javascript to detect canvas feature.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<script> $(document).ready(function(){ var canvasElement = ""; canvasElement = document.createElement('canvas'); if(canvasElement = = = undefined){ $("#draw").html("Canvas is not supported, use something else."); }else{ // canvas is supported by the browser. $("#draw").html("Canvas is supported"); $("body").append(canvasElement); } }); </script> |
In this code, we tried to create a canvas element and then added it to body.
we could use the Modernizr.canvas property to test if the browser supported canvas or not:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<script> $(document).ready(function(){ var canvasElement = ""; canvasElement = document.createElement('canvas'); if(Modernizr.canvas){ $("#draw").html("Canvas is supported"); $("body").append(canvasElement); }else{ // canvas is supported by the browser. $("#draw").html("Canvas is not supported, use something else."); } }); </script> |
Modernizr creates an object named Modernizr to the document, which contains all of the test results as Boolean properties. We can write a simple script to list all supported and unsupported features:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$(document).ready(function(){ for(var key in Modernizr) { if(typeof Modernizr[key] == 'boolean'){ console.log(key + "::" + Modernizr[key] ); if(Modernizr[key]){ $("#supported- features").append("<b>"+key+"</b><br/>"); }else{ $("#unsupported- features").append("<b>"+key+"</b><br/>"); } } } }); |
Polyfilling with YepNope,requireJS
As a Developer, we can load the JS as per the feature is supported by your browsers. We can use Modernizr.load() method to test if a certain feature is supported or not and load the appropriate JavaScript based on the result. Unfortunately Modernizr.load() is not provided in the modernizr.js file by default and was a part of Modernizr Version 2.8.3. But there are many other third party libraries are available to load resources like YepNope,requireJS,HeadJS etc.
Modernizr.load() is actually yepnope.js (http://yepnopejs.com/). yepnope.js was known as a conditional loader for polyfills, but now its deprecated after version 1.5.
Let’s take a example for this:
1 2 3 4 5 6 7 |
<script> Modernizr.load({ test: Modernizr.touch, yep : 'js/touch.js', nope: 'js/no-touch.js' }); </script> |
In below code we are checking if touch is available in the browser then it will load touch.js, otherwise it will load the no-touch.js file into the browser.
For the newer versions of Modernizr, we can include yepnope.js to use Modernizr.load().
Developers can also wrote the previous example of code as below with RequireJS:
1 2 3 4 5 6 7 8 9 10 11 |
<script> if(Modernizr.touch){ require(['js/touch'], function() { //code to execute when touch..js is loaded }); }else{ require(['js/no-touch'], function() { //code to execute when our polyfill is loaded }); } </script> |
AngularJS Starter Templete With Bootstrap
If you have not tried yet to create angularjs website then definitely this article will help you to create angularjs website with bootstrap starter template. So let’s start.
In this example i am using bootstrap “Justified nav” template.
These are the following AngularJs features what we will covered with creating this template
- MVC(Model view Controller) Architecture
- Calling JSON service with factory
- Dependency Injection
- AngularUI Bootstrap
- Routing
- Directives
- Image Gallery with AngularJS
At the end of the article you understand what is MVC Architecture and how it works in AngularJS? | AngularJS MVC
MVC: It’s a design pattern that breaks the application into three parts Model View Controller. Using a pattern is a reusable solution but in regular javascript we organize the code differently. Another way of looking at patterns are as a templates which can be used in quite a few different scenarios.
Model:
Model is belongs to data it can be dynamic data or static which means you can get it from a database like Mongodb and you can also get data from static JSON file.
Let’s create a static JSON to defining a model. Add following lines in a data.json file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{ "projectTitle" : "Angular", "nav" : [ {"name" : "Home", "url" : "#/", "className" : "active"}, {"name" : "Projects", "url" : "#/projects", "className" : ""}, {"name" : "Services", "url" : "#/services", "className" : ""}, {"name" : "Downloads", "url" : "#/", "className" : "dropdown", "sub" : [ {"name" : "AngularJS", "url" : "#/angularJS"} ] }, {"name" : "Image Gallery", "url" : "#/imagegallery", "className" : ""}, {"name" : "Contact", "url" : "#/contact", "className" : ""} ] } |
View:
View is displaying the data of model. For this we use double curly brackets and it is lot like using JavaScript template library (like mustache.js).
Let’s create a view, following this instruction in index.html:
- First declare the name of the application using ng-app directive.
1<html lang="en" ng-app="angularApp"> - Download and add the angular library in your page version – v1.3.0-rc.5.
1<script src="lib/angular.min.js"></script>
- Defining the controller name using ng-controller directive in your parent element.
1<div class="container" ng-controller="homeCntrl">
Let’s combined the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<div class="container" ng-controller="homeCntrl"> <!-- Static navbar --> <div class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle" ng-init="navCollapsed = true" ng-click="isCollapsed = !isCollapsed"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">{{menu.projectTitle}}</a> </div> <div collapse="isCollapsed" class="navbar-collapse collapse navbar-responsive-collapse"> <ul class="nav navbar-nav"> <li ng-repeat="nav in navbar" class="{{nav.className}}" > <a is-active-nav href="{{nav.url}}" class="{{ nav.sub && 'dropdown-toggle'}}" data-toggle="{{nav.sub && 'dropdown' || ''}}" >{{nav.name}} <b ng-show="nav.sub" class="caret"></b></a> <ul class="dropdown-menu" ng-show="nav.sub"> <li ng-repeat="subItem in nav.sub"><a href="{{subItem.url}}">{{subItem.name}}</a></li> </ul> </li> </ul> </div><!--/.nav-collapse --> </div><!--/.container-fluid --> </div> <div ng-view></div> <!-- Site footer --> <div class="footer"> <p>© ngBootstrap.com 2015</p> </div> </div> |
In this HTML we have use ng directives to rendering the data in view.
ng-repeat: The ngRepeat directive instantiates a template once per item from a collection. Each template instance gets its own scope, where the given loop variable is set to the current collection item, and $index is set to the item index or key.
ng-init: The ngInit directive allows you to evaluate an expression in the current scope.
ng-click: The ngClick directive allows you to specify custom behavior when an element is clicked.
ng-show: The ngShow directive shows or hides the given HTML element based on the expression provided to the ngShow attribute.
Controller
It controls how data is retrieved and displayed to end-user. which means Controller gives you control over model and view.
1 |
var angularApp = angular.module('angularApp', ['ngRoute', 'ui.bootstrap']) |
Creating angular module name which we have defined in ng-app directive in HTML.
We can also defined the dependency as per the module using [] brackets. For our application we need ngRoute for routing to navigate the site. For this you have to download and include
1 |
<script src="lib/angular-router.min.js"></script> |
AngularUI Bootstrap “ui.bootstrap” using the angular bootstrap component., you can download the library from this link https://angular-ui.github.io. and add in html page.
1 |
<script src="modules/ui-bootstrap-tpls-0.12.0.min.js"></script> |
Creating Factory in Angularjs
AngularJS provides you three ways to create a service : factory,service and provider. A factory is a simple function which allows you to add some logic before creating the object. It returns the created object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var angularApp = angular.module('angularApp', ['ngRoute', 'ui.bootstrap']).factory('serviceInfo', function ($http){ 'use strict'; return $http.get('data/json/data.json'); }); angularApp.controller('homeCntrl', function ($scope,serviceInfo, $location){ serviceInfo.success(function(data) { $scope.menu= data; $scope.navbar= data.nav; $scope.navbarRight= data.navRight; }) }); |
Scope is the glue between application controller and the view. During the template linking phase the directives set up $watch expressions on the scope. The $watch allows the directives to be notified of property changes, which allows the directive to render the updated value to the DOM.
If you find AngularJS MVC Article helpful please share it and comment below.
You can get the full code from here: https://github.com/harmeet090/Angularjs
What’s new in Bootstrap 4 alpha
Bootstrap 4 comes with ton of major changes so it’s impossible to cover them all in detail here, so here are some of our most major changes:
- Moved from Less to Sass:Using Libsass bootstrap now compiles Sass faster then ever, and also joined large community of Sass developers.
- Improved grid system: Bootstrap now have added a new grid standard to better target mobile devices.
- Flexbox support: The future of web is now change a boolean variable and recompile your CSS to take advantage of a flexbox-based grid system and components.
- Customization is easy:In Bootstrap 4 Now It’s easy to customize the some CSS style options instead of creating new style of like box-shadows,text-shadows transitions, gradients, and more to a separate stylesheet like v3, bootstrap moved all these options into Sass variables. If we want default transitions on everything or to disable rounded corners? Simply we just need to update a variable in Sass and recompiled it.
- Drop IE8 support and moved to rem and em units: Bootstrap 4 dropped support for IE8 and we can take advantage of the CSS3 without being held back with CSS hacks or fallbacks. Typography Pixels have been swapped for rems and ems where appropriate to make responsive and component sizing even easier. If you need IE8 support, keep using Bootstrap 3.
- Rewrote all our JavaScript plugins: Every plugin has been rewritten in ES6 to take advantage of the newest JavaScript enhancements. They also now come with UMD support, generic teardown methods, option type checking, and tons more.
- Tooltips and Popovers: Bootstrap 4 improved auto-placement of tooltips and popovers using library called Tether.
And many other things like Custom form controls, margin and padding classes, new utility classes, and more have also been included.
Know more about Bootstrap 4 head to the v4 alpha docs!
One more thing…
Bootstrap 4 alpha also launching our latest side project, Official Bootstrap Themes.
wkhtmltopdf – Generating PDF and Images
wkhtmltopdf is amazing open source (LGPLv3) command line tool wkhtmltopdf to render HTML to PDF and various image formats using the Qt WebKit rendering engine. These run entirely “headless” and do not require any service.
There is also a C library, if you’re into that kind of thing.
How to use wkhtmltopdf ?
- Download a precompiled binary or build from source
- Create your HTML document that you want to convert into a PDF (or image)
- Run your HTML document through the tool.
For example, if I really like the treatment LiferayUI has done to their logo today and want to capture it forever as a PDF:wkhtmltopdf https://liferayui.com liferayui.pdf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
Global Options: --collate Collate when printing multiple copies (default) --no-collate Do not collate when printing multiple copies --cookie-jar <path> Read and write cookies from and to the supplied cookie jar file --copies <number> Number of copies to print into the pdf file (default 1) -d, --dpi <dpi> Change the dpi explicitly (this has no effect on X11 based systems) -H, --extended-help Display more extensive help, detailing less common command switches -g, --grayscale PDF will be generated in grayscale -h, --help Display help --htmldoc Output program html help --image-dpi <integer> When embedding images scale them down to this dpi (default 600) --image-quality <integer> When jpeg compressing images use this quality (default 94) --license Output license information and exit -l, --lowquality Generates lower quality pdf/ps. Useful to shrink the result document space --manpage Output program man page -B, --margin-bottom <unitreal> Set the page bottom margin -L, --margin-left <unitreal> Set the page left margin (default 10mm) -R, --margin-right <unitreal> Set the page right margin (default 10mm) -T, --margin-top <unitreal> Set the page top margin -O, --orientation <orientation> Set orientation to Landscape or Portrait (default Portrait) --page-height <unitreal> Page height -s, --page-size <Size> Set paper size to: A4, Letter, etc. (default A4) --page-width <unitreal> Page width --no-pdf-compression Do not use lossless compression on pdf objects -q, --quiet Be less verbose --read-args-from-stdin Read command line arguments from stdin --readme Output program readme --title <text> The title of the generated pdf file (The title of the first document is used if not specified) -V, --version Output version information and exit |
Additional options
That’s amazing, I’ve always wanted to turn LiferayUI homepage into a PDF, but I want a table of contents as well.
There are lot’s of command line options. You can check out the auto-generated wkhtmltopdf options.
You can also add custom Header and Footer in PDF using page option.
1 |
wkhtmltopdf --header-html header.html http://liferayui.com liferayui.pdf |
Bootstrap 3 with Liferay 6.2
In Current Liferay version 6.2 we have bootstrap 2.3.2 as default theme library.Bootstrap 3 was released on August 19th, 2013, before releasing Liferay 6.3. Bootstrap 3 completely dropped support for IE7 and below. While Liferay 6.2 provided limited support for IE7 and below. So here are some of the distinctive differences between Bootstrap v2.3 and v3.0:
Bootstrap 3 |
Bootstrap 2.3.2 |
Grids: Fluid (480px, 768px, 992px, 1200px) | Flat Design: Gradients are no longer available in V3.0 |
Grids: Fluid and fixed (724px, 940px, 1170px; below 768px are single column and vertically stacked) | CSS3 gradients and box shadows |
Columns Classes: .col-xs-1, .col-sm-1 .col-md-1, .col-lg-1, .col-xs-2, .col-sm-2 .col-md-2, .col-lg-2 | .span1,.span2 |
For know more in detail please go to this link:- http://getbootstrap.com/migration/
Creating Theme with Bootstrap 3 in Liferay 6.2
These are some basic steps for creating Theme with Bootstrap 3. In some of the steps are same except few additional Steps.
- In Liferay Devloper Studio, Go to –> File – New – Liferay Plugin Project.
- Provide the Name inside Project Name Text Box (say Test).
- Select the Plugin Type as – Theme and click Next.
- Select Theme parent – _styled and Theme framework – Velocity. Click Finish.
- A new Theme (Test-theme) will be created.
- Inside _diffs folder create css, images, js and templates folder.
- Copy and paste the required files in the corresponding folders as needed.
- Download Bootstrap 3 CSS file and place it inside _diffs – css
- Open Main.css and call the Bootstrap 3 css file above custom.css
- Modify the files as explained.
Liferay 6.2 Basic Structure: [Two Column Layout]
1 2 3 4 5 6 7 8 9 10 |
<div id="main-content" role="main"> <div class="portlet-layout row-fluid"> <div class="span6 portlet-column portlet-column-first" id="column-1"> $processor.processColumn("column-1", "portlet-column-content portlet-column-content-first") </div> <div class="span6 portlet-column portlet-column-last" id="column-2"> $processor.processColumn("column-2", "portlet-column-content portlet-column-content-last") </div> </div> </div> |
These are the some comman classes who are conflict with Bootstarp 2.3 in liferay 6.2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
.signed-in .collapse { display:block; } .lfr-edit-layout-panel .collapse{ display:block; } .dockbar.navbar-static-top .collapse { display: block; } .navbar-inner .collapse{ display:block; } .dockbar-ready #wrapper { margin-top: 149px; } .dockbar-ready section#content { padding: 0 0 0 0; } .dockbar.navbar-static-top { background-color: transparent; padding: 0px; } .dockbar-messages{ display:none; } .lfr-device-preview{ z-index: 1000; } .modal{ display:block; } .modal-hidden{ display:none; } |