{"size": 2293, "ext": "js", "max_stars_repo_path": "src/client/app/components/product-form/product-form.directive.js", "max_stars_repo_name": "dcallner/project-jellyshit-api", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/client/app/components/product-form/product-form.directive.js", "max_issues_repo_name": "dcallner/project-jellyshit-api", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/client/app/components/product-form/product-form.directive.js", "max_forks_repo_name": "dcallner/project-jellyshit-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.3936170213, "max_line_length": 88, "alphanum_fraction": 0.5730484082}
(function() {
  'use strict';

  angular.module('app.components')
    .directive('productForm', ProductFormDirective);

  /** @ngInject */
  function ProductFormDirective() {
    var directive = {
      restrict: 'AE',
      scope: {
        heading: '@?',
        product: '=',
        productType: '='
      },
      link: link,
      templateUrl: 'app/components/product-form/product-form.html',
      controller: ProductFormController,
      controllerAs: 'vm',
      bindToController: true
    };

    return directive;

    function link(scope, element, attrs, vm, transclude) {
      vm.activate();
    }

    /** @ngInject */
    function ProductFormController($scope, $state, Tag, Toasts, TAG_QUERY_LIMIT) {
      var vm = this;

      var showValidationMessages = false;
      var home = 'admin.products';

      vm.activate = activate;
      vm.backToList = backToList;
      vm.queryTags = queryTags;
      vm.showErrors = showErrors;
      vm.hasErrors = hasErrors;
      vm.onSubmit = onSubmit;

      function activate() {
        vm.heading = vm.heading || 'Add A Product';
      }

      function backToList() {
        $state.go(home);
      }

      function queryTags(query) {
        return Tag.query({q: query, limit: TAG_QUERY_LIMIT}).$promise;
      }

      function showErrors() {
        return showValidationMessages;
      }

      function hasErrors(field) {
        if (angular.isUndefined(field)) {
          return showValidationMessages && vm.form.$invalid;
        }

        return showValidationMessages && vm.form[field].$invalid;
      }

      function onSubmit() {
        showValidationMessages = true;
        // This is so errors can be displayed for 'untouched' angular-schema-form fields
        $scope.$broadcast('schemaFormValidate');

        if (vm.form.$valid) {
          if (vm.product.id) {
            vm.product.$update(saveSuccess, saveFailure);
          } else {
            vm.product.$save(saveSuccess, saveFailure);
          }
        }

        return false;

        function saveSuccess() {
          Toasts.toast(vm.product.name + ' saved to products.');
          $state.go(home);
        }

        function saveFailure() {
          Toasts.error('Server returned an error while saving.');
        }
      }
    }
  }
})();
