UNPKG

ng2-json-editor

Version:

Angular2 component for editing large json documents

221 lines 10.7 kB
/* * This file is part of ng2-json-editor. * Copyright (C) 2016 CERN. * * ng2-json-editor is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * ng2-json-editor is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ng2-json-editor; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * In applying this license, CERN does not * waive the privileges and immunities granted to it by virtue of its status * as an Intergovernmental Organization or submit itself to any jurisdiction. */ import { Injectable } from '@angular/core'; import { List } from 'immutable'; import { EmptyValueService } from './empty-value.service'; import { JsonStoreService } from './json-store.service'; import { JsonSchemaService } from './json-schema.service'; import { DomUtilService } from './dom-util.service'; import { PathUtilService } from './path-util.service'; import { KeysStoreService } from './keys-store.service'; var ShortcutActionService = (function () { function ShortcutActionService(emptyValueService, domUtilService, jsonStoreService, jsonSchemaService, pathUtilService, keysStoreService) { this.emptyValueService = emptyValueService; this.domUtilService = domUtilService; this.jsonStoreService = jsonStoreService; this.jsonSchemaService = jsonSchemaService; this.pathUtilService = pathUtilService; this.keysStoreService = keysStoreService; } ShortcutActionService.prototype.addToRootAction = function (path) { this.add(path, true); }; ShortcutActionService.prototype.addAction = function (path) { this.add(path, false); }; ShortcutActionService.prototype.add = function (path, root) { var _path = this.pathUtilService.getNearestOrRootArrayParentInPath(path, root); this.addNewElementInArray(_path, this.jsonSchemaService.forPathArray(_path)); }; /** * @param path - Path of the array parent that the element is about to be inserted * @param schema - Schema of the element that is about to be inserted */ ShortcutActionService.prototype.addNewElementInArray = function (path, schema) { var itemSchema = schema.items; var emptyValue = this.emptyValueService.generateEmptyValue(itemSchema); var values = this.jsonStoreService.getIn(path) || List(); this.jsonStoreService.setIn(path, values.push(emptyValue)); path.push(values.size); this.waitThenFocus(this.pathUtilService.toPathString(path)); }; ShortcutActionService.prototype.addBelowToRootAction = function (path) { var rootPath = this.pathUtilService.getNearestOrRootArrayParentInPath(path, true); var schema = this.jsonSchemaService.forPathArray(rootPath); var itemSchema = schema.items; var emptyValue = this.emptyValueService.generateEmptyValue(itemSchema); var values = this.jsonStoreService.getIn(rootPath) || List(); this.jsonStoreService.setIn(rootPath, values.insert(path[1] + 1, emptyValue)); rootPath.push(path[1] + 1); this.waitThenFocus(this.pathUtilService.toPathString(rootPath)); }; ShortcutActionService.prototype.moveUpAction = function (path) { this.move(path, -1); }; ShortcutActionService.prototype.moveDownAction = function (path) { this.move(path, 1); }; ShortcutActionService.prototype.moveUpRootAction = function (path) { this.move(path, -1, true); }; ShortcutActionService.prototype.moveDownRootAction = function (path) { this.move(path, 1, true); }; /** * @param path - Path of the element that is moved * @param direction - Movement direction. -1 for UP, +1 for DOWN */ ShortcutActionService.prototype.move = function (path, direction, root) { if (root === void 0) { root = false; } this.domUtilService.blurFirstEditableChildById(this.pathUtilService.toPathString(path)); var index = this.pathUtilService.getElementIndexInForwardOrReversePath(path, root); path = this.jsonStoreService.moveIn(this.pathUtilService.getNearestOrRootArrayParentInPath(path, root), index, direction); var pathString = this.pathUtilService.toPathString(path); this.waitThenFocus(pathString); }; ShortcutActionService.prototype.deleteAction = function (path) { // blur element before delete for ensuring that `commitValueChange` will not show again the deleted value this.domUtilService.blurFirstEditableChildById(this.pathUtilService.toPathString(path)); this.deleteElement(this.pathUtilService.getNearestOrRootArrayParentInPath(path, false), this.pathUtilService.getElementIndexInForwardOrReversePath(path, false)); }; /** * @param path - Path of the element's array parent * @param index - Index of the element that is deleted from array parent path */ ShortcutActionService.prototype.deleteElement = function (path, index) { var values = this.jsonStoreService.getIn(path); this.jsonStoreService.setIn(path, values.remove(index)); }; ShortcutActionService.prototype.navigateUpAction = function (path) { this.navigateUpDown(path, -1); }; ShortcutActionService.prototype.navigateDownAction = function (path) { this.navigateUpDown(path, 1); }; /** * @param path - Path of the element that is focused * @param direction - Navigation direction. -1 for UP, +1 for DOWN */ ShortcutActionService.prototype.navigateUpDown = function (path, direction) { var values = this.jsonStoreService.getIn(this.pathUtilService.getNearestOrRootArrayParentInPath(path, false)); if (List.isList(values)) { var elemIndexInPath = this.pathUtilService.getElementIndexInForwardOrReversePath(path, true); if ((elemIndexInPath + direction) < values.size && (elemIndexInPath + direction) >= 0) { path[path.length - 2] = elemIndexInPath + direction; } else { path[path.length - 2] = values.size - Math.abs((elemIndexInPath + direction)); } var pathString = this.pathUtilService.toPathString(path); this.domUtilService.focusAndSelectFirstEditableChildById(pathString); } }; ShortcutActionService.prototype.navigateLeftAction = function (path) { this.navigateRightLeft(path, -1); }; ShortcutActionService.prototype.navigateRightAction = function (path) { this.navigateRightLeft(path, 1); }; /** * @param path - Path of the element that is focused * @param direction - Navigation direction. -1 for LEFT, +1 for RIGHT */ ShortcutActionService.prototype.navigateRightLeft = function (path, direction) { var pathString = this.pathUtilService.toPathString(path); this.domUtilService.focusRightOrLeftParentCell(pathString, direction); }; /** * Copies the current row in table below and sets the value of the previous focused field to empty in the new row * @param path - Path */ ShortcutActionService.prototype.copyAction = function (path) { this.copyRowOrSchemaBelow(path, false); }; /** * Copies the root parent element below(eg creates a new author in authors list) * when you edit an author's field) * @param path - Path */ ShortcutActionService.prototype.copyFromRootAction = function (path) { this.copyRowOrSchemaBelow(path, true); }; /** * @param originalPath - Path of the element that is copied * @param root - Copy item from parent or root. Set to true for usage as in `copyFromRootAction` and false * for usage as in `copyAction` */ ShortcutActionService.prototype.copyRowOrSchemaBelow = function (originalPath, root) { var arrayParentPath = this.pathUtilService.getNearestOrRootArrayParentInPath(originalPath, root); if (this.jsonSchemaService.forPathArray(arrayParentPath)['items'].hasOwnProperty('properties')) { var elemIndex = this.pathUtilService.getElementIndexInForwardOrReversePath(originalPath, root); var valuesList = this.jsonStoreService.getIn(arrayParentPath) || List(); var newValue = valuesList.get(elemIndex); var newPath = arrayParentPath.concat(elemIndex + 1); var newPathString = this.pathUtilService.toPathString(newPath); if (!root) { newValue = newValue.set(originalPath[originalPath.length - 1]); newPathString = "" + newPathString + this.pathUtilService.separator + originalPath[originalPath.length - 1]; } this.jsonStoreService.setIn(arrayParentPath, valuesList.insert(elemIndex + 1, newValue)); this.waitThenFocus(newPathString); } }; ShortcutActionService.prototype.undoAction = function () { var rolledBackPath = this.jsonStoreService.rollbackLastChange(); if (rolledBackPath) { this.waitThenFocus(rolledBackPath); } }; ShortcutActionService.prototype.waitThenFocus = function (path) { var _this = this; setTimeout(function () { _this.domUtilService.focusAndSelectFirstEditableChildById(path, true); }); }; ShortcutActionService.prototype.generateShortcutAction = function (actionName) { var _this = this; return function (event) { event.preventDefault(); var eventTarget = event.target; var pathString = eventTarget.getAttribute('data-path'); if (pathString) { _this[actionName](_this.pathUtilService.toPathArray(pathString)); } return false; }; }; return ShortcutActionService; }()); export { ShortcutActionService }; ShortcutActionService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ ShortcutActionService.ctorParameters = function () { return [ { type: EmptyValueService, }, { type: DomUtilService, }, { type: JsonStoreService, }, { type: JsonSchemaService, }, { type: PathUtilService, }, { type: KeysStoreService, }, ]; }; //# sourceMappingURL=shortcut-action.service.js.map