From b0103cb17821622826c73b828727d5acc1fc61b6 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sat, 11 Apr 2015 23:48:41 -0600 Subject: [PATCH] * Experimental enter/leave events for Views --- core/edit_text_view.js | 1 + core/text_view.js | 15 ++++++++++++++- core/view.js | 16 ++++++++++++++++ core/view_controller.js | 4 ++++ mods/apply.js | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) diff --git a/core/edit_text_view.js b/core/edit_text_view.js index c771c5bf..1c836dba 100644 --- a/core/edit_text_view.js +++ b/core/edit_text_view.js @@ -12,6 +12,7 @@ exports.EditTextView = EditTextView; function EditTextView(options) { options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true); options.acceptsInput = miscUtil.valueWithDefault(options.acceptsInput, true); + options.resizable = false; TextView.call(this, options); diff --git a/core/text_view.js b/core/text_view.js index f09596e7..9bd290b0 100644 --- a/core/text_view.js +++ b/core/text_view.js @@ -22,6 +22,7 @@ function TextView(options) { this.multiLine = options.multiLine || false; this.fillChar = miscUtil.valueWithDefault(options.fillChar, ' ').substr(0, 1); this.justify = options.justify || 'right'; + this.resizable = miscUtil.valueWithDefault(options.resizable, true); //this.inputType = options.inputType || 'normal'; this.isPasswordTextStyle = 'P' === this.textStyle || 'password' === this.textStyle; @@ -84,6 +85,12 @@ TextView.prototype.getViewData = function() { }; TextView.prototype.setText = function(text) { + + var widthDelta = 0; + if(this.text && this.text !== text) { + widthDelta = Math.abs(this.text.length - text.length); + } + this.text = text; if(this.maxLength > 0) { @@ -92,8 +99,14 @@ TextView.prototype.setText = function(text) { this.text = strUtil.stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle); - if(!this.multiLine && !this.dimens.width) { + /*if(!this.multiLine && !this.dimens.width) { this.dimens.width = this.text.length; + }*/ + + if(!this.multiLine) { + if(this.resizable) { + this.dimens.width = this.text.length + widthDelta; + } } this.redraw(); diff --git a/core/view.js b/core/view.js index b8b52cdd..71d9c556 100644 --- a/core/view.js +++ b/core/view.js @@ -116,6 +116,20 @@ View.prototype.setPosition = function(pos) { 'Y position ' + this.position.y + ' out of terminal range ' + this.client.term.termWidth); }; +View.prototype.setColor = function(fg, bg, flags) { + if(fg) { + this.color.fg = fg; + } + + if(bg) { + this.color.bg = bg; + } + + if('undefined' !== typeof flags) { + this.color.flags = flags; + } +}; + View.prototype.getColor = function() { return this.color; }; @@ -133,6 +147,8 @@ View.prototype.setFocus = function(focused) { this.hasFocus = focused; this.client.term.write('show' === this.cursor ? ansi.showCursor() : ansi.hideCursor()); + + this.emit(focused ? 'enter' : 'leave'); }; View.prototype.onKeyPress = function(key, isSpecial) { diff --git a/core/view_controller.js b/core/view_controller.js index cc98e376..43fe8fd6 100644 --- a/core/view_controller.js +++ b/core/view_controller.js @@ -140,6 +140,10 @@ ViewController.prototype.getView = function(id) { return this.views[id]; }; +ViewController.prototype.getFocusedView = function() { + return this.focusedView; +}; + ViewController.prototype.switchFocus = function(id) { if(this.focusedView && this.focusedView.acceptsFocus) { this.focusedView.setFocus(false); diff --git a/mods/apply.js b/mods/apply.js index 66207311..d834cca5 100644 --- a/mods/apply.js +++ b/mods/apply.js @@ -51,5 +51,44 @@ ApplyModule.prototype.mciReady = function(mciMap) { self.viewController = self.addViewController(new ViewController(self.client)); self.viewController.loadFromMCIMapAndConfig( { mciMap : mciMap, menuConfig : self.menuConfig }, function onViewReady(err) { + + var usernameView = self.viewController.getView(1); + var userExistsView = self.viewController.getView(10); + usernameView.on('leave', function leave() { + user.getUserIdAndName(usernameView.getViewData(), function userIdAndName(err) { + if(!err) { + userExistsView.setText('That username already exists!'); + } else { + userExistsView.setText(''); + } + //if(11 !== self.viewController.getFocusedView()) { + self.viewController.switchFocus(2); + //} + }); + }); + + var pwView = self.viewController.getView(8); + var pwConfirmView = self.viewController.getView(9); + var pwSecureView = self.viewController.getView(11); + var pwConfirmNoticeView = self.viewController.getView(12); + + // :TODO: show a secure meter here instead + pwView.on('leave', function pwLeave() { + if(pwView.getViewData().length > 3) { + pwSecureView.setColor(32); + pwSecureView.setText('Secure'); + } else { + pwSecureView.setColor(31); + pwSecureView.setText('Insecure!'); + } + }); + + pwConfirmView.on('leave', function confirmPwLeave() { + if(pwView.getViewData() !== pwConfirmView.getViewData()) { + pwConfirmNoticeView.setText('Passwords must match!'); + } else { + pwConfirmNoticeView.setText(''); + } + }); }); }; \ No newline at end of file