Skip to content

Commit

Permalink
#6745 password beakerx widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz Mitusinski committed Feb 6, 2018
1 parent 76bfc5e commit 31aab3d
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 2 deletions.
21 changes: 20 additions & 1 deletion beakerx/beakerx/beakerx_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ipywidgets import Box, DOMWidget, CoreWidget, Text, Label, Textarea, \
from ipywidgets import Box, DOMWidget, CoreWidget, \
Text, Label, Textarea, Password, \
Button, Widget, \
SelectMultiple, Select, Dropdown, Checkbox, HBox, \
VBox, RadioButtons, register, Layout, widget_serialization, HTML
Expand Down Expand Up @@ -150,6 +151,24 @@ def __init__(self, **kwargs):
style = None


class BeakerxPassword(Password, EasyFormComponent):
def on_value_change(self, change):
self.fireChanged(change['new'])

def __init__(self, **kwargs):
super(BeakerxPassword, self).__init__(**kwargs)
self.observe(self.on_value_change, names='value')

_view_module = Unicode('beakerx').tag(sync=True)
_model_module = Unicode('beakerx').tag(sync=True)
_model_module_version = Unicode('*').tag(sync=True)
_view_module_version = Unicode('*').tag(sync=True)

size = Int(default_value=-1).tag(sync=True)
layout = InstanceDict(BeakerxLayout).tag(sync=True, **widget_serialization)
style = None


class BeakerxHTML(HTML, EasyFormComponent):
def __init__(self, *args, **kwargs):
super(BeakerxHTML, self).__init__(**kwargs)
Expand Down
7 changes: 7 additions & 0 deletions beakerx/beakerx/easyform/easyform.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ def addTextField(self, *args, **kwargs):
self.components[text.description] = text
return text

def addPasswordField(self, *args, **kwargs):
password = BeakerxPassword(description=self.getDescription(args, kwargs))
password.size = getValue(kwargs, 'width', -1)
self.children += (password,)
self.components[password.description] = password
return password

def addTextArea(self, *args, **kwargs):
textarea = BeakerxTextArea(
description=self.getDescription(args, kwargs))
Expand Down
1 change: 1 addition & 0 deletions doc/groovy/EasyForm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
"g = new EasyForm(\"Field Types\")\n",
"g.addTextField(\"Short Text Field\", 10)\n",
"g.addTextField(\"Text Field\")\n",
"g.addPasswordField(\"Password Field\", 10)\n",
"g.addTextArea(\"Text Area\")\n",
"g.addTextArea(\"Tall Text Area\", 10, 5)\n",
"g.addCheckBox(\"Check Box\")\n",
Expand Down
1 change: 1 addition & 0 deletions doc/python/EasyForm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"g = EasyForm(\"Field Types\")\n",
"g.addTextField(\"Short Text Field\", width=10)\n",
"g.addTextField(\"Text Field\")\n",
"g.addPasswordField(\"Password Field\", width=10)\n",
"g.addTextArea(\"Text Area\")\n",
"g.addTextArea(\"Tall Text Area\", 10, 5)\n",
"g.addCheckBox(\"Check Box\")\n",
Expand Down
3 changes: 2 additions & 1 deletion js/notebook/src/EasyForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
var ENTER_KEY_CODE = 13;
var widgets = require('./widgets');
var _ = require('underscore');

var selectMultipleWidget = require('./easyForm/selectMultipleWidget');
var selectMultipleSingleWidget = require('./easyForm/selectMultipleSingleWidget');
var datePickerWidget = require('./easyForm/datePickerWidget');
var comboBoxWidget = require('./easyForm/comboBoxWidget').default;
var textWidget = require('./easyForm/textWidget').default;
var passwordWidget = require('./easyForm/passwordWidget').default
var TextareaWidget = require('./easyForm/TextareaWidget').default;
var checkboxWidget = require('./easyForm/checkboxWidget').default;

Expand Down Expand Up @@ -99,6 +99,7 @@ _.extend(module.exports, selectMultipleWidget);
_.extend(module.exports, selectMultipleSingleWidget);
_.extend(module.exports, datePickerWidget);
_.extend(module.exports, textWidget);
_.extend(module.exports, passwordWidget);
_.extend(module.exports, TextareaWidget);
_.extend(module.exports, comboBoxWidget);
_.extend(module.exports, checkboxWidget);
72 changes: 72 additions & 0 deletions js/notebook/src/easyForm/passwordWidget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const widgets = require('../widgets');

export const TEXT_INPUT_WIDTH_UNIT = 'px';

class PasswordModel extends widgets.PasswordModel {
defaults() {
return {
...super.defaults(),
_view_name: "PasswordView",
_model_name: "PasswordModel",
_model_module: "beakerx",
_view_module: "beakerx",
_model_module_version: BEAKERX_MODULE_VERSION,
_view_module_version: BEAKERX_MODULE_VERSION
};
}
}

class PasswordView extends widgets.PasswordView {
handleKeypress(e) {
if (e.keyCode == 13) {
this.send({ event: 'submit' });
e.preventDefault();
}
}

handleEnterKeyPress(e) {
if (e.keyCode == 13) {
this.send({ event: 'submit' });
e.preventDefault();
}
}

render() {
super.render.call(this);

const width = this.model.get('width');
const size = this.model.get('size');

width >= 0 && this.setWidth(width);
size >= 0 && this.setSize(size);
}

setWidth(width: number): void {
this.textbox.style.maxWidth = width + TEXT_INPUT_WIDTH_UNIT;
}

setSize(size: number): void {
this.textbox.setAttribute('size', size);
}
}

