launcher/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 16 Jan 2013 12:25:50 +0100
branchdew
changeset 466 d6b1f996c0d8
parent 461 ccc3fd318cb1
child 469 c6e6782950b8
permissions -rw-r--r--
Serve byte code as JSON. Initialize the fields in JavaScript.
phrebejk@460
     1
// 'use strict';
phrebejk@460
     2
phrebejk@460
     3
// Declare app level module which depends on filters, and services
phrebejk@460
     4
angular.module('bck2brwsr', []).
phrebejk@460
     5
  directive('uiCodemirror', ['$timeout', function($timeout) {
phrebejk@460
     6
        'use strict';
phrebejk@460
     7
phrebejk@460
     8
        var events = ["cursorActivity", "viewportChange", "gutterClick", "focus", "blur", "scroll", "update"];
phrebejk@460
     9
        return {
phrebejk@460
    10
            restrict: 'A',
phrebejk@460
    11
            require: 'ngModel',
phrebejk@460
    12
            link: function(scope, elm, attrs, ngModel) {
phrebejk@460
    13
                var options, opts, onChange, deferCodeMirror, codeMirror, timeoutId, val;
phrebejk@460
    14
phrebejk@460
    15
                if (elm[0].type !== 'textarea') {
phrebejk@460
    16
                    throw new Error('uiCodemirror3 can only be applied to a textarea element');
phrebejk@460
    17
                }
phrebejk@460
    18
phrebejk@460
    19
                options = /* uiConfig.codemirror  || */ {};
phrebejk@460
    20
                opts = angular.extend({}, options, scope.$eval(attrs.uiCodemirror));
phrebejk@460
    21
phrebejk@460
    22
                onChange = function(instance, changeObj) {                    
phrebejk@460
    23
                    val = instance.getValue();
phrebejk@460
    24
                    $timeout.cancel(timeoutId);
phrebejk@460
    25
                    timeoutId = $timeout(function() {
phrebejk@460
    26
                        ngModel.$setViewValue(val);                        
phrebejk@460
    27
                      }, 500);                    
phrebejk@460
    28
                };
phrebejk@460
    29
                
phrebejk@460
    30
                deferCodeMirror = function() {
phrebejk@460
    31
                    codeMirror = CodeMirror.fromTextArea(elm[0], opts);
phrebejk@460
    32
                    // codeMirror.on("change", onChange(opts.onChange));
phrebejk@460
    33
                    codeMirror.on("change", onChange);
phrebejk@460
    34
phrebejk@460
    35
                    for (var i = 0, n = events.length, aEvent; i < n; ++i) {
phrebejk@460
    36
                        aEvent = opts["on" + events[i].charAt(0).toUpperCase() + events[i].slice(1)];
phrebejk@460
    37
                        if (aEvent === void 0)
phrebejk@460
    38
                            continue;
phrebejk@460
    39
                        if (typeof aEvent !== "function")
phrebejk@460
    40
                            continue;
phrebejk@460
    41
                                                
phrebejk@460
    42
                        var bound = _.bind( aEvent, scope );
phrebejk@460
    43
                        
phrebejk@460
    44
                        codeMirror.on(events[i], bound);
phrebejk@460
    45
                    }
phrebejk@460
    46
phrebejk@460
    47
                    // CodeMirror expects a string, so make sure it gets one.
phrebejk@460
    48
                    // This does not change the model.
phrebejk@460
    49
                    ngModel.$formatters.push(function(value) {
phrebejk@460
    50
                        if (angular.isUndefined(value) || value === null) {
phrebejk@460
    51
                            return '';
phrebejk@460
    52
                        }
phrebejk@460
    53
                        else if (angular.isObject(value) || angular.isArray(value)) {
phrebejk@460
    54
                            throw new Error('ui-codemirror cannot use an object or an array as a model');
phrebejk@460
    55
                        }
phrebejk@460
    56
                        return value;
phrebejk@460
    57
                    });
phrebejk@460
    58
phrebejk@460
    59
                    // Override the ngModelController $render method, which is what gets called when the model is updated.
phrebejk@460
    60
                    // This takes care of the synchronizing the codeMirror element with the underlying model, in the case that it is changed by something else.
phrebejk@460
    61
                    ngModel.$render = function() {
phrebejk@460
    62
                        codeMirror.setValue(ngModel.$viewValue);
phrebejk@460
    63
                    };
phrebejk@460
    64
phrebejk@460
    65
                };
phrebejk@460
    66
phrebejk@460
    67
                $timeout(deferCodeMirror);
phrebejk@460
    68
phrebejk@460
    69
            }
phrebejk@460
    70
        };
phrebejk@460
    71
}]);
phrebejk@460
    72
