import angular from 'angular'; import Clipboard from 'clipboard'; import coreModule from '../core_module'; import kbn from 'app/core/utils/kbn'; /** @ngInject */ function tip($compile) { return { restrict: 'E', link: function(scope, elem, attrs) { var _t = ''; _t = _t.replace(/{/g, '\\{').replace(/}/g, '\\}'); elem.replaceWith($compile(angular.element(_t))(scope)); }, }; } function clipboardButton() { return { scope: { getText: '&clipboardButton', }, link: function(scope, elem) { scope.clipboard = new Clipboard(elem[0], { text: function() { return scope.getText(); }, }); scope.$on('$destroy', function() { if (scope.clipboard) { scope.clipboard.destroy(); } }); }, }; } /** @ngInject */ function compile($compile) { return { restrict: 'A', link: function(scope, element, attrs) { scope.$watch( function(scope) { return scope.$eval(attrs.compile); }, function(value) { element.html(value); $compile(element.contents())(scope); } ); }, }; } function watchChange() { return { scope: { onchange: '&watchChange' }, link: function(scope, element) { element.on('input', function() { scope.$apply(function() { scope.onchange({ inputValue: element.val() }); }); }); }, }; } /** @ngInject */ function editorOptBool($compile) { return { restrict: 'E', link: function(scope, elem, attrs) { var ngchange = attrs.change ? ' ng-change="' + attrs.change + '"' : ''; var tip = attrs.tip ? ' ' + attrs.tip + '' : ''; var showIf = attrs.showIf ? ' ng-show="' + attrs.showIf + '" ' : ''; var template = '
' + ' ' + '' + ' '; elem.replaceWith($compile(angular.element(template))(scope)); }, }; } /** @ngInject */ function editorCheckbox($compile, $interpolate) { return { restrict: 'E', link: function(scope, elem, attrs) { var text = $interpolate(attrs.text)(scope); var model = $interpolate(attrs.model)(scope); var ngchange = attrs.change ? ' ng-change="' + attrs.change + '"' : ''; var tip = attrs.tip ? ' ' + attrs.tip + '' : ''; var label = ''; var template = '' + ' '; template = template + label; elem.addClass('gf-form-checkbox'); elem.html($compile(angular.element(template))(scope)); }, }; } /** @ngInject */ function gfDropdown($parse, $compile, $timeout) { function buildTemplate(items, placement?) { var upclass = placement === 'top' ? 'dropup' : ''; var ul = [ '', ]; for (let index = 0; index < items.length; index++) { let item = items[index]; if (item.divider) { ul.splice(index + 1, 0, '
  • '); continue; } var li = '' + '' + (item.text || '') + ''; if (item.submenu && item.submenu.length) { li += buildTemplate(item.submenu).join('\n'); } li += ''; ul.splice(index + 1, 0, li); } return ul; } return { restrict: 'EA', scope: true, link: function postLink(scope, iElement, iAttrs) { var getter = $parse(iAttrs.gfDropdown), items = getter(scope); $timeout(function() { var placement = iElement.data('placement'); var dropdown = angular.element( buildTemplate(items, placement).join('') ); dropdown.insertAfter(iElement); $compile(iElement.next('ul.dropdown-menu'))(scope); }); iElement.addClass('dropdown-toggle').attr('data-toggle', 'dropdown'); }, }; } coreModule.directive('tip', tip); coreModule.directive('clipboardButton', clipboardButton); coreModule.directive('compile', compile); coreModule.directive('watchChange', watchChange); coreModule.directive('editorOptBool', editorOptBool); coreModule.directive('editorCheckbox', editorCheckbox); coreModule.directive('gfDropdown', gfDropdown);