link: function()

in public/components/editable-field/editable-field.js [162:241]


        link: function($scope, $element, $attrs, [ ngModel, wfEditable ]) {
            $attrs.$addClass('editable__text-field');
            if ($scope.editableType === 'textarea') {
                $attrs.$addClass('editable__text-field--textarea');
            }

            // resets / sets the ng-model-options (prevents default behaviour)
            ngModel.$options = ngModel.$options || {};

            function commit() {
                var newValue = ngModel.$viewValue,
                    oldValue = ngModel.$modelValue;

                // TODO: could check for promise from onEditableUpdate to
                //       display loader, before committing view value.

                ngModel.$commitViewValue();

                wfEditable.setErrors(ngModel.$error);

                if (ngModel.$valid) {
                    $scope.onEditableUpdate({
                        newValue: newValue,
                        oldValue: oldValue
                    });
                    if ($scope.noCloseMode) {

                        // reset input
                        $element[0].value = '';

                    } else {
                        wfEditable.setEditMode(false);
                    }

                    ngModel.$setUntouched();
                    ngModel.$setPristine();
                }
            }

            function cancel() {
                $scope.onEditableCancel();
                ngModel.$rollbackViewValue();
                wfEditable.setErrors(ngModel.$error);
                wfEditable.setEditMode(false);
            }

            function implicitCancel() {
                if (ngModel.$viewValue === ngModel.$modelValue) {
                    wfEditable.setEditMode(false);
                } else {
                    wfEditable.setErrors({ notSaved: true });
                }
            }

            $scope.$on('wfEditable.commit', commit);
            $scope.$on('wfEditable.cancel', cancel);
            $scope.$on('wfEditable.implicitCancel', implicitCancel);

            $scope.$on('wfEditable.changedEditMode', ($event, mode) => {
                if (mode === true) {
                    $timeout(() => $element[0].select(), 100);
                }
            });

            $element.on('keydown', ($event) => {
                if ($event.keyCode === KEYCODE_ESC) {
                    $scope.$apply(cancel);

                } else if ($event.keyCode === KEYCODE_ENTER) {
                    if ($scope.editableType === 'textarea') {
                        if ($event.metaKey || $event.ctrlKey || $event.altKey) {
                            $scope.$apply(commit);
                        }

                    } else {
                        $scope.$apply(commit);
                    }
                }
            });
        }