Skip to content

Commit

Permalink
#7236: add _dom_classes support
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslawmalekcodete authored and Mariusz Jurowicz committed May 24, 2018
1 parent 8c54553 commit 8b282b5
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import com.twosigma.beakerx.message.Message;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand All @@ -31,9 +33,11 @@ public abstract class DOMWidget extends Widget {
public static final String SYNC_DATA = "state";
public static final String MODEL_MODULE_VALUE = "@jupyter-widgets/controls";
public static final String VIEW_MODULE_VALUE = "@jupyter-widgets/controls";
public static final String DOM_CLASSES = "_dom_classes";

private Layout layout;
protected Style style;
private List<String> domClasses = new ArrayList<>();

private UpdateValueCallback updateValueCallback = () -> {
};
Expand Down Expand Up @@ -75,7 +79,7 @@ public Optional<Object> getSyncDataValue(Message msg) {
ret = Optional.of(sync_data.get(VALUE));
} else if (sync_data.containsKey(INDEX)) {
ret = Optional.of(sync_data.get(INDEX));
} else if (sync_data.containsKey("outputs")){
} else if (sync_data.containsKey("outputs")) {
ret = Optional.of(sync_data.get("outputs"));
}
}
Expand Down Expand Up @@ -104,12 +108,12 @@ public void doUpdateValueWithCallback(Object value) {
}

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

@Override
public String getViewModuleValue(){
public String getViewModuleValue() {
return VIEW_MODULE_VALUE;
}

Expand All @@ -125,9 +129,19 @@ protected HashMap<String, Serializable> content(HashMap<String, Serializable> co
content.put("font_weight", "");
content.put("background_color", null);
content.put("color", null);
content.put(DOM_CLASSES, domClasses.toArray());
return content;
}

public List<String> getDomClasses() {
return domClasses;
}

public void setDomClasses(List<String> domClasses) {
this.domClasses = checkNotNull(domClasses);
sendUpdate(DOM_CLASSES, this.domClasses.toArray());
}

public Layout getLayout() {
if (layout == null) {
layout = new Layout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private static Optional<Message> getError(List<Message> messages) {
}


private static Optional<Message> getUpdate(List<Message> messages) {
public static Optional<Message> getUpdate(List<Message> messages) {
return messages.stream().
filter(x -> x.type().equals(JupyterMessages.COMM_MSG)).
filter(x -> TestWidgetUtils.getData(x).get("method").equals("update")).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.
*/
package com.twosigma.beakerx.widget;

import com.twosigma.beakerx.KernelTest;
import com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher;
import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.message.Message;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Map;

import static com.twosigma.beakerx.widget.DOMWidget.DOM_CLASSES;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

public class DOMWidgetTest {

private KernelTest kernel;
private DOMWidget widget;

@Before
public void setUp() throws Exception {
kernel = new KernelTest();
KernelManager.register(kernel);
widget = new Button();
}

@After
public void tearDown() throws Exception {
KernelManager.register(null);
}

@Test
public void shouldSendDomClassesUpdateMessage() {
//given
//when
widget.setDomClasses(asList("class1", "class2"));
//then
Message update = EvaluatorResultTestWatcher.getUpdate(kernel.getPublishedMessages()).get();
Map state = TestWidgetUtils.getState(update);
assertThat(state.get(DOM_CLASSES)).isEqualTo(asList("class1", "class2").toArray());
}
}
40 changes: 40 additions & 0 deletions test/ipynb/groovy/DomClassesSupport.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import com.twosigma.beakerx.widget.Button\n",
"bt = new Button()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bt.domClasses = [\"BeakerX-button\", \"icon-close\"]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Groovy",
"language": "groovy",
"name": "groovy"
},
"language_info": {
"codemirror_mode": "groovy",
"file_extension": ".groovy",
"mimetype": "",
"name": "Groovy",
"nbconverter_exporter": "",
"version": "2.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 8b282b5

Please sign in to comment.