{"size": 3943, "ext": "js", "max_stars_repo_path": "app/client/client/client.js", "max_stars_repo_name": "skeiter9/traru", "max_stars_repo_stars_event_min_datetime": "2015-10-28T18:31:14.000Z", "max_stars_repo_stars_event_max_datetime": "2015-10-30T06:56:47.000Z", "max_issues_repo_path": "app/client/client/client.js", "max_issues_repo_name": "skeiter9/traru", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/client/client/client.js", "max_forks_repo_name": "skeiter9/traru", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.9645390071, "max_line_length": 85, "alphanum_fraction": 0.5297996449}
const moduleName = 'client';
const modelName = moduleName.slice(0, 1).toUpperCase() +
  moduleName.slice(1).toLowerCase();

import utilsDirectives from '../utils/utils-directive/utils.directive.js';
import personAm from '../person/person.js';
import companyAm from '../company/company.js';

export default angular.module(`traru${modelName}`, [
  utilsDirectives.name,
  personAm.name,
  companyAm.name
])

  .directive(`${moduleName}List`, ['layout', modelName, '$translate', '$log',
  '$timeout',
  (l, M, $tr, $l, $t) => ({
    restrict: 'E',
    scope: {
      module: '='
    },
    bindToController: true,
    controller: angular.noop,
    controllerAs: 'vm',
    template: require(`./templates/${moduleName}-list.jade`)(),
    link(s, elem, attrs, vm) {

      const init = () => l.validModule(vm.module)
        .then(() => vm.module.model.find({
          filter: {include: ['person', 'company']}
        }).$promise)

        .then((items) => {
          vm.initialize = true;
          vm.items = items;
        })
        .catch((err) => {
          $l.debug('module for client fails', err);
          vm.initialize = true;
        });

      init();

      vm.refresh = init;

      l.crudRoutes({
        moduleName: 'client',
        vm: vm
      });

    }

  })])

  .directive('clients', ['layout', (l) => ({
    restrict: 'E',
    scope: {
      item: '='
    },
    bindToController: true,
    controller: angular.noop,
    controllerAs: 'vm',
    template: require(`./templates/client-show.jade`)()
  })])

  .directive(`${moduleName}Form`, ['layout', '$log', 'yeValidForm', 'Client',
  '$mdToast', '$translate', 'validFormUtils', '$q', 'Person', 'Company',
  'async',
  (l, $l, vForm, M, $mdT, $tr, vFormU, $q, P, C, async) => ({
    restrict: 'E',
    scope: {
      item: '='
    },
    bindToController: true,
    controller: angular.noop,
    controllerAs: 'mForm',
    template: require(`./templates/${moduleName}-form.jade`)(),
    link(s, elem, attrs, mForm) {

      const init = () => {
        mForm.initialize = false;
        mForm.theme = moduleName;

        mForm.update = l.isFormUpdate(mForm);

        mForm.form = mForm.update ?
          angular.extend({}, mForm.item) :
          {person: {}, company: {}};

        mForm.form.clientType = 0;
        return M.find().$promise
          .then(cs => {
            mForm.initialize = true;
            mForm.clients = cs;
            if (mForm.update) {
              mForm.form.update = true;
              mForm.form.clientType = !!mForm.item.person ? 0 : 1;
              if (!!mForm.item.person) mForm.personaId = mForm.item.person.id;
              else if (!!mForm.item.company) mForm.companyId = mForm.item.company.id;
            }
          });
      };

      init();

      mForm.loadPersons = () => P.find().$promise
        .then(items => l.checkAvailables(items, mForm.clients, 'typeId'))
        .then(results => mForm.persons = results);

      mForm.loadCompanies = () => C.find({filter: {
          where: {main: {neq: true}}
        }}).$promise
        .then(items => l.checkAvailables(items, mForm.clients, 'typeId'))
        .then(results => mForm.companies = results);

      mForm.save = (form) => l.saveItem({
        Model: M,
        form: form,
        mForm: mForm,
        modelName: moduleName,
        upsertItem: (mForm.form.clientType === 0 && mForm.personaId === '0') ||
          (mForm.form.clientType === 1 && mForm.companyId === '0') ||
          mForm.update,

        dataToSend: mForm.form.clientType === 0 && mForm.personaId !== '0'
          && !mForm.update ? {
            type: 'person',
            typeId: mForm.personaId
          } :
          mForm.form.clientType === 1 && mForm.companyId !== '0'  &&
          !mForm.update ? {
            type: 'company',
            typeId: mForm.companyId
          } :
          false,
        formSuccess: () => l.closeSidenav('right')
      });

    }
  })]);
