# Installing and running
## Installing tools
1. Download and install [Git](http://git-scm.com/downloads) and make sure the executable is in environment path

2. Download and install [Node.js](https://nodejs.org/download/) and make sure the executable is in environment path

3. Download and install [Ruby](http://rubyinstaller.org/) and make sure the executable is on environment path

4. Open a terminal and type:

    ```bash
    $ gem install compass
    ```
    If you are running this command on a Windows system you might get this error:

    ```
    ERROR: Could not find a valid gem 'compass' (>= 0), here is why:
    Unable to download data from https://rubygems.org/ - SSL_connect returned=1
    errno=0 state=SSLv3 read server certificate B: certificate verify failed
    (https://api.rubygems.org/latest_specs.4.8.gz)
    ```

    If you get the above error run the following commands:

    ```bash
    $ gem sources -r https://rubygems.org/
    $ gem sources -a http://rubygems.org/
    ```

    You can find more details here: http://stackoverflow.com/questions/10246023/
    After you run the two commands above run again:

    ```bash
    $ gem install compass
    ```
    And this time it should work.

5. Run the folowing command (for linux/mac use `sudo`):

    ```bash
    $ npm install -g gulp bower cex
    ```

    If you are running this command on a Windows system you might get this error:

    Error: ENOENT, stat `'C:\Users\<user>\AppData\Roaming\npm'`

    If that happens go inside the `'C:\Users\<user>\AppData\Roaming\'` folder and create there a 'npm' folder.

6. Create a folder for the project and cd into it

## Your project
### Creating a new project
1. Install yeoman and the cex yeoman generator by running:
   
    ```bash    
    $ sudo npm install -g yo generator-cex
    ````
    Go to a location on disk where you want the project created, e.g.:
    
    ```bash
    $ mkdir my-project
    $ cd my-project
    ```
2. Run `yo cex` and fill in the answers to the questions the nice man will ask you.
    You should see a lot of output after which you can run gulp serve and you should be able to see your project up and running on http://localhost:9000
    
### Working on an existing project
1. Clone the project from git by running the following command inside the project folder:

    ```bash
    $ git clone http://git.everymatrix.com/ufe-operators/<project>.git
    ```

2. After the project is downloaded run the following command from the project folder:

    ```bash
    $ npm install && bower install && cex install
    ```

## Running the project
1. After the project is built you can start with (If a browser window does not open access the home page at `http://localhost:9000`):
    * localhost:

    ```bash
    $ gulp serve [--env=<environments>]
    ```
    * localhost with minification:

    ```bash
    $ gulp serve:dist [--env=<environments>]
    ```

2. API environments:
    * by default the server will launch on the dev api environment, you can start the server with a different environment using '--env=<environment>' option
    * to change the environment in localhost you can use the fallowing commands:

    ```bash
    $ gulp set-env --env=<environments>
    ```
    if you use the `serve:dist` command you can change the message using

    ```bash
    $ gulp set-dist-env --env=<environments>
    ```
    * the available environments are:
        - ` dev   `
        - ` qa    `
        - ` stage ` 
        - ` prod  `

3. To build the project run:

    ```bash
    $ gulp build [--env=<environments>]
    ```

    to change the environment after the project is built you can use:

    ```bash
    $ gulp set-dist-env --env=<environments>
    ```

5. How to run unit tests
    * Testing dist scripts (make sure you run `gulp build` or `gulp serve:dist` before):   
    
    ```bash
    $ gulp test:dist
    ```    
    * Testing dev scripts  (make sure you run `gulp scripts` or `gulp serve` before):   
       
    ```bash
    $ gulp test:tmp
    ```    
    * To rerun the tests at every file change you can yous the above to commands with `--watch` option
    * useful resources:
        - [writing tests in angular](https://docs.angularjs.org/guide/unit-testing)   
        - [angular more test](http://www.smashingmagazine.com/2014/10/07/introduction-to-unit-testing-in-angularjs/)
        - [everything about angular testing](http://lmgtfy.com/?q=angular+tests)

6. How to run acceptance testing

    first run `gulp serve` or `gulp serve:dist` as the tests will be ran against the localhost then:
    
    ```bash
    $ gulp test:e2e
    ```    
    resources:    
    - [angular e2e](https://docs.angularjs.org/guide/e2e-testing)
    - [BDD in JavaScript: CucumberJS](http://custardbelly.com/blog/blog-posts/2014/01/08/bdd-in-js-cucumberjs/)
    
# Writing Code
## Components    
### Installing cex components
CEX components can be found in our [component explorer](http://cex.everymatrix.com/components/list/ALL).

To install run `cex install <component-name>`

Include the component import it by its name in `app/scripts/app.json` or where it is needed

```javasciprt
// require-em-components
require('<cex-component>');
```
And if it is a angular module include its module to you an angular module

```javascript
var appModule = angular.module('myApp', [
    ....
    // inject-em-components
    '3rd-party-component',
    'ui.router',
```
Some components may expose additional functionality that you can use:

```javascript
import {module, observable} from 'em-utils'
```

### Installing 3rd party components
The applications supports 3rd party components out of the box.

To install a 3rd party bower component run `bower install <3rd-party-component>` and if the component is setted up correctly your can simply strart using the component.

If the component is an angular module you need to require the components module name in the `app/scripts/app.js` file

```javascript
var appModule = angular.module('myApp', [
    // inject-3rd-party-modules
    '3rd-party-component',
    'ui.router',
```
If the 3rd-party-component is not configured correctly you may need to include its source files manually

### Writing your own components
The suggested pattern is component based. 

Meaning that all the sub components that if put together creates a future should pe placed in the same folder.

The recommended folder structure is:

    scripts/
        modules/
            index.js    
            my-module/
                index.js
                services/
                    my-servie.js
                directives/
                    my-directive.js
                views/
                    my-directive.html
                    
We recommend to create a `<my-module>/index.js` file in your component that includes all its sub components        
 
#### Writing angular components
We recommend writing fragmented angular sub modules, e.g.:

    scripts/
        modules/
            index.js                        |   angular.module('myapp.modules', ['myCustomModule'])
            my-module/
                index.js                    |   angular.module('myCustomModule', ['myCustomModule.directive', 'myCustomModule.service', 'myCustomModule.secondDirective'])
                services/
                    my-servie.js            |   angular.module('myCustomModule.service', [])
                directives/
                    my-directive.js         |   angular.module('myCustomModule.directive', ['myCustomModule.service'])
                    my-second-directive.js  |   angular.module('myCustomModule.secondDirective', ['myCustomModule.service'])
                views/
                    my-directive.html
                    
We recommend the above structure heaving in mind that the application doesn't and shouldn't need to know the structure of the sub modules

## States
### Creating states with Yeoman
The Yeoman generator includes a sub generator for basic states. 

To run the generator use yo cex:state <stateName>.

The state name should be camelcase 

Note: this generator only generates basic root pages (for now)

### Manually creating states  
To create a state you need to create a js file under the `scripts/states` folder and a html under `views/states` folder 

The folder structure should represent the given url, for example:

If the desired url is '/terms/casino/fun-mode' the file structure should be:

    scripts/
        states/
            index.js  | includes all states file and angular sub modules and the homepage and includes ./terms/index.js page
            terms/
                index.js | the state that renders `www.site.com/terms` page and includes ./casino/index.js page
                casino/
                    index.js | the state that renders `www.site.com/terms/casino` page and includes ./fun-mode.js page
                    fun-mode.js | the state that renders `www.site.com/terms/casino/fun-mode` page
    views/
        states/
            index.html | template for the `www.site.com/` page
                casino/
                    index.html | the template for `www.site.com/terms/casino` page
                    fun-mode  | the template for `www.site.com/terms/casino/fun-mode` page
                    
If a page has subpages it's js file should include all the direct subpages

## Folder structure

    app/
        index.html
        config.json
        favicon.ico
        robots.txt
        browserconfig.xml
        bower_components/                   | the bower components installation folder
        cex_components/                     | the cex components installation folder        
        fonts/  
        img/
        scripts/                            | all the application specific scripts should be found in here
            app.js                          | the main entering point to the application scripts
            weinre.js                       | used bie [weinre](http://people.apache.org/~pmuellr/weinre-docs/latest/) to debug on old browsers
            states/                         | scripts that defines all the application states
                404.js
                home.js
                index.js
                about/
                    casino.js
                    index.js
            modules/                        | custom application specific modules
                index.js    
                my-module/
                    index.js
                    services/
                        my-service.js
                        my-service.spec.js
                    directives/
                        my-directive.js
                        my-directive.controller.js
                        my-directive.controller.spec.js
                    views/
                        my-directive.html
        views/                              | templates folder 
            states/                         | templates for application states
                404.html
                home.html
                about/
                    casino.html
                    index.html
            asses/                          | templates exposed by cex components to allow overwriting to meed applications specific needs 
                <cex-component>/
                    template.html
                 
        styles/                             | sass/css folder
            ie.scss                         | a scss file that will be included only in ie browsers
            main.scss                       | the main entry point for sass file
            pages/                          | css for specific pages (if needed)
                about.scss
            base/                           | some base sass variables and mixis that we strongly recommend using 
                variables.scss
                mixins.scss
                reset.scss
        features/                           | e2e tests
            definitions/
                <someFeature>/
                    <feature>.feature
                    <step1>.js
                    <step2>.js
            support/                        | files that helps writing e2e step definitions
                hooks.js
        resources/
            <files used by servers>
    bower.json
    cex.json
    Dockerfile
    Gulpfile.js
    karma.conf.js
    protractor.conf.js
    package.json
    ...
