(function () { 'use strict'; var name = 'adNodeMetaView'; angular.module('app.directive') .directive('adNodeMetaView', ['$parse', function ($parse) { var fieldMap = { added: { added: 'date', addedIp: 'ip', addedUserId: 'userId', addedUserName: 'userName' }, changed: { changed: 'date', changedIp: 'ip', changedUserId: 'userId', changedUserName: 'userName' } }; return { replace: true, templateUrl: 'directive/' + name + '/' + name + '.html', scope: {}, link: function (scope, element, attr) { scope.data = null; var mode = attr[name + 'Mode']; if (!fieldMap.hasOwnProperty(mode)) { throw new Error('[app.directive.adNodeMetaView] wrong attr ad-node-meta-view-mode "' + mode + '"'); } var expr = $parse(attr[name]); var extractData = function (source, mapping) { var src, result = {}; for (src in mapping) { if (mapping.hasOwnProperty(src) && source.hasOwnProperty(src)) { result[mapping[src]] = source[src]; } } return result; }; scope.$parent.$watch(function () { return expr(scope.$parent); }, function (value) { if (value) { scope.data = extractData(value, fieldMap[mode]); } }); } }; }]); })();