/*!
 Copyright (C) 2016 Google Inc.
 Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
 */

(function (GGRC, can) {
  'use strict';

  GGRC.ListLoaders.BaseListLoader('GGRC.ListLoaders.TreePageLoader', {}, {
    init: function (model, instance, mapping) {
      this.model = model;
      this.binding = instance.get_binding(mapping);
    },
    load: function (params) {
      var result;
      return this.model.query(params)
        .then(function (data) {
          result = data;
          return this.insertInstancesFromMappings(data.values);
        }.bind(this))
        .then(function (values) {
          result.values = values;
          return result;
        });
    },
    insertInstancesFromMappings: function (mappings) {
      var self = this;
      var result;

      result = can.map(can.makeArray(mappings), function (mapping) {
        return self.getResultFromMapping(mapping);
      });
      return $.when.apply($, result).then(function () {
        return new can.List(Array.prototype.slice.call(arguments));
      });
    },
    getResultFromMapping: function (mapping) {
      var binding = this.binding;
      return this.makeResult(mapping.reify(), binding);
    },
    makeResult: function (instance, binding) {
      var result;
      if (binding.instance.workflow_people) {
        result = CMS.Models.Person
          .getPersonMappings(binding.instance, instance, 'workflow_people');
      } else if (binding.instance.object_people.length) {
        result = CMS.Models.Person
          .getPersonMappings(binding.instance, instance, 'object_people');
      } else if (instance instanceof CMS.Models.Person) {
        result = CMS.Models.Person
          .getUserRoles(binding.instance, instance);
      } else {
        result = CMS.Models.Relationship
          .getRelationshipBetweenInstances(binding.instance, instance, true);
      }
      return result
        .then(function (relationships) {
          return new GGRC.ListLoaders.MappingResult(
            instance, can.map(relationships, function (relationship) {
              return {
                binding: binding,
                instance: relationship,
                mappings: [{
                  instance: true,
                  mappings: [],
                  binding: binding
                }]
              };
            }), binding);
        });
    }
  });
})(GGRC, can);

@@ -1275,24 +1275,77 @@
 var result;%0A
+      if (instance instanceof CMS.Models.Person) %7B%0A  
       if (bi
@@ -1378,32 +1378,34 @@
 ople) %7B%0A        
+  
 result = CMS.Mod
@@ -1407,32 +1407,34 @@
 S.Models.Person%0A
+  
           .getPe
@@ -1492,32 +1492,34 @@
 people');%0A      
+  
 %7D else if (bindi
@@ -1554,32 +1554,34 @@
 ngth) %7B%0A        
+ 
+ 
 result = CMS.Mod
@@ -1583,32 +1583,34 @@
 S.Models.Person%0A
+  
           .getPe
@@ -1678,60 +1678,21 @@
     
+  
 %7D else
- if (instance instanceof CMS.Models.Person)
  %7B%0A
+  
     
@@ -1714,32 +1714,34 @@
 S.Models.Person%0A
+  
           .getUs
@@ -1773,24 +1773,34 @@
  instance);%0A
+        %7D%0A
       %7D else

Improve tree page loader