phrebejk@461
    73
function DevCtrl( $scope, $http ) {
jaroslav@466
    74
    var templateHtml = "<html><body>\n"
jaroslav@466
    75
        + " <button id='btn'>Hello!</button>\n"
jaroslav@466
    76
        + " <hr/>\n"
jaroslav@466
    77
        + "\n"
jaroslav@466
    78
        + "\n"
jaroslav@466
    79
        + "\n"
jaroslav@466
    80
        + "\n"
jaroslav@466
    81
        + "\n"
jaroslav@466
    82
        + "\n"
jaroslav@466
    83
        + "\n"
jaroslav@466
    84
        + "\n"
jaroslav@466
    85
        + "\n"
jaroslav@466
    86
        + "\n"
jaroslav@466
    87
        + "\n"
jaroslav@466
    88
        + "\n"
jaroslav@466
    89
        + "\n"
jaroslav@466
    90
        + "\n"
jaroslav@466
    91
        + "\n"
jaroslav@466
    92
        + "\n"
jaroslav@466
    93
        + "\n"
jaroslav@466
    94
        + "\n"
jaroslav@466
    95
        + "\n"
jaroslav@466
    96
        + "\n"
jaroslav@466
    97
        + " <script src=\"/bck2brwsr.js\"></script>\n"
jaroslav@466
    98
        + " <script type=\"text/javascript\">\n"
jaroslav@466
    99
        + "   function ldCls(res) {\n"
jaroslav@466
   100
        + "     var request = new XMLHttpRequest();\n"
jaroslav@466
   101
        + "     request.open('GET', '/dew/classes/' + res, false);\n"
jaroslav@466
   102
        + "     request.send();\n"
jaroslav@466
   103
        + "     var arr = eval('(' + request.responseText + ')');\n"
jaroslav@466
   104
        + "     return arr;\n"
jaroslav@466
   105
        + "   }\n"
jaroslav@466
   106
        + "   var vm = new bck2brwsr(ldCls);\n"
jaroslav@466
   107
        + "   vm.loadClass('bck2brwsr.demo.Index');\n"
jaroslav@466
   108
        + " </script>\n"
jaroslav@466
   109
        + "</body></html>\n";
jaroslav@466
   110
    var templateJava = "package bck2brwsr.demo;\n"
jaroslav@466
   111
        + "import org.apidesign.bck2brwsr.htmlpage.api.*;\n"
jaroslav@466
   112
        + "@Page(xhtml=\"index.html\", className=\"Index\")\n"
jaroslav@466
   113
        + "class YourFirstHTML5PageInRealLanguage {\n"
jaroslav@466
   114
        + "   @OnClick(id=\"btn\") static void clcs() {\n"
jaroslav@466
   115
        + "     Index.BTN.setDisabled(true);\n"
jaroslav@466
   116
        + "   }\n"
jaroslav@466
   117
        + "}\n";
jaroslav@466
   118
phrebejk@460
   119
    
phrebejk@461
   120
    $scope.reload= function() {
phrebejk@461
   121
        var frame = document.getElementById("result");        
phrebejk@461
   122
        frame.src = "result.html";
phrebejk@461
   123
        frame.contentDocument.location.reload(true);
phrebejk@461
   124
        frame.contentWindow.location.reload();
phrebejk@461
   125
    };
phrebejk@461
   126
    
phrebejk@461
   127
    $scope.post = function(html, java) {
phrebejk@461
   128
        return $http({url: ".",
phrebejk@461
   129
            method: "POST",
phrebejk@461
   130
            //headers: this.headers,
phrebejk@461
   131
            data: { html : $scope.html, java : $scope.java} 
phrebejk@461
   132
        }).success( $scope.reload );
phrebejk@460
   133
    };
phrebejk@460
   134
    
phrebejk@460
   135
    $scope.tab = "html";
jaroslav@466
   136
    $scope.html= templateHtml;  
jaroslav@466
   137
    $scope.java = templateJava;  
phrebejk@460
   138
    
phrebejk@460
   139
    $scope.tabActive = function( tab ) {
phrebejk@460
   140
        return tab === $scope.tab ? "active" : "";
phrebejk@460
   141
    };
phrebejk@460
   142
    
phrebejk@461
   143
    // $scope.$watch( "html", htmlChange );
jaroslav@466
   144
    $scope.post();
phrebejk@460
   145
    
phrebejk@460
   146
}