<commit_before>angular.module('domegis')

.controller('SiteCtrl', [
  '$state',
  'Server',
  '$scope',
  'MessageService',
  function($state, Server, $scope, Message) {

    Server.auth().then(function() {
      $scope.token = Server.app.get('token');
      $scope.user = Server.app.get('user');
    });

    $scope.auth = function(credentials) {
      Server.auth(_.extend({
        type: 'local'
      }, credentials)).then(function() {
        $scope.token = Server.app.get('token');
        $scope.user = Server.app.get('user');
        if($state.current.name == 'login')
          $state.go('home');
      }, function(err) {
        Message.add(err.message);
      });
    };

    $scope.logout = function() {
      $scope.token = undefined;
      $scope.user = undefined;
      Server.app.logout();
    };

    $scope.hasRole = function(role) {
      if($scope.user)
        return _.find($scope.user.roles, function(r) { return r == role; });
      else
        return false;
    }

    $scope.getLogs = function() {
      Server.find(Server.service('/admin/logs.csv')).then(function(data) {
        var uri = 'data:text/csv;charset=utf-8,' + escape(data);
        var link = document.createElement('a');
        link.href = uri;
        link.style = 'visibility:hidden';
        link.download = 'domegis-logs.csv';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    };

    $scope.bodyClass = [];

    $scope.$on('$stateChangeStart', function(ev, toState, toParams) {
      $scope.bodyClass = [];
      if(toState.name == 'map') {
        $scope.bodyClass.push('map');
      }
      $scope.bodyClass.push('loading');
    });

    $scope.$on('$stateChangeSuccess', function(ev, toState, toParams) {
      if(toState.name == 'login' && $scope.token)
        $state.go('home');
      $scope.bodyClass = _.filter($scope.bodyClass, function(c) {
        return c != 'loading';
      });
      $scope.bodyClass.push('loaded');
    });

  }
])

.controller('GenerateCtrl', [
  '$scope',
  '$state',
  'Lang',
  'Server',
  function($scope, $state, Lang, Server) {

    var searchService = Server.service('search');
    var viewService = Server.service('views');

    $scope.langs = Lang.getLanguages();

    $scope.settings = {
      lng: ''
    };

    $scope.scroll = false;

    $scope.search = '';
    $scope.results = [];

    $scope.$watch('search', function(search) {
      if(search) {
        searchService.find({
          query: {
            'term': search
          }
        }).then(function(res) {
          $scope.$apply(function() {
            if(res.layers && res.layers.length) {
              $scope.results = res.layers;
            } else {
              $scope.results = [];
            }
          });
        });
      } else {
        $scope.results = [];
      }
    });

    $scope.map = {};
    $scope._layers = [];

    $scope.addLayer = function(layer) {
      if(!$scope.map[layer.id]) {
        $scope.map[layer.id] = {};
        $scope._layers.push(layer);
        viewService.find({
          query: {
            layerId: layer.id
          }
        }).then(function(res) {
          $scope.$apply(function() {
            layer.views = res.data;
            $scope.map[layer.id].id = res.data[0].id;
            $scope.map[layer.id].hidden = false;
          });
        });
      }
    };

    $scope.removeLayer = function(layerId) {
      if($scope.map[layerId]) {
        delete $scope.map[layerId];
        $scope._layers = _.filter($scope._layers, function(l) {
          return l.id !== layerId;
        });
      }
    };

    $scope.getHTMLEmbed = function() {
      var url = $state.href('map', {views: getCSViews(), lang: $scope.settings.lng, base: $scope.base, scroll: $scope.scroll}, {absolute: true});
      return '<iframe src="' + url + '" width="100%" height="400" frameborder="0" allowfullscren></iframe>';
    };

    $scope.getWPShortcode = function() {
      var config = {
        views: getCSViews(),
        lang: $scope.settings.lng,
        base: $scope.base,
        scroll: $scope.scroll
      };
      var shortcode = '[domegis_map';
      for(var key in config) {
        if(config[key])
          shortcode += ' ' + key + '="' + config[key] + '"';
      }
      shortcode += ']';
      return shortcode;
    };

    $scope.views = [];

    var _update = function() {
      $scope.views = [];
      $scope._layers.forEach(function(l) {
        if($scope.map[l.id] && $scope.map[l.id].id)
          $scope.views.push($scope.map[l.id]);
      });
    };

    $scope.$watch('map', _update, true);
    $scope.$watch('_layers', _update, true);
    $scope.$watch('settings', _update, true);
    $scope.$watch('base', _update, true);

    function getCSViews() {
      var views = [];
      $scope._layers.forEach(function(l) {
        if($scope.map[l.id] && $scope.map[l.id].id)
          views.push($scope.map[l.id].id + ':' + ($scope.map[l.id].hidden ? 0 : 1));
      });

      return views.join(',');
    }
  }
])

.controller('QueryCtrl', [
  '$scope',
  'Content',
  'Synced',
  'Server',
  'esriService',
  'MessageService',
  function($scope, Content, Synced, Server, Esri, Message) {

    var contentService = Server.service('contents');

    $scope.content = Content;

    $scope.search = '';
    $scope.query = {
      type: 'Feature Service'
    };
    $scope.params = {};

    $scope.sort = 'modified';

    $scope.availableTypes = Esri.getContentTypes();

    $scope.doQuery = function() {
      $scope.doSort();
      Esri.getContent(
        $scope.search,
        $scope.query,
        $scope.params
      ).then(function(data) {
        $scope.content = data;
      });
    };

    $scope.$watchGroup(['search', 'query'], _.debounce(function() {
      $scope.doQuery();
    }, 500), true);

    $scope.doSort = function() {
      $scope.params = _.extend($scope.params, {
        sortField: $scope.sort,
        sortOrder: getSortOrder($scope.sort)
      });
    };

    function getSortOrder(sort) {
      if(sort == 'title' || sort == 'owner') {
        return 'asc';
      } else {
        return 'desc';
      }
    }

    $scope.syncItem = function(item) {
      return Server.create(contentService, item).then(function() {
      }, function(err) {
        Message.add(err.message);
      });
    };

    $scope.unsyncItem = function(item) {
      Server.remove(contentService, item.id);
    };


    $scope.toggleSync = function(item) {
      if(!$scope.isSynced(item)) {
        $scope.syncItem(item).then(function() {
          item.$viewLayers = true;
        });
      } else {
        if(confirm('Are you sure you\'d like to remove this content from collection?'))
          $scope.unsyncItem(item);
      }
    };

    $scope.toggleLayers = function(item) {
      if(item.$viewLayers) {
        item.$viewLayers = false;
      } else {
        if(!$scope.isSynced(item) && Server.app.get('token')) {
          if(confirm('Would you like to add this content to collection?')) {
            $scope.toggleSync(item);
          }
        } else {
          item.$viewLayers = true;
        }
      }
    }

    $scope.synced = Synced.data;

    Server.on(contentService, 'created', function(data) {
      if(!$scope.isSynced(data))
        $scope.synced.push(data);
    });
    Server.on(contentService, 'removed', function(data) {
      $scope.synced = _.filter($scope.synced, function(item) {
        return item.id !== data.id;
      });
    });

    $scope.isSynced = function(item) {
      return _.find($scope.synced, function(s) {
        return item.id == s.id;
      });
    };

  }
])

.controller('LayerEditCtrl', [
  '$scope',
  'Server',
  'Edit',
  'Lang',
  'MessageService',
  function($scope, Server, Edit, Lang, Message) {

    var layerService = Server.service('layers');

    $scope.layer = angular.copy(Edit);

    $scope.layer.name = langSplit($scope.layer.name);

    $scope.langs = Lang.getLanguages();

    $scope.lang = Lang.get();

    $scope.formLang = function(lang) {
      $scope.lang = lang;
    };

    $scope.save = function(layer) {
      layer.name = langJoin(layer.name);
      if(Edit.id) {
        Server.update(layerService, Edit.id, layer).then(function(layer) {
          $scope.layer = layer;
          $scope.layer.name = langSplit($scope.layer.name);
        }, function(err) {
          Message.add(err.message);
        });
      } else {
        Server.create(layerService, layer).then(function(layer) {
          $scope.layer = layer;
          $scope.layer.name = langSplit($scope.layer.name);
        }, function(err) {
          Message.add(err.message);
        });
      }
    };

  }
])

.controller('ArcGisCtrl', [
  '$scope',
  function($scope) {
    $scope.getArcGisUrl = function(content) {
      return 'http://' + arcgis.organization + '.maps.arcgis.com/home/item.html?id=' + content.id;
    }
  }
])

.controller('ContentEditCtrl', [
  '$scope',
  'Server',
  'Edit',
  function($scope, Server, Edit) {

    var contentService = Server.service('contents');

    $scope.content = angular.copy(Edit);

    $scope.save = function(content) {
      if(Edit.id) {
        Server.update(contentService, Edit.id, content);
      } else {
        Server.create(contentService, content);
      }
    };

  }
])

.controller('ViewEditCtrl', [
  '$scope',
  '$state',
  'Server',
  'Edit',
  'Layer',
  'Distinct',
  'MessageService',
  function($scope, $state, Server, Edit, Layer, Distinct, Message) {

    $scope.distinct = Distinct.data;

    var viewService = Server.service('views');
    var previewService = Server.service('previews');

    $scope.layer = Layer;

    $scope.view = angular.copy(Edit);

    $scope.toggleField = function(field) {
      var idx = $scope.view.fields.indexOf(field.name);
      // is currently selected
      if (idx > -1) {
        $scope.view.fields.splice(idx, 1);
      }
      // is newly selected
      else {
        $scope.view.fields.push(field.name);
      }
    };

    $scope.$watch('view.style', _.debounce(function() {
      if($scope.view.id) {
        Server.update(previewService, $scope.view.id, $scope.view).then(function(preview) {
          $scope.view = preview;
          $scope.$broadcast('updateLayers');
        });
      } else {
        Server.create(previewService, $scope.view).then(function(preview) {
          $scope.view = preview;
          $scope.$broadcast('updateLayers');
        });
      }
    }, 300), true);

    $scope.save = function(view) {
      if(Edit.id) {
        Server.patch(viewService, Edit.id, view).then(function(view) {
          $scope.view = view;
          $state.go('editView', {id: view.id}, {reload: true});
        }, function(err) {
          Message.add(err.message);
        });
      } else {
        Server.create(viewService, view).then(function(view) {
          $scope.view = view;
          $state.go('editView', {id: view.id}, {reload: true});
        }, function(err) {
          Message.add(err.message);
        });
      }
    };

  }
])

.controller('UserCtrl', [
  '$scope',
  'Server',
  'Users',
  function($scope, Server, Users) {

    var userService = Server.service('users');

    $scope.users = Users.data;

    $scope.remove = function(user) {
      if(confirm('Are you sure?')) {
        Server.remove(userService, user.id);
      }
    };

    Server.on(userService, 'created', function(data) {
      $scope.users.push(data);
    });
    Server.on(userService, 'removed', function(data) {
      $scope.users = _.filter($scope.users, function(user) {
        return user.id !== data.id;
      });
    });


  }
])

.controller('UserEditCtrl', [
  '$scope',
  '$state',
  'Server',
  'Edit',
  'MessageService',
  function($scope, $state, Server, Edit, Message) {

    var userService = Server.service('users');

    $scope.user = angular.copy(Edit);

    $scope.toggleRole = function(role) {

      if(!$scope.user.roles)
        $scope.user.roles = [];

      var idx = $scope.user.roles.indexOf(role);
      // is currently selected
      if (idx > -1) {
        $scope.user.roles.splice(idx, 1);
      }
      // is newly selected
      else {
        $scope.user.roles.push(role);
      }
    };

    $scope.save = function(user) {
      if(Edit.id) {
        delete user.password;
        Server.patch(userService, Edit.id, user).then(function(user) {
          $scope.user = user;
          $state.go('usersEdit', {id: user.id}, {reload: true});
        }, function(err) {
          Message.add(err.message);
        });
      } else {
        Server.create(userService, user).then(function(user) {
          $scope.user = user;
          $state.go('usersEdit', {id: user.id}, {reload: true});
        }, function(err) {
          Message.add(err.message);
        });
      }
    };

    $scope.pwd = {};

    $scope.updatePwd = function(pwd) {
      if(!pwd.password) {
        alert('You have to type in a new password');
      } else if(pwd.password !== <PASSWORD>.passwordRepeat) {
        alert('Passwords don\'t match');
      } else {
        Server.patch(userService, Edit.id, {
          userPassword: <PASSWORD>,
          password: <PASSWORD>
        }).then(function(data) {
          console.log(data);
        }, function(err) {
          Message.add(err.message);
        });
      }
    }
  }
])

.controller('DerivedCtrl', [
  '$http',
  '$scope',
  function($http, $scope) {
    $scope.sql = '';
    $scope.err = '';
    $scope.submit = function() {
      $scope.err = '';
      $scope.data = [];
      $http.get('/layers/preview', {
        params: {
          sql: $scope.sql
        }
      }).then(function(res) {
        console.log(res);
        $scope.data = res.data[0];
        $scope.keys = Object.keys(res.data[0][0]);
      }, function(err) {
        $scope.err = err.data.message;
        console.log(err);
      });
    };
  }
]);
<commit_msg>fix allowfullscreen<commit_after>angular.module('domegis')

.controller('SiteCtrl', [
  '$state',
  'Server',
  '$scope',
  'MessageService',
  function($state, Server, $scope, Message) {

    Server.auth().then(function() {
      $scope.token = Server.app.get('token');
      $scope.user = Server.app.get('user');
    });

    $scope.auth = function(credentials) {
      Server.auth(_.extend({
        type: 'local'
      }, credentials)).then(function() {
        $scope.token = Server.app.get('token');
        $scope.user = Server.app.get('user');
        if($state.current.name == 'login')
          $state.go('home');
      }, function(err) {
        Message.add(err.message);
      });
    };

    $scope.logout = function() {
      $scope.token = undefined;
      $scope.user = undefined;
      Server.app.logout();
    };

    $scope.hasRole = function(role) {
      if($scope.user)
        return _.find($scope.user.roles, function(r) { return r == role; });
      else
        return false;
    }

    $scope.getLogs = function() {
      Server.find(Server.service('/admin/logs.csv')).then(function(data) {
        var uri = 'data:text/csv;charset=utf-8,' + escape(data);
        var link = document.createElement('a');
        link.href = uri;
        link.style = 'visibility:hidden';
        link.download = 'domegis-logs.csv';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    };

    $scope.bodyClass = [];

    $scope.$on('$stateChangeStart', function(ev, toState, toParams) {
      $scope.bodyClass = [];
      if(toState.name == 'map') {
        $scope.bodyClass.push('map');
      }
      $scope.bodyClass.push('loading');
    });

    $scope.$on('$stateChangeSuccess', function(ev, toState, toParams) {
      if(toState.name == 'login' && $scope.token)
        $state.go('home');
      $scope.bodyClass = _.filter($scope.bodyClass, function(c) {
        return c != 'loading';
      });
      $scope.bodyClass.push('loaded');
    });

  }
])

.controller('GenerateCtrl', [
  '$scope',
  '$state',
  'Lang',
  'Server',
  function($scope, $state, Lang, Server) {

    var searchService = Server.service('search');
    var viewService = Server.service('views');

    $scope.langs = Lang.getLanguages();

    $scope.settings = {
      lng: ''
    };

    $scope.scroll = false;

    $scope.search = '';
    $scope.results = [];

    $scope.$watch('search', function(search) {
      if(search) {
        searchService.find({
          query: {
            'term': search
          }
        }).then(function(res) {
          $scope.$apply(function() {
            if(res.layers && res.layers.length) {
              $scope.results = res.layers;
            } else {
              $scope.results = [];
            }
          });
        });
      } else {
        $scope.results = [];
      }
    });

    $scope.map = {};
    $scope._layers = [];

    $scope.addLayer = function(layer) {
      if(!$scope.map[layer.id]) {
        $scope.map[layer.id] = {};
        $scope._layers.push(layer);
        viewService.find({
          query: {
            layerId: layer.id
          }
        }).then(function(res) {
          $scope.$apply(function() {
            layer.views = res.data;
            $scope.map[layer.id].id = res.data[0].id;
            $scope.map[layer.id].hidden = false;
          });
        });
      }
    };

    $scope.removeLayer = function(layerId) {
      if($scope.map[layerId]) {
        delete $scope.map[layerId];
        $scope._layers = _.filter($scope._layers, function(l) {
          return l.id !== layerId;
        });
      }
    };

    $scope.getHTMLEmbed = function() {
      var url = $state.href('map', {views: getCSViews(), lang: $scope.settings.lng, base: $scope.base, scroll: $scope.scroll}, {absolute: true});
      return '<iframe src="' + url + '" width="100%" height="400" frameborder="0" allowfullscreen></iframe>';
    };

    $scope.getWPShortcode = function() {
      var config = {
        views: getCSViews(),
        lang: $scope.settings.lng,
        base: $scope.base,
        scroll: $scope.scroll
      };
      var shortcode = '[domegis_map';
      for(var key in config) {
        if(config[key])
          shortcode += ' ' + key + '="' + config[key] + '"';
      }
      shortcode += ']';
      return shortcode;
    };

    $scope.views = [];

    var _update = function() {
      $scope.views = [];
      $scope._layers.forEach(function(l) {
        if($scope.map[l.id] && $scope.map[l.id].id)
          $scope.views.push($scope.map[l.id]);
      });
    };

    $scope.$watch('map', _update, true);
    $scope.$watch('_layers', _update, true);
    $scope.$watch('settings', _update, true);
    $scope.$watch('base', _update, true);

    function getCSViews() {
      var views = [];
      $scope._layers.forEach(function(l) {
        if($scope.map[l.id] && $scope.map[l.id].id)
          views.push($scope.map[l.id].id + ':' + ($scope.map[l.id].hidden ? 0 : 1));
      });

      return views.join(',');
    }
  }
])

.controller('QueryCtrl', [
  '$scope',
  'Content',
  'Synced',
  'Server',
  'esriService',
  'MessageService',
  function($scope, Content, Synced, Server, Esri, Message) {

    var contentService = Server.service('contents');

    $scope.content = Content;

    $scope.search = '';
    $scope.query = {
      type: 'Feature Service'
    };
    $scope.params = {};

    $scope.sort = 'modified';

    $scope.availableTypes = Esri.getContentTypes();

    $scope.doQuery = function() {
      $scope.doSort();
      Esri.getContent(
        $scope.search,
        $scope.query,
        $scope.params
      ).then(function(data) {
        $scope.content = data;
      });
    };

    $scope.$watchGroup(['search', 'query'], _.debounce(function() {
      $scope.doQuery();
    }, 500), true);

    $scope.doSort = function() {
      $scope.params = _.extend($scope.params, {
        sortField: $scope.sort,
        sortOrder: getSortOrder($scope.sort)
      });
    };

    function getSortOrder(sort) {
      if(sort == 'title' || sort == 'owner') {
        return 'asc';
      } else {
        return 'desc';
      }
    }

    $scope.syncItem = function(item) {
      return Server.create(contentService, item).then(function() {
      }, function(err) {
        Message.add(err.message);
      });
    };

    $scope.unsyncItem = function(item) {
      Server.remove(contentService, item.id);
    };


    $scope.toggleSync = function(item) {
      if(!$scope.isSynced(item)) {
        $scope.syncItem(item).then(function() {
          item.$viewLayers = true;
        });
      } else {
        if(confirm('Are you sure you\'d like to remove this content from collection?'))
          $scope.unsyncItem(item);
      }
    };

    $scope.toggleLayers = function(item) {
      if(item.$viewLayers) {
        item.$viewLayers = false;
      } else {
        if(!$scope.isSynced(item) && Server.app.get('token')) {
          if(confirm('Would you like to add this content to collection?')) {
            $scope.toggleSync(item);
          }
        } else {
          item.$viewLayers = true;
        }
      }
    }

    $scope.synced = Synced.data;

    Server.on(contentService, 'created', function(data) {
      if(!$scope.isSynced(data))
        $scope.synced.push(data);
    });
    Server.on(contentService, 'removed', function(data) {
      $scope.synced = _.filter($scope.synced, function(item) {
        return item.id !== data.id;
      });
    });

    $scope.isSynced = function(item) {
      return _.find($scope.synced, function(s) {
        return item.id == s.id;
      });
    };

  }
])

.controller('LayerEditCtrl', [
  '$scope',
  'Server',
  'Edit',
  'Lang',
  'MessageService',
  function($scope, Server, Edit, Lang, Message) {

    var layerService = Server.service('layers');

    $scope.layer = angular.copy(Edit);

    $scope.layer.name = langSplit($scope.layer.name);

    $scope.langs = Lang.getLanguages();

    $scope.lang = Lang.get();

    $scope.formLang = function(lang) {
      $scope.lang = lang;
    };

    $scope.save = function(layer) {
      layer.name = langJoin(layer.name);
      if(Edit.id) {
        Server.update(layerService, Edit.id, layer).then(function(layer) {
          $scope.layer = layer;
          $scope.layer.name = langSplit($scope.layer.name);
        }, function(err) {
          Message.add(err.message);
        });
      } else {
        Server.create(layerService, layer).then(function(layer) {
          $scope.layer = layer;
          $scope.layer.name = langSplit($scope.layer.name);
        }, function(err) {
          Message.add(err.message);
        });
      }
    };

  }
])

.controller('ArcGisCtrl', [
  '$scope',
  function($scope) {
    $scope.getArcGisUrl = function(content) {
      return 'http://' + arcgis.organization + '.maps.arcgis.com/home/item.html?id=' + content.id;
    }
  }
])

.controller('ContentEditCtrl', [
  '$scope',
  'Server',
  'Edit',
  function($scope, Server, Edit) {

    var contentService = Server.service('contents');

    $scope.content = angular.copy(Edit);

    $scope.save = function(content) {
      if(Edit.id) {
        Server.update(contentService, Edit.id, content);
      } else {
        Server.create(contentService, content);
      }
    };

  }
])

.controller('ViewEditCtrl', [
  '$scope',
  '$state',
  'Server',
  'Edit',
  'Layer',
  'Distinct',
  'MessageService',
  function($scope, $state, Server, Edit, Layer, Distinct, Message) {

    $scope.distinct = Distinct.data;

    var viewService = Server.service('views');
    var previewService = Server.service('previews');

    $scope.layer = Layer;

    $scope.view = angular.copy(Edit);

    $scope.toggleField = function(field) {
      var idx = $scope.view.fields.indexOf(field.name);
      // is currently selected
      if (idx > -1) {
        $scope.view.fields.splice(idx, 1);
      }
      // is newly selected
      else {
        $scope.view.fields.push(field.name);
      }
    };

    $scope.$watch('view.style', _.debounce(function() {
      if($scope.view.id) {
        Server.update(previewService, $scope.view.id, $scope.view).then(function(preview) {
          $scope.view = preview;
          $scope.$broadcast('updateLayers');
        });
      } else {
        Server.create(previewService, $scope.view).then(function(preview) {
          $scope.view = preview;
          $scope.$broadcast('updateLayers');
        });
      }
    }, 300), true);

    $scope.save = function(view) {
      if(Edit.id) {
        Server.patch(viewService, Edit.id, view).then(function(view) {
          $scope.view = view;
          $state.go('editView', {id: view.id}, {reload: true});
        }, function(err) {
          Message.add(err.message);
        });
      } else {
        Server.create(viewService, view).then(function(view) {
          $scope.view = view;
          $state.go('editView', {id: view.id}, {reload: true});
        }, function(err) {
          Message.add(err.message);
        });
      }
    };

  }
])

.controller('UserCtrl', [
  '$scope',
  'Server',
  'Users',
  function($scope, Server, Users) {

    var userService = Server.service('users');

    $scope.users = Users.data;

    $scope.remove = function(user) {
      if(confirm('Are you sure?')) {
        Server.remove(userService, user.id);
      }
    };

    Server.on(userService, 'created', function(data) {
      $scope.users.push(data);
    });
    Server.on(userService, 'removed', function(data) {
      $scope.users = _.filter($scope.users, function(user) {
        return user.id !== data.id;
      });
    });


  }
])

.controller('UserEditCtrl', [
  '$scope',
  '$state',
  'Server',
  'Edit',
  'MessageService',
  function($scope, $state, Server, Edit, Message) {

    var userService = Server.service('users');

    $scope.user = angular.copy(Edit);

    $scope.toggleRole = function(role) {

      if(!$scope.user.roles)
        $scope.user.roles = [];

      var idx = $scope.user.roles.indexOf(role);
      // is currently selected
      if (idx > -1) {
        $scope.user.roles.splice(idx, 1);
      }
      // is newly selected
      else {
        $scope.user.roles.push(role);
      }
    };

    $scope.save = function(user) {
      if(Edit.id) {
        delete user.password;
        Server.patch(userService, Edit.id, user).then(function(user) {
          $scope.user = user;
          $state.go('usersEdit', {id: user.id}, {reload: true});
        }, function(err) {
          Message.add(err.message);
        });
      } else {
        Server.create(userService, user).then(function(user) {
          $scope.user = user;
          $state.go('usersEdit', {id: user.id}, {reload: true});
        }, function(err) {
          Message.add(err.message);
        });
      }
    };

    $scope.pwd = {};

    $scope.updatePwd = function(pwd) {
      if(!pwd.password) {
        alert('You have to type in a new password');
      } else if(pwd.password !== pwd.passwordRepeat) {
        alert('Passwords don\'t match');
      } else {
        Server.patch(userService, Edit.id, {
          userPassword: <PASSWORD>,
          password: <PASSWORD>
        }).then(function(data) {
          console.log(data);
        }, function(err) {
          Message.add(err.message);
        });
      }
    }
  }
])

.controller('DerivedCtrl', [
  '$http',
  '$scope',
  function($http, $scope) {
    $scope.sql = '';
    $scope.err = '';
    $scope.submit = function() {
      $scope.err = '';
      $scope.data = [];
      $http.get('/layers/preview', {
        params: {
          sql: $scope.sql
        }
      }).then(function(res) {
        console.log(res);
        $scope.data = res.data[0];
        $scope.keys = Object.keys(res.data[0][0]);
      }, function(err) {
        $scope.err = err.data.message;
        console.log(err);
      });
    };
  }
]);

{'fraction_non_alphanumeric': 0.18013939785873392, 'fraction_numerical': 0.0017245095925846087, 'mean_word_length': 2.154465095194923, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '85208', 'n_tokens_mistral': 9108, 'n_tokens_neox': 8883, 'n_words': 1409}