angular.module('domegis')

.filter('dataTemplate', function() {
  return _.memoize(function(input, template) {
    var output = '';
    if(input && input.length) {
      if(input.length == 1) {
        output = Mustache.render(template, input[0]);
      } else {
        output = Mustache.render(template, {data: input});
      }
    }
    return output;
  }, function() {
    return JSON.stringify(arguments);
  })
})

.filter('parseWidgets', [
  '$filter',
  function($filter) {
    return function(input) {
      input.forEach(function(widget) {
        if(!widget.type) return;
        widget._text = '';
        switch (widget.type) {
          case 'text':
            widget._text = widget.content
            break;
          case 'analysis':
            if(widget.content) {
              if(widget.content.dataTemplate) {
                widget._text = $filter('dataTemplate')(widget.content.results, widget.content.dataTemplate);
              } else {
                widget._text = $filter('list')(widget.content.results);
              }
            }
            break;
        }
      });
      return input;
    }
  }
])

.filter('list', [
  '$sce',
  function($sce) {
    return _.memoize(function(input) {
      var html = '';
      if(_.isArray(input)) {
        input.forEach(function(item, i) {
          delete item['$$hashKey'];
          html += '<div class="item-' + i + '">';
          for(var key in item) {
            html += '<p>';
            html += '<strong>' + key + ':</strong> ';
            html += item[key];
            html += '</p>';
          }
          html += '</div>';
        })
      } else if(_.isObject(input)) {
        delete input['$$hashKey'];
        for(var key in input) {
          html += '<p>';
          html += '<strong>' + key + ':</strong> ';
          html += input[key];
          html += '</p>';
        }
      }
      // return $sce.trustAsHtml(html);
      return html;
    }, function() {
      return JSON.stringify(arguments);
    });
  }
])

.filter('trustHtml', [
  '$sce',
  function($sce) {
    return _.memoize(function(input) {
      return $sce.trustAsHtml(input);
    }, function() {
      return JSON.stringify(arguments);
    });
  }
])

.filter('join', function() {
  return function(input, splitChar) {
    return input.join(splitChar);
  }
})

.filter('formatDate', [
  function() {
    return function(input, format) {
      if(input) {
        input = moment(input).format(format || 'LLLL');
      }
      return input;
    }
  }
])

.filter('fromNow', [
  function() {
    return function(input, format) {
      if(input) {
        input = moment(input).fromNow();
      }
      return input;
    }
  }
])

.filter('parseLayerField', [
  function() {
    return function(input) {
      return _.filter(input, function(field) {
        return field.name.trim().toLowerCase() !== 'fid' &&
          field.name !== 'OBJECTID' &&
          field.name.trim().toLowerCase() !== 'id' &&
          field.name !== 'OBJECTID_1';
      });
    }
  }
])

.filter('reverse', function() {
  return _.memoize(function(input) {
    if(_.isArray(input))
      input = input.slice().reverse();
    return input;
  }, function() {
    return JSON.stringify(arguments);
  });
})

.filter('map', [
  function() {
    return function(input, prop) {
      return _.map(input, function(item) {
        return item[prop];
      });
    }
  }
])

@@ -435,24 +435,36 @@
 Widgets', %5B%0A
+  'Server',%0A
   '$filter',
@@ -475,16 +475,24 @@
 unction(
+Server, 
 $filter)
@@ -490,39 +490,60 @@
  $filter) %7B%0A    
-return 
+var parseFilter = _.memoize(
 function(input) 
@@ -832,33 +832,127 @@
         
-if(widget.content
+Server.get(Server.service('analyses'), widget.content.id).then(function(analysis) %7B%0A                if(analysis
 .dataTem
@@ -956,24 +956,26 @@
 Template) %7B%0A
+  
             
@@ -1021,46 +1021,34 @@
 e')(
-widget.content.results, widget.content
+analysis.results, analysis
 .dat
@@ -1069,24 +1069,26 @@
             
+  
 %7D else %7B%0A   
@@ -1076,32 +1076,34 @@
        %7D else %7B%0A
+  
                 
@@ -1133,30 +1133,24 @@
 'list')(
-widget.content
+analysis
 .results
@@ -1148,24 +1148,26 @@
 s.results);%0A
+  
             
@@ -1162,32 +1162,50 @@
               %7D%0A
+              %7D);%0A
             %7D%0A  
@@ -1258,32 +1258,118 @@
 urn input;%0A    %7D
+, function() %7B%0A      return JSON.stringify(arguments);%0A    %7D);%0A    return parseFilter;
 %0A  %7D%0A%5D)%0A%0A.filter

fix #69