'use strict';

angular.module('arethusaTranslateGuiApp').directive('trslValue', [
  function() {
    return {
      restrict: 'A',
      link: function(scope, element) {
        var DIRTY = 'dirty-bg';
        var CLEAN = 'clean-bg';

        function switchClassAndNotify(newVal) {
          scope.statusClass = newVal ? DIRTY : CLEAN;
        }

        scope.$watch('value.dirty', switchClassAndNotify);

        scope.$on('mainChange', function() {
          scope.value.dirty = true;
          scope.$broadcast('mainDirty');
          scope.deferredUpdate();
        });

        scope.$on('trslChange', function() {
          if (scope.allClean(scope.value.translations)) {
            scope.value.dirty = false;
          } else {
            scope.value.dirty = true;
          }
        });

        function changeAllStatus(bool) {
          var trsls = scope.value.translations;
          for (var i = trsls.length - 1; i >= 0; i--){
            var trsl = trsls[i];
            trsl.dirty = bool;
          }
          emit();
        }

        function emit() {
          scope.$emit('trslChange');
        }

        scope.setDirty = function() {
          changeAllStatus(true);
        };

        scope.setClean = function() {
          changeAllStatus(false);
        };

        scope.removeConfirmation = function() {
          // Should ask if user is sure he wants to do that
          var values = scope.container.values;
          var i = values.indexOf(scope.value);
          values.splice(i, 1);
          emit();
          scope.deferredUpdate();
        };
      },
      templateUrl: 'app/i18n/trsl_value.directive.html'
    };
  }
]);



@@ -353,68 +353,148 @@
     
-scope.$watch('value.dirty', switchClassAndNotify);%0A%0A
+function isClean() %7B%0A          return scope.allClean(scope.value, 'translations');%0A        %7D%0A%0A        function checkDirtyness() %7B%0A  
         
 scop
@@ -493,153 +493,146 @@
     
-s
 co
-pe.$on('mainChange', function() %7B%0A          scope.value.dirty = true;%0A          scope.$broadcast('mainDirty');%0A          scope.deferredUpdate(
+nsole.log('checking');%0A          scope.value.dirty = !isClean();%0A        %7D%0A%0A        function emit() %7B%0A          scope.$emit('trslChange'
 );%0A 
@@ -631,34 +631,32 @@
 nge');%0A        %7D
-);
 %0A%0A        scope.
@@ -660,107 +660,178 @@
 pe.$
-on('trslChange', function() %7B%0A          if (scope.allClean(scope.value.translations)) %7B%0A           
+watch('value.dirty', switchClassAndNotify);%0A%0A        scope.$watch('value.name', function(newVal, oldVal) %7B%0A          if (!oldVal) checkDirtyness();%0A          if (!newVal)
  sco
@@ -847,20 +847,19 @@
 dirty = 
-fals
+tru
 e;%0A     
@@ -865,21 +865,58 @@
     
-  %7D else
+%7D);%0A%0A        scope.$on('mainChange', function()
  %7B%0A
-  
     
@@ -961,19 +961,132 @@
     
-%7D%0A        %7D
+scope.$broadcast('mainDirty');%0A          scope.deferredUpdate();%0A        %7D);%0A%0A        scope.$on('trslChange', checkDirtyness
 );%0A%0A
@@ -1309,63 +1309,8 @@
   %7D%0A
-          emit();%0A        %7D%0A%0A        function emit() %7B%0A
     

Require a name in trslValue