UNPKG

nakedobjects.spa

Version:

Single Page Application client for a Naked Objects application.

150 lines 7.27 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import { Injectable } from '@angular/core'; import * as Msg from './user-messages'; import { ContextService } from './context.service'; import { UrlManagerService } from './url-manager.service'; import { MaskService } from './mask.service'; import { ErrorService } from './error.service'; import { ConfigService } from './config.service'; import { Location } from '@angular/common'; import { CiceroContextService } from './cicero-context.service'; import { Action } from './cicero-commands/action'; import { Back } from './cicero-commands/back'; import { Cancel } from './cicero-commands/cancel'; import { Clipboard } from './cicero-commands/clipboard'; import { Edit } from './cicero-commands/edit'; import { Enter } from './cicero-commands/enter'; import { Forward } from './cicero-commands/forward'; import { Gemini } from './cicero-commands/gemini'; import { Goto } from './cicero-commands/goto'; import { Help } from './cicero-commands/help'; import { Menu } from './cicero-commands/menu'; import { OK } from './cicero-commands/ok'; import { Page } from './cicero-commands/page'; import { Reload } from './cicero-commands/reload'; import { Root } from './cicero-commands/root'; import { Save } from './cicero-commands/save'; import { Selection } from './cicero-commands/selection'; import { Show } from './cicero-commands/show'; import { Where } from './cicero-commands/where'; import { Result } from './cicero-commands/result'; import filter from 'lodash/filter'; import reduce from 'lodash/reduce'; import last from 'lodash/last'; import map from 'lodash/map'; import fromPairs from 'lodash/fromPairs'; import { CiceroRendererService } from './cicero-renderer.service'; var ParseResult = (function () { function ParseResult() { } return ParseResult; }()); export { ParseResult }; ParseResult.create = function (commands) { return ({ commands: commands }); }; ParseResult.createError = function (msg) { return ({ error: msg }); }; var CiceroCommandFactoryService = (function () { function CiceroCommandFactoryService(urlManager, location, context, mask, error, configService, ciceroContext, ciceroRenderer) { var _this = this; this.urlManager = urlManager; this.location = location; this.context = context; this.mask = mask; this.error = error; this.configService = configService; this.ciceroContext = ciceroContext; this.ciceroRenderer = ciceroRenderer; this.commandsInitialised = false; this.commandTypes = [Action, Back, Cancel, Clipboard, Edit, Enter, Forward, Gemini, Goto, Help, Menu, OK, Page, Reload, Root, Save, Selection, Show, Where]; this.allCommands = map(this.commandTypes, function (T) { return new T(_this.urlManager, _this.location, _this, _this.context, _this.mask, _this.error, _this.configService, _this.ciceroContext, _this.ciceroRenderer); }); this.commands = fromPairs(map(this.allCommands, function (c) { return [c.shortCommand, c]; })); this.getSingleCommand = function (input, chained) { input = input.trim(); var firstWord = input.split(" ")[0]; var command = _this.getCommand(firstWord); command.argString = _this.getArgs(input); command.chained = chained; return command; }; //TODO: could do more than auto complete e.g. reject unrecognised action or one not available in context. this.preParse = function (input) { if (!input) { return Result.create(input, null); } var lastInChain = last(input.split(";")).toLowerCase(); var charsTyped = lastInChain.length; lastInChain = lastInChain.trim(); if (lastInChain.length === 0 || lastInChain.indexOf(" ") >= 0) { return Result.create(input + " ", null); } try { var command = _this.getCommand(lastInChain); var earlierChain = input.substr(0, input.length - charsTyped); return Result.create("" + earlierChain + command.fullCommand + " ", null); } catch (e) { return Result.create("", e.message); } }; this.getCommand = function (commandWord) { if (commandWord.length < 2) { throw new Error(Msg.commandTooShort); } commandWord = commandWord.toLowerCase(); var abbr = commandWord.substr(0, 2); var command = _this.commands[abbr]; if (command == null) { throw new Error(Msg.noCommandMatch(abbr)); } command.checkMatch(commandWord); return command; }; this.allCommandsForCurrentContext = function () { var commandsInContext = filter(_this.commands, function (c) { return c.isAvailableInCurrentContext(); }); return reduce(commandsInContext, function (r, c) { return "" + r + c.fullCommand + "\n"; }, Msg.commandsAvailable); }; } CiceroCommandFactoryService.prototype.mapInputToCommands = function (input) { var _this = this; if (!input) { //Special case for hitting Enter with no input return [this.getCommand("wh")]; } var commands = input.split(";"); return map(commands, function (c) { return _this.getSingleCommand(c, commands.length > 1); }); }; CiceroCommandFactoryService.prototype.getCommands = function (input) { try { return ParseResult.create(this.mapInputToCommands(input)); } catch (e) { return ParseResult.createError(e.message); } }; ; CiceroCommandFactoryService.prototype.getArgs = function (input) { var index = input.indexOf(" "); return index >= 0 ? input.substr(index + 1) : null; }; return CiceroCommandFactoryService; }()); CiceroCommandFactoryService = __decorate([ Injectable(), __metadata("design:paramtypes", [UrlManagerService, Location, ContextService, MaskService, ErrorService, ConfigService, CiceroContextService, CiceroRendererService]) ], CiceroCommandFactoryService); export { CiceroCommandFactoryService }; //# sourceMappingURL=cicero-command-factory.service.js.map