var angular = require('angular');
require('../app').controller('NomenclaturesController', /*@ngInject*/function ($scope, $state, $stateParams, db, Nomenclature) {
  var $ctrl = this;

  $ctrl.nomenclatures = db.nomenclatures;
  $ctrl.groups = {};
  angular.forEach(db.nomenclatures, function (nomenclature, key) {
    var group = key.split('_', 2)[0];
    var name = key.split('_', 2)[1];
    $ctrl.groups[group] = $ctrl.groups[group] || {};
    $ctrl.groups[group][name] = nomenclature;

    if (group === $stateParams.group && name === $stateParams.nomenclature) {
      $ctrl.selected = $ctrl.selected || {};
      $ctrl.selected.group = $ctrl.selected.group || {};
      $ctrl.selected.group[group] = true;
      $ctrl.selected.name = name;
      $ctrl.selected.type = key;
      $ctrl.selected.nomenclature = [];
      angular.forEach(nomenclature, function (item) {
        $ctrl.selected.nomenclature.push(angular.copy(item));
      });
    }
  });

  $ctrl.remove = function (idx) {
    $ctrl.selected.nomenclature.splice(idx, 1);
    $scope.editform.$setDirty();
  };

  $ctrl.add = function (key) {
    $ctrl.selected.nomenclature.push(new Nomenclature({
      type: $ctrl.selected.type,
    }));
    $scope.editform.$setDirty();
  };

  $ctrl.save = function () {
    if ($scope.editform.$invalid) return;
    if (!$ctrl.selected.nomenclature.length) {
      return;
    }
    Nomenclature.updateGroup({type: $ctrl.selected.type}, {items: $ctrl.selected.nomenclature})
      .$promise.then(function (items) {
      $scope.editform.$setPristine();
      db.$updateNomenclatures();
    });
  };
});


@@ -315,21 +315,21 @@
     var 
-group
+parts
  = key.s
@@ -340,52 +340,72 @@
 ('_'
-, 2)%5B0%5D;%0A    var name = key.split('_', 2)%5B1%5D
+);%0A    var group = parts.shift();%0A    var name = parts.join('_')
 ;%0A  

fix nomenclature editor support for multi_dashed names