'use strict'

const React = window.React
const angular = window.angular
const {
  map
} = window.R

angular.module('scegratooApp')
  .directive('treeview', function () {
    return {
      // template: '',
      restrict: 'AE',
      scope: {
        data: '=',
        id: '@'
      },
      link: function postLink (scope, element) {
        const TreeNode = React.createClass({
          displayName: 'TreeNode',
          getInitialState: function () {
            return {
              children: []
            }
          },
          render: function () {
            let children = []

            if (this.props.data.children) {
              children = this.props.data.children
            }

            return (
              <li ref='node'>
                <a data-id={this.props.data.id}>
                  {this.props.data.nodeName}
                </a>
                <ul>
                  {map(child => <TreeNode data={child}/>, children)}
                </ul>
              </li>
            )
          }
        })

        const TreeView = React.createClass({
          displayName: 'TreeView',
          getDefaultProps: () => ({data: {}}),
          getInitialState: () => ({data: {}}),
          render: function () {
            return (
              <div>
                <ul>
                  <TreeNode data={this.props.data} />
                </ul>
              </div>
            )
          }
        })

        scope.$watch('data', function () {
          React.render(
            React.createElement(
              TreeView,
              {
                data: scope.data
              }
            ),
            element[0]
          )
        })
      }
    }
  })

@@ -79,24 +79,469 @@
 %7B%0A  
-map%0A%7D = window.R
+__,%0A  always,%0A  concat,%0A  curry,%0A  gt,%0A  identity,%0A  ifElse,%0A  isArrayLike,%0A  isNil,%0A  length,%0A  map,%0A  pipe,%0A  reduce,%0A  substringTo%0A%7D = window.R%0Aconst ifNil = ifElse(isNil)%0Aconst ensureArray = ifElse(isArrayLike, identity, always(%5B%5D))%0Aconst shorten = curry((maxLength, string) =%3E %7B%0A  return ifElse(%0A    pipe(%0A      length,%0A      gt(__, maxLength)%0A    ),%0A    pipe(%0A      substringTo(maxLength),%0A      concat(__, ' ...')%0A    ),%0A    always(string)%0A  )(string)%0A%7D)
 %0A%0Aan
@@ -1018,43 +1018,75 @@
     
-let children = %5B%5D%0A%0A            if (
+return (%0A              %3Cli ref='node'%3E%0A                %3Ca data-id=%7B
 this
@@ -1101,20 +1101,14 @@
 ata.
-children) %7B%0A
+id%7D%3E%0A 
     
@@ -1121,19 +1121,15 @@
     
-children = 
+   %7B%60$%7B
 this
@@ -1140,24 +1140,37 @@
 ps.data.
-children
+nodeName%7D:: $%7Breduce(
 %0A       
@@ -1178,19 +1178,16 @@
     
-%7D%0A%0A
         
     
@@ -1186,50 +1186,85 @@
     
-    return (%0A              %3Cli ref='node'%3E
+(o, a) =%3E %60$%7Bo%7D $%7Ba.name%7D:%22$%7Bshorten(20, a.value)%7D%22%60,%0A                    '',
 %0A   
@@ -1272,36 +1272,40 @@
             
-%3Ca data-id=%7B
+    ensureArray(
 this.props.d
@@ -1300,36 +1300,43 @@
 this.props.data.
-id%7D%3E
+attributes)
 %0A               
@@ -1342,33 +1342,11 @@
     
-%7Bthis.props.data.nodeName
+)%7D%60
 %7D%0A  
@@ -1408,47 +1408,149 @@
    %7B
-map(child =%3E %3CTreeNode data=%7Bchild%7D/%3E, 
+ifNil(%0A                    always(undefined),%0A                    map(child =%3E %3CTreeNode data=%7Bchild%7D/%3E)%0A                  )(this.props.data.
 chil

add attributes to tree view