ko/kosample/client/src/main/java/org/apidesign/bck2brwsr/kosample/DataModel.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Apr 2016 08:04:12 +0200
changeset 1941 621825e167d7
child 1944 644d4f4bc6e0
permissions -rw-r--r--
Adding sample module using knockout
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.kosample;
    19 
    20 import net.java.html.json.ComputedProperty;
    21 import net.java.html.json.Function;
    22 import net.java.html.json.Model;
    23 import net.java.html.json.Property;
    24 import org.apidesign.bck2brwsr.kosample.js.Dialogs;
    25 
    26 /** Model annotation generates class Data with
    27  * one message property, boolean property and read only words property
    28  */
    29 @Model(className = "Data", targetId="", properties = {
    30     @Property(name = "message", type = String.class),
    31     @Property(name = "rotating", type = boolean.class)
    32 })
    33 final class DataModel {
    34     @ComputedProperty static java.util.List<String> words(String message) {
    35         String[] arr = new String[6];
    36         String[] words = message == null ? new String[0] : message.split(" ", 6);
    37         for (int i = 0; i < 6; i++) {
    38             arr[i] = words.length > i ? words[i] : "!";
    39         }
    40         return java.util.Arrays.asList(arr);
    41     }
    42 
    43     @Function static void turnAnimationOn(Data model) {
    44         model.setRotating(true);
    45     }
    46 
    47     @Function static void turnAnimationOff(final Data model) {
    48         Dialogs.confirmByUser("Really turn off?", new Runnable() {
    49             @Override
    50             public void run() {
    51                 model.setRotating(false);
    52             }
    53         });
    54     }
    55 
    56     @Function static void rotate5s(final Data model) {
    57         model.setRotating(true);
    58         java.util.Timer timer = new java.util.Timer("Rotates a while");
    59         timer.schedule(new java.util.TimerTask() {
    60             @Override
    61             public void run() {
    62                 model.setRotating(false);
    63             }
    64         }, 5000);
    65     }
    66 
    67     @Function static void showScreenSize(Data model) {
    68         model.setMessage(Dialogs.screenSize());
    69     }
    70     private static Data ui;
    71     /**
    72      * Called when the page is ready.
    73      */
    74     static void onPageLoad() throws Exception {
    75         ui = new Data();
    76         ui.setMessage("Hello World from HTML and Java!");
    77         ui.applyBindings();
    78     }
    79 }