thenikso / angular-autolayout

Constraint based layout for your AngularJS apps
http://thenikso.github.io/angular-autolayout
220 stars 17 forks source link

Autolayout for AngularJS

Constraint based layout paradigm for AngularJS web applications inspired by Apple's Auto Layout for iOS and OS X.

HTML and CSS have been designed to present a page style layout like one that you might find on a newspaper. However, nowadays those technologies are also used to for layout of applications that should resemble native ones.

Many features that are needed to properly layout an application are missing from CSS. For example, there is no way to specify that two elements on a page should have the same height!

With angular-autolayout, you can use the same layout technology that Apple gives to native iOS and OS X developers for your HTML5 app.

Usage

Install by cloning the repository or via Bower:

bower install angular-autolayout

Add angular-autolayout to your imported scripts:

<script src="https://github.com/thenikso/angular-autolayout/raw/master/bower_components/angular/angular.js"></script>
<script src="https://github.com/thenikso/angular-autolayout/raw/master/bower_components/angular-autolayout/dit/angular-autolayout.min.js"></script>

Require autolayout in your AngularJS app module:

angular.module("myApp", ["autolayout"]);

You can now use the al-constraint directive to add layout constraints with both Appple's Visual Format Language or by specifying parameters:

<div id="myContainer">
    <div id="myLeftBox">Hello</div>
    <div id="myRightBox">Layout!</div>

    <al-constraint align="top">|-[myLeftBox(==myRightBox)]-[myRightBox]-|</al-constraint>
    <al-constraint>V:|-[myLeftBox]-|</al-constraint>
    <al-constraint
        element="myLeftBox"
        attribute="height"
        relation="equal"
        to-element="myRightBox"
        to-attribute="height"></al-constraint>
</div>

Or programmatically in your controller:

angular.module("myApp").controller("myController", function(autolayout) {
    autolayout(document.getElementById("myContainer"))
        .addConstraint({
            element: document.getElementById("myLeftBox"),
            attribute: "right",
            toElement: document.getElementById("myRightBox"),
            toAttribute: "left",
            relation: "equal",
            constant: -10
        });
});

Documentation

Angular-Autolayout is made to replicate Apple's Auto Layout for the web. The Auto Layout Guide provides some useful documentation that is relevant for this project. Refer especially to the Visual Format Languagee documentation as it has been closely adopted.

Directives

The preferred way of usage of angular-autolayout is via the provided directives:

al-constraint

The al-constraint is the core directive that will add layout constraints to its sibling elements relative to its parent element.

It can be used with both visual language format constraints:

<al-constraint visual-format="V:|-[myDiv]-|"></al-constraint>

or equivalent:

<al-constraint>V:|-[myDiv]-|</al-constraint>

Or it can be used in a form that will map to the programmatic API:

<al-constraint
    element="myLeftBox"
    attribute="height"
    relation="equal"
    to-element="myRightBox"
    to-attribute="height"
    multiplier="2"
    constant="10"
    priority="100"></al-constraint>

See examples for practical usages of this and other directives.

al-update-on

This is an attribute directive to be used on an autolayout container element. It expect a value that identify the name of an event upon which the autolayout should update an re-materialize. For example, in your view:

<div id="containerDiv" al-update-on="myEvent">
    <div id="myBox">Content</div>
    <al-constraint>|[myBox]|</al-constraint>
</div>

In you controller:

angular.element("#containerDiv").css("width", "50%");
scope.$broadcast("myEvent"); // This will trigger an update on the containerDiv's autolayout

al-autolayout-on-resize

The al-autolayout-on-resize attribute directive will update the autolayout of the container element it's defined on upon window resize. Because the update of an autolayout affects all the child autolayouts, this directive needs to be used only on the topmost autolayout in a hierarchy. For example:

<div id="containerDiv" style="width:100%" al-update-on-resize>
    <div id="myBox">Content</div>
    <al-constraint>|[myBox]|</al-constraint>
</div>

Service

The only injectable service exposed is called autolayout. With it you can access to all the functionalities provided by the al-constraint directive via code.

API

Provider

The autolayout service has an autolayoutProvider that can be injected in a module's config phase:

angular.module("myModule", ["autolayout"]).config(function(autolayoutProvider) {
    // Configure autolayoutProvider here
});

The autolayoutProvider allows your to configure many part of how the autolayout service behaves. The accessible configuration properties are:

The provider also exposes methods that are used to interact with attributeConverters:

Cassowary.js

Cassowary.js is included and provided as a constant via the injectable cassowary. Being it a constant, it can also be injected in the config phase to be used within autolayoutProvider's configuration parameters.

Setup project for development

To setup the project, you'll need a working node environment with NPM, Bower and Grunt. When that is set up execute the following commands in a shell within the project directory:

npm install
bower install

Run tests

There are a number of Karma based unit tests using Mocha, Chai and Sinon. To run them use:

grunt test

Build

Building the project will generate a packaged angular-autolayout.js and it's minified version in the dist directory that are already provided for convenience. Build with:

grunt build

Participate

This project uses Cassowary.js and PEG, any contribution to those projects will help this project too.

The first thing you could do to partecipate to angular-autolayout is to use it in your project. By doing that you'll be more likely to find fixes or improvements that may be needed.

Some tasks that can be foreseen are:

License

The MIT License (MIT)

Copyright (c) 2014 Nicola Peduzzi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.