Skip to content
Permalink
Browse files

Adding type=password support for Text in TextView class.

  • Loading branch information...
Janki Akhani
Janki Akhani committed Apr 27, 2017
1 parent c87ff49 commit f19ac2a23159e92b09c13bbb35321f6a5f71cc09
Showing with 9 additions and 139 deletions.
  1. +2 −32 ipywidgets/widgets/widget_string.py
  2. +7 −107 jupyter-js-widgets/src/widget_string.ts
@@ -110,37 +110,7 @@ def on_submit(self, callback, remove=False):


@register
class Password(_String):
class Password(Text):
"""Single line textbox widget."""
_view_name = Unicode('PasswordView').tag(sync=True)
_model_name = Unicode('PasswordModel').tag(sync=True)

def __init__(self, *args, **kwargs):
super(Password, self).__init__(*args, **kwargs)
self._submission_callbacks = CallbackDispatcher()
self.on_msg(self._handle_string_msg)

def _handle_string_msg(self, _, content, buffers):
"""Handle a msg from the front-end.
Parameters
----------
content: dict
Content of the msg.
"""
if content.get('event', '') == 'submit':
self._submission_callbacks(self)

def on_submit(self, callback, remove=False):
"""(Un)Register a callback to handle text submission.
Triggered when the user clicks enter.
Parameters
----------
callback: callable
Will be called with exactly one argument: the Widget instance
remove: bool (optional)
Whether to unregister the callback
"""
self._submission_callbacks.register_callback(callback, remove=remove)
_model_name = Unicode('PasswordModel').tag(sync=True)
@@ -268,6 +268,8 @@ class TextModel extends StringModel {

export
class TextView extends LabeledDOMWidgetView {

protected inputType = 'text';
/**
* Called when view is rendered.
*/
@@ -278,7 +280,7 @@ class TextView extends LabeledDOMWidgetView {
this.el.classList.add('widget-text');

this.textbox = document.createElement('input');
this.textbox.setAttribute('type', 'text');
this.textbox.setAttribute('type', this.inputType);
this.el.appendChild(this.textbox);

this.update(); // Set defaults.
@@ -387,7 +389,7 @@ class TextView extends LabeledDOMWidgetView {
}

export
class PasswordModel extends StringModel {
class PasswordModel extends TextModel {
defaults() {
return _.extend(super.defaults(), {
_view_name: 'PasswordView',
@@ -397,109 +399,7 @@ class PasswordModel extends StringModel {
}

export
class PasswordView extends LabeledDOMWidgetView {
/**
* Called when view is rendered.
*/
render() {
super.render();
this.el.classList.add('jupyter-widgets');
this.el.classList.add('widget-inline-hbox');
this.el.classList.add('widget-text');

this.textbox = document.createElement('input');
this.textbox.setAttribute('type', 'password');
this.el.appendChild(this.textbox);

this.update(); // Set defaults.
}

update(options?) {
/**
* Update the contents of this view
*
* Called when the model is changed. The model may have been
* changed by another view or by a state update from the back-end.
*/
if (options === undefined || options.updated_view != this) {
if (this.textbox.value != this.model.get('value')) {
this.textbox.value = this.model.get('value');
}

var disabled = this.model.get('disabled');
this.textbox.disabled = disabled;
}
return super.update();
}

events(): {[e: string]: string} {
return {
// Dictionary of events and their handlers.
'keydown input' : 'handleKeyDown',
'keypress input' : 'handleKeypress',
'keyup input' : 'handleChanging',
'paste input' : 'handleChanging',
'cut input' : 'handleChanging',
'blur input' : 'handleBlur',
'focusout input' : 'handleFocusOut'
}
}

/**
* Handle key down
*
* Stop propagation so the keypress isn't sent to the application.
*/
handleKeyDown(e) {
e.stopPropagation();
}

/**
* Handles text submission
*/
handleKeypress(e) {
e.stopPropagation();
if (e.keyCode == 13) { // Return key
this.send({event: 'submit'});
e.preventDefault();
}
}

/**
* Handles user input.
*
* Calling model.set will trigger all of the other views of the
* model to update.
*/
handleChanging(e) {
e.stopPropagation();
this.model.set('value', e.target.value, {updated_view: this});
this.touch();
}

/**
* Prevent a blur from firing if the blur was not user intended.
* This is a workaround for the return-key focus loss bug.
* TODO: Is the original bug actually a fault of the keyboard
* manager?
*/
handleBlur(e) {
if (e.relatedTarget === null) {
e.stopPropagation();
e.preventDefault();
}
}

/**
* Prevent a blur from firing if the blur was not user intended.
* This is a workaround for the return-key focus loss bug.
*/
handleFocusOut(e) {
if (e.relatedTarget === null) {
e.stopPropagation();
e.preventDefault();
}
}

textbox: HTMLInputElement;
class PasswordView extends TextView
{
protected inputType = 'password';
}

0 comments on commit f19ac2a

Please sign in to comment.
You can’t perform that action at this time.