"use strict";

// Generators for Arethusa code for things such as
// - useful directives

angular.module('arethusa.util').service('generator', [
  function ArethusaGenerator() {
    this.panelTrigger = function (conf) {
      return {
        restrict: 'A',
        compile: function(element) {
          var hint;

          function updateTitle(translation) {
            var title = translation();
            if (hint) title += ' (' + hint + ')';
            element.attr('title', title);
          }

          return function link(scope, element, attrs) {
            function executeToggle() {
              element.toggleClass('on');
              conf.service.toggle();
            }

            function toggle() {
              // Need to check for a running digest. When we trigger this
              // function through a hotkey, the keyCapture service will
              // have launched a digest already.
              if (scope.$$phase) {
                executeToggle();
              } else {
                scope.$apply(executeToggle);
              }
            }

            conf.trsl(conf.trslKey, updateTitle);

            element.bind('click', toggle);

            if (conf.kC) {
              var keys = conf.kC.initCaptures(function(kC) {
                var mapping = {};
                mapping[conf.mapping.name] = [
                  kC.create('toggle', function() { toggle(); }, conf.mapping.key)
                ];
                return mapping;
              });

              hint = keys[conf.mapping.name].toggle;
            }
          };
        },
        template: conf.template
      };
    };

    this.historyTrigger = function (history, translator, type, icon) {
      // type is either undo or redo
      icon = icon || type;
      return {
        restrict: 'A',
        scope: {},
        link: function(scope, element, attrs) {
          scope.history = history;

          scope.$watch('history.mode', function(newVal, oldVal) {
            if (newVal === 'editor') {
              element.show();
            } else {
              element.hide();
            }
          });

          scope.$watch('history.can' + aU.capitalize(type), function(newVal, oldVal) {
            if (newVal) {
              element.removeClass('disabled');
            } else {
              element.addClass('disabled');
            }
          });

          element.bind('click', function() {
            scope.$apply(history[type]());
          });


          var trsl, hint;

          scope.$on('keysAdded', function(_, keys) {
            var sel = keys.history;
            if (sel) {
              hint = aU.formatKeyHint(sel[type]);
              setTitle();
            }
          });

          translator('history.' + type, function(translation) {
            trsl = translation();
            setTitle();
          });

          function setTitle() {
            element.attr('title', trsl + ' ' + hint);
          }
        },
        template: '<i class="fa fa-' + icon + '"/>'
      };
    };
  }
]);


@@ -2534,22 +2534,42 @@
 pe.$
-on('keysAdded'
+watch('history.activeKeys.' + type
 , fu
@@ -2579,55 +2579,15 @@
 ion(
-_, 
 key
-s
 ) %7B%0A
-            var sel = keys.history;%0A
     
@@ -2598,19 +2598,19 @@
     if (
-sel
+key
 ) %7B%0A    
@@ -2647,17 +2647,11 @@
 int(
-sel%5Btype%5D
+key
 );%0A 

Fix key hint for undo/redo directives