(function (module) {
    module.controller('TemplatesModalController', TemplatesModalController);
    TemplatesModalController.$inject = ["project", "templates", "mcmodal", "$modalInstance",
        "Restangular", "User", "processTemplates"];

    function TemplatesModalController(project, templates, mcmodal, $modalInstance, Restangular, User, processTemplates) {
        var ctrl = this;
        ctrl.templates = templates;
        ctrl.viewSetup = viewSetup;

        ctrl.editPrefill = editPrefill;
        ctrl.addPrefill = addPrefill;

        ctrl.toggleFavorite = toggleFavorite;
        ctrl.selectTemplate = selectTemplate;
        ctrl.dismiss = dismiss;
        ctrl.activeTab = 'all';
        ctrl.isActive = isActive;
        ctrl.setActive = setActive;

        /////////////////////

        function viewSetup(template) {
            mcmodal.viewSetup(template);
        }

        function editPrefill(template) {
            var templateDetails = template.create();
            mcmodal.preFill(templateDetails).then(function (t) {
                Restangular.one('v2').one('projects', project.id)
                    .customPUT({
                        process_templates: [
                            {
                                command: 'update',
                                template: {
                                    name: t.name,
                                    setup: t.setup.settings[0],
                                    process_name: t.process_name
                                }
                            }
                        ]
                    }).then(function () {
                        processTemplates.replace(ctrl.templates, t);
                    });
            });
        }

        function addPrefill(template) {
            var templateDetails = template.create();
            var existingTemplateNames = templates.map(function (t) {
                return t.name;
            });
            mcmodal.preFill(templateDetails, existingTemplateNames).then(function (t) {
                console.dir(t.setup.settings[0]);
                Restangular.one('v2').one('projects', project.id)
                    .customPUT({
                        process_templates: [
                            {
                                command: 'add',
                                template: {
                                    name: t.name,
                                    setup: t.setup.settings[0].map(function(setting) {
                                        return {
                                            name: setting.property.name,
                                            attribute: setting.property.attribute,
                                            unit: setting.property.unit,
                                            value: setting.property.value,
                                            _type: setting.property._type
                                        };
                                    }),
                                    process_name: t.process_name
                                }
                            }
                        ]
                    }).then(function () {
                        processTemplates.add(ctrl.templates, t);
                    });
            });
        }

        function toggleFavorite(template) {
            var command = template.favorite ? 'delete' : 'add';
            template.favorite = !template.favorite;

            Restangular.one('v2').one('users', project.id)
                .customPUT({
                    favorites: {
                        processes: [
                            {
                                command: command,
                                name: template.name
                            }
                        ]
                    }
                }).then(function () {
                    if (command == 'add') {
                        User.addToFavorites(project.id, template.name);
                    } else {
                        User.removeFromFavorites(project.id, template.name);
                    }
                });
        }

        function selectTemplate(template) {
            $modalInstance.close(template.name);
        }

        function dismiss() {
            $modalInstance.dismiss('cancel');
        }

        function isActive(tab) {
            return ctrl.activeTab === tab;
        }

        function setActive(tab) {
            ctrl.activeTab = tab;
        }
    }
}(angular.module('materialscommons')));

@@ -1636,36 +1636,32 @@
                 
-    
 processTemplates
@@ -1681,36 +1681,32 @@
 .templates, t);%0A
-    
                 
@@ -2037,58 +2037,8 @@
 ) %7B%0A
-                console.dir(t.setup.settings%5B0%5D);%0A
     
@@ -2098,37 +2098,16 @@
 ject.id)
-%0A                    
 .customP
@@ -2391,16 +2391,27 @@
 ings%5B0%5D.
+properties.
 map(func
@@ -2414,16 +2414,17 @@
 function
+ 
 (setting
@@ -2755,16 +2755,54 @@
   value:
+ setting.property.value == null ? %22%22 :
  setting
@@ -3144,32 +3144,53 @@
               %7D)
+%0A                    
 .then(function (
@@ -3892,20 +3892,16 @@
         
-    
 if (comm
@@ -3912,28 +3912,24 @@
 == 'add') %7B%0A
-    
             
@@ -3976,36 +3976,32 @@
 template.name);%0A
-    
                 
@@ -4025,28 +4025,24 @@
             
-    
 User.removeF
@@ -4094,34 +4094,26 @@
             
-    %7D%0A    
+%7D%0A
             

Reformat and cleanup attributes for templates.