Source: volume/editLinks.js

// Generated by CoffeeScript 1.12.7

/**
 * The volume links tab functionalities
 * @module
 */
'use strict';
app.directive('volumeEditLinksForm', [
  'constantService', 'messageService', function(constants, messages) {
    return {
      restrict: 'E',
      templateUrl: 'volume/editLinks.html',
      link: function($scope) {
        var blank, form, update, volume;
        volume = $scope.volume;
        form = $scope.volumeEditLinksForm;
        blank = function() {
          return form.data.push({
            head: '',
            url: ''
          });
        };
        update = function() {
          form.data = _.map(volume.links, function(ref) {
            return {
              head: ref.head,
              url: ref.url
            };
          });
          return blank();
        };
        update();
        form.change = function() {
          if (form.data[form.data.length - 1].url !== '') {
            blank();
          }
          return form.validator.server({});
        };
        form.remove = function(ref, i) {
          ref.removed = true;
          ref.head = '';
          ref.url = '';
          return $scope.removed = true;
        };
        form.save = function() {
          var data;
          messages.clear(form);
          data = _.filter(form.data, function(ref) {
            return !ref.removed && (ref.head || ref.url);
          });
          form.$setSubmitted();
          return volume.saveLinks(data).then(function() {
            form.validator.server({});
            update();
            messages.add({
              type: 'green',
              body: constants.message('volume.edit.success'),
              owner: form
            });
            delete $scope.removed;
            form.$setPristine();
          }, function(res) {
            form.$setUnsubmitted();
            form.validator.server(res);
            messages.addError({
              body: constants.message('volume.edit.error'),
              report: res,
              owner: form
            });
          });
        };
      }
    };
  }
]);

//# sourceMappingURL=editLinks.js.map