export default {
PasswordModel,
PasswordView
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.twosigma.beakerx.easyform.formitem.widgets.CheckBoxWidget;
import com.twosigma.beakerx.easyform.formitem.widgets.ComboBoxWidget;
import com.twosigma.beakerx.easyform.formitem.widgets.DatePickerComponentWidget;
import com.twosigma.beakerx.easyform.formitem.widgets.PasswordWidget;
import com.twosigma.beakerx.easyform.formitem.widgets.SelectMultipleSingleWidget;
import com.twosigma.beakerx.easyform.formitem.widgets.SelectMultipleWidget;
import com.twosigma.beakerx.easyform.formitem.widgets.RadioButtonComponentWidget;
Expand Down Expand Up @@ -84,6 +85,18 @@ public EasyFormComponent addTextField(final String label, final Integer size) th
return addComponentOrThrow(label, textField);
}

public EasyFormComponent addPasswordField(final String label) throws Exception {
return addPasswordField(label, -1);
}

public EasyFormComponent addPasswordField(String label, int size) throws Exception {
PasswordWidget passwordWidget = new PasswordWidget();
passwordWidget.registerUpdateValueCallback(passwordWidget::fireChanged);
passwordWidget.setLabel(label);
passwordWidget.setSize(size);
return addComponentOrThrow(label, passwordWidget);
}

public EasyFormComponent addTextArea(final String label) throws Exception {
return addTextArea(label, null, TextAreaWidget.AUTO_WIDTH, TextAreaWidget.AUTO_HEIGHT);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*import static org.assertj.core.api.Assertions.assertThat;
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.twosigma.beakerx.easyform.formitem.widgets;

import com.twosigma.beakerx.easyform.EasyFormComponent;
import com.twosigma.beakerx.widgets.strings.Password;

public class PasswordWidget extends EasyFormComponent<Password>{

private Integer width;
private Integer size;

public Integer getSize() {
return size;
}

public void setSize(Integer size) {
this.size = size;
getWidget().sendUpdate("size", size);
}

public PasswordWidget() {
super(new Password());
}

public Integer getWidth() {
return width;
}

@Override
protected boolean checkValue(final Object value) {
return value instanceof String;
}

public PasswordWidget setWidth(Integer width) {
this.width = width;
getWidget().sendUpdate("width", width);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*import static org.assertj.core.api.Assertions.assertThat;
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.twosigma.beakerx.widgets.strings;

public class Password extends StringWidget {

public static final String VIEW_NAME_VALUE = "PasswordView";
public static final String MODEL_NAME_VALUE = "PasswordModel";
public static final String MODEL_MODULE_VALUE = "beakerx";
public static final String VIEW_MODULE_VALUE = "beakerx";

public Password() {
super();
openComm();
}

@Override
public String getModelNameValue() {
return MODEL_NAME_VALUE;
}

@Override
public String getViewNameValue() {
return VIEW_NAME_VALUE;
}

@Override
public String getModelModuleValue(){
return MODEL_MODULE_VALUE;
}

@Override
public String getViewModuleValue(){
return VIEW_MODULE_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.twosigma.beakerx.widgets.TestWidgetUtils.verifyOpenCommMsg;
import static com.twosigma.beakerx.widgets.TestWidgetUtils.verifyOpenCommMsgWitoutLayout;
import static com.twosigma.beakerx.widgets.Widget.VALUE;
import static com.twosigma.beakerx.widgets.strings.PasswordTest.verifyPasswordField;
import static com.twosigma.beakerx.widgets.strings.TextTest.verifyTextField;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -42,6 +43,7 @@
import com.twosigma.beakerx.widgets.selections.RadioButtons;
import com.twosigma.beakerx.widgets.selections.SelectMultiple;
import com.twosigma.beakerx.widgets.selections.SelectMultipleSingle;
import com.twosigma.beakerx.widgets.strings.Password;
import com.twosigma.beakerx.widgets.strings.Text;
import com.twosigma.beakerx.widgets.strings.Textarea;

Expand Down Expand Up @@ -277,6 +279,26 @@ public void shouldCreateEasyFormWithTextField() throws Exception {
verifyDisplayMsg(kernel.getPublishedMessages());
}

@Test
public void shouldCreateEasyFormWithPasswordField() throws Exception {
//given
String label = "pass1";
//when
EasyForm easyForm = new EasyForm("EasyForm with password field");
easyForm.addPasswordField(label, 10);
easyForm.display();
//then
verifyPasswordField(
kernel.getPublishedMessages(),
Password.MODEL_NAME_VALUE,
Password.MODEL_MODULE_VALUE,
Password.VIEW_NAME_VALUE,
Password.VIEW_MODULE_VALUE
);
verifyEasyForm(kernel.getPublishedMessages(), easyForm.getCommFunctionalities());
verifyDisplayMsg(kernel.getPublishedMessages());
}

private void verifyEasyForm(List<Message> messages, List<Widget> children) {
Message msg = SearchMessages.getListWidgetsByViewName(messages, EasyFormView.VIEW_NAME_VALUE)
.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public void shouldUpdateTextField() throws Exception {
assertThat(((ValueWidget) easyForm.getWidget(label)).getValue()).isEqualTo("new Value");
}

@Test
public void shouldUpdatePasswordField() throws Exception {
//given
String label = "pass1";
EasyForm easyForm = new EasyForm("EasyForm with password field");
easyForm.addPasswordField(label);
//when
easyForm.getWidget(label).doUpdateValueWithCallback("new Value");
//then
assertThat(((ValueWidget) easyForm.getWidget(label)).getValue()).isEqualTo("new Value");
}


@Test
public void shouldUpdateRadioButton() throws Exception {
Expand Down
Loading

0 comments on commit 31aab3d

Please sign in to comment.