diff --git a/awx/ui/client/lib/components/index.js b/awx/ui/client/lib/components/index.js index 8080175627..1e5767fd36 100644 --- a/awx/ui/client/lib/components/index.js +++ b/awx/ui/client/lib/components/index.js @@ -42,6 +42,7 @@ import toolbar from '~components/list/list-toolbar.directive'; import topNavItem from '~components/layout/top-nav-item.directive'; import truncate from '~components/truncate/truncate.directive'; import atCodeMirror from '~components/code-mirror'; +import atSyntaxHighlight from '~components/syntax-highlight'; import card from '~components/cards/card.directive'; import cardGroup from '~components/cards/group.directive'; import atSwitch from '~components/switch/switch.directive'; @@ -54,7 +55,8 @@ const MODULE_NAME = 'at.lib.components'; angular .module(MODULE_NAME, [ atLibServices, - atCodeMirror + atCodeMirror, + atSyntaxHighlight, ]) .directive('atActionGroup', actionGroup) .directive('atActionButton', actionButton) diff --git a/awx/ui/client/lib/components/syntax-highlight/index.js b/awx/ui/client/lib/components/syntax-highlight/index.js new file mode 100644 index 0000000000..a87b69923f --- /dev/null +++ b/awx/ui/client/lib/components/syntax-highlight/index.js @@ -0,0 +1,10 @@ +// import 'codemirror/mode/jinja2/jinja2'; +import syntaxHighlight from './syntax-highlight.directive'; +// import strings from './syntax-highlight.strings'; + +const MODULE_NAME = 'at.syntax.highlight'; + +angular.module(MODULE_NAME, []) + .directive('atSyntaxHighlight', syntaxHighlight); + +export default MODULE_NAME; diff --git a/awx/ui/client/lib/components/syntax-highlight/syntax-highlight.directive.js b/awx/ui/client/lib/components/syntax-highlight/syntax-highlight.directive.js new file mode 100644 index 0000000000..08d37ee81a --- /dev/null +++ b/awx/ui/client/lib/components/syntax-highlight/syntax-highlight.directive.js @@ -0,0 +1,83 @@ +const templateUrl = require('~components/syntax-highlight/syntax-highlight.partial.html'); + +function atSyntaxHighlightController ($scope, AngularCodeMirror) { + const vm = this; + const variablesName = `${$scope.name}_codemirror`; + + function init () { + if ($scope.disabled === 'true') { + $scope.disabled = true; + } else if ($scope.disabled === 'false') { + $scope.disabled = false; + } + // TODO: get default value + $scope.codeMirrorValue = $scope.codeMirrorValue || ''; + $scope.parseType = 'jinja2'; + + $scope.variablesName = variablesName; + $scope[variablesName] = $scope.codeMirrorValue; + const codeMirror = AngularCodeMirror($scope.disabled); + codeMirror.addModes({ + jinja2: { + mode: 'text/x-jinja2', + matchBrackets: true, + autoCloseBrackets: true, + styleActiveLine: true, + lineNumbers: true, + gutters: ['CodeMirror-lint-markers'], + lint: true, + scrollbarStyle: null, + } + }); + // codeMirror.fromTextArea(document.getElementById(`${$scope.name}_codemirror`)); + codeMirror.showTextArea({ + scope: $scope, + model: variablesName, + element: `${$scope.name}_codemirror`, + lineNumbers: true, + mode: 'jinja2', + }); + + $scope.$watch(variablesName, () => { + $scope.codeMirrorValue = $scope[variablesName]; + }); + } + + vm.name = $scope.name; + vm.variablesName = variablesName; + vm.parseType = $scope.parseType; + if ($scope.init) { + $scope.init = init; + } + angular.element(document).ready(() => { + init(); + }); +} + +atSyntaxHighlightController.$inject = [ + '$scope', + 'AngularCodeMirror' +]; + +function atCodeMirrorTextarea () { + return { + restrict: 'E', + replace: true, + transclude: true, + templateUrl, + controller: atSyntaxHighlightController, + controllerAs: 'vm', + scope: { + disabled: '@', + label: '@', + labelClass: '@', + tooltip: '@', + tooltipPlacement: '@', + variables: '=', + name: '@', + init: '=' + } + }; +} + +export default atCodeMirrorTextarea; diff --git a/awx/ui/client/lib/components/syntax-highlight/syntax-highlight.partial.html b/awx/ui/client/lib/components/syntax-highlight/syntax-highlight.partial.html new file mode 100644 index 0000000000..26f99cb7cb --- /dev/null +++ b/awx/ui/client/lib/components/syntax-highlight/syntax-highlight.partial.html @@ -0,0 +1,30 @@ +