dew/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js
branchcanvas
changeset 1439 6c04b26f9a9a
parent 1438 03cd1b3e2e70
parent 1437 3ec3ae9699ef
child 1440 c943709738df
     1.1 --- a/dew/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js	Tue Feb 11 10:48:24 2014 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,224 +0,0 @@
     1.4 -// 'use strict';
     1.5 -
     1.6 -// Declare app level module which depends on filters, and services
     1.7 -angular.module('bck2brwsr', []).
     1.8 -  directive('uiCodemirror', ['$timeout', function($timeout) {
     1.9 -        'use strict';
    1.10 -
    1.11 -        var events = ["cursorActivity", "viewportChange", "gutterClick", "focus", "blur", "scroll", "update"];
    1.12 -        return {
    1.13 -            restrict: 'A',
    1.14 -            require: 'ngModel',
    1.15 -            link: function(scope, elm, attrs, ngModel) {
    1.16 -                var options, opts, onChange, deferCodeMirror, codeMirror, timeoutId, val;
    1.17 -
    1.18 -                if (elm[0].type !== 'textarea') {
    1.19 -                    throw new Error('uiCodemirror3 can only be applied to a textarea element');
    1.20 -                }
    1.21 -
    1.22 -                options = /* uiConfig.codemirror  || */ {};
    1.23 -                opts = angular.extend({}, options, scope.$eval(attrs.uiCodemirror));
    1.24 -
    1.25 -                onChange = function(instance, changeObj) {                    
    1.26 -                    val = instance.getValue();
    1.27 -                    $timeout.cancel(timeoutId);
    1.28 -                    timeoutId = $timeout(function() {
    1.29 -                        ngModel.$setViewValue(val);                        
    1.30 -                      }, 500);                    
    1.31 -                };
    1.32 -                
    1.33 -                deferCodeMirror = function() {
    1.34 -                    codeMirror = CodeMirror.fromTextArea(elm[0], opts);
    1.35 -                    elm[0].codeMirror = codeMirror;
    1.36 -                    // codeMirror.on("change", onChange(opts.onChange));
    1.37 -                    codeMirror.on("change", onChange);
    1.38 -
    1.39 -                    for (var i = 0, n = events.length, aEvent; i < n; ++i) {
    1.40 -                        aEvent = opts["on" + events[i].charAt(0).toUpperCase() + events[i].slice(1)];
    1.41 -                        if (aEvent === void 0)
    1.42 -                            continue;
    1.43 -                        if (typeof aEvent !== "function")
    1.44 -                            continue;
    1.45 -                                                
    1.46 -                        var bound = _.bind( aEvent, scope );
    1.47 -                        
    1.48 -                        codeMirror.on(events[i], bound);
    1.49 -                    }
    1.50 -
    1.51 -                    // CodeMirror expects a string, so make sure it gets one.
    1.52 -                    // This does not change the model.
    1.53 -                    ngModel.$formatters.push(function(value) {
    1.54 -                        if (angular.isUndefined(value) || value === null) {
    1.55 -                            return '';
    1.56 -                        }
    1.57 -                        else if (angular.isObject(value) || angular.isArray(value)) {
    1.58 -                            throw new Error('ui-codemirror cannot use an object or an array as a model');
    1.59 -                        }
    1.60 -                        return value;
    1.61 -                    });
    1.62 -
    1.63 -                    // Override the ngModelController $render method, which is what gets called when the model is updated.
    1.64 -                    // This takes care of the synchronizing the codeMirror element with the underlying model, in the case that it is changed by something else.
    1.65 -                    ngModel.$render = function() {
    1.66 -                        codeMirror.setValue(ngModel.$viewValue);
    1.67 -                    };
    1.68 -
    1.69 -                };
    1.70 -
    1.71 -                $timeout(deferCodeMirror);
    1.72 -
    1.73 -            }
    1.74 -        };
    1.75 -}]);
    1.76 -
    1.77 -function DevCtrl( $scope, $http ) {
    1.78 -    var templateHtml = 
    1.79 -"<html><body>\n" +
    1.80 -"  <input data-bind=\"value: value, valueUpdate: 'afterkeydown'\" \n" +
    1.81 -"     value=\"0\" type=\"number\">\n" +
    1.82 -"  </input>\n" +
    1.83 -"  * <span data-bind=\"text: value\">0</span> \n" +
    1.84 -"  = <span data-bind=\"text: powerValue\">0</span>\n" +
    1.85 -"  <br/>\n" +
    1.86 -"  <button id='dupl'>Duplicate!</button>\n" +
    1.87 -"  <button id=\"clear\">Clear!</button>" +
    1.88 -" <hr/>\n" +
    1.89 -"\n" +
    1.90 -"\n" +
    1.91 -"\n" +
    1.92 -"\n" +
    1.93 -"\n" +
    1.94 -"\n" +
    1.95 -"\n" +
    1.96 -"\n" +
    1.97 -"\n" +
    1.98 -"\n" +
    1.99 -"\n" +
   1.100 -"\n" +
   1.101 -"\n" +
   1.102 -"\n" +
   1.103 -"\n" +
   1.104 -"\n" +
   1.105 -"\n" +
   1.106 -"\n" +
   1.107 -"\n" +
   1.108 -"\n" +
   1.109 -" <script src=\"/bck2brwsr.js\"></script>\n" +
   1.110 -" <script type=\"text/javascript\">\n" +
   1.111 -"   function ldCls(res) {\n" +
   1.112 -"     var request = new XMLHttpRequest();\n" +
   1.113 -"     request.open('GET', '/classes/' + res, false);\n" +
   1.114 -"     request.send();\n" +
   1.115 -"     var arr = eval('(' + request.responseText + ')');\n" +
   1.116 -"     return arr;\n" +
   1.117 -"   }\n" +
   1.118 -"   var vm = bck2brwsr(ldCls);\n" +
   1.119 -"   vm.loadClass('${fqn}');\n" +
   1.120 -" </script>\n" +
   1.121 -"</body></html>";
   1.122 -    var templateJava = 
   1.123 -"package bck2brwsr.demo;\n" +
   1.124 -"import org.apidesign.bck2brwsr.htmlpage.api.*;\n" +
   1.125 -"import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;\n" +
   1.126 -"\n" +
   1.127 -"@Page(xhtml=\"index.html\", className=\"Index\", properties={\n" +
   1.128 -"  @Property(name=\"value\", type=int.class)\n" +
   1.129 -"})\n" +
   1.130 -"class YourFirstHTML5PageInRealLanguage {\n" +
   1.131 -"  static { new Index().applyBindings(); }\n" +
   1.132 -"  @On(event=CLICK, id=\"dupl\") static void duplicateValue(Index m) {\n" +
   1.133 -"    m.setValue(m.getValue() * 2);\n" +
   1.134 -"  }\n" +
   1.135 -"  @On(event=CLICK, id=\"clear\") static void zeroTheValue(Index m) {\n" +
   1.136 -"     m.setValue(0);;\n" +
   1.137 -"  }\n" +
   1.138 -"  @ComputedProperty static int powerValue(int value) {\n" +
   1.139 -"    return value * value;\n" +
   1.140 -"  }\n" +
   1.141 -"}";
   1.142 -
   1.143 -    
   1.144 -    $scope.makeMarker = function( editor, line ) {
   1.145 -        var marker = document.createElement("div");
   1.146 -        marker.innerHTML = " ";
   1.147 -        marker.className = "issue";
   1.148 -        
   1.149 -        var info = editor.lineInfo(line);
   1.150 -        editor.setGutterMarker(line, "issues", info.markers ? null : marker);
   1.151 -        
   1.152 -        return marker;
   1.153 -    };
   1.154 -    
   1.155 -    
   1.156 -    // Returns a function, that, as long as it continues to be invoked, will not
   1.157 -    // be triggered. The function will be called after it stops being called for
   1.158 -    // N milliseconds. If `immediate` is passed, trigger the function on the
   1.159 -    // leading edge, instead of the trailing.
   1.160 -    $scope.debounce = function(func, wait, immediate) {
   1.161 -      var timeout, result;
   1.162 -      return function() {
   1.163 -        var context = this, args = arguments;
   1.164 -        var later = function() {
   1.165 -          timeout = null;
   1.166 -          if (!immediate) result = func.apply(context, args);
   1.167 -        };
   1.168 -        var callNow = immediate && !timeout;
   1.169 -        clearTimeout(timeout);
   1.170 -        timeout = setTimeout(later, wait);
   1.171 -        if (callNow) result = func.apply(context, args);
   1.172 -        return result;
   1.173 -      };
   1.174 -    };
   1.175 -    
   1.176 -    $scope.reload = function() {
   1.177 -        $scope.errors = null;
   1.178 -        var frame = document.getElementById("result");        
   1.179 -        frame.src = "result.html";
   1.180 -        frame.contentDocument.location.reload(true);
   1.181 -        frame.contentWindow.location.reload();
   1.182 -        document.getElementById("editorJava").codeMirror.clearGutter("issues");   
   1.183 -    };
   1.184 -    
   1.185 -    $scope.fail = function( data ) {
   1.186 -        $scope.errors = eval( data );
   1.187 -        var editor = document.getElementById("editorJava").codeMirror;   
   1.188 -        editor.clearGutter( "issues" );
   1.189 -        
   1.190 -        for( var i = 0; i < $scope.errors.length; i ++ ) {
   1.191 -            $scope.makeMarker( editor, $scope.errors[i].line - 1 );
   1.192 -        }
   1.193 -        
   1.194 -    };
   1.195 -    
   1.196 -    $scope.post = function() {
   1.197 -        return $http({url: ".",
   1.198 -            method: "POST",
   1.199 -            //headers: this.headers,
   1.200 -            data: { html : $scope.html, java : $scope.java} 
   1.201 -        }).success( $scope.reload ).error( $scope.fail );
   1.202 -    };
   1.203 -    
   1.204 -    $scope.errorClass = function( kind ) {
   1.205 -        switch( kind ) {
   1.206 -            case "ERROR" :
   1.207 -                return "error";
   1.208 -            default :         
   1.209 -                return "warning";   
   1.210 -        }
   1.211 -    };
   1.212 -    
   1.213 -    $scope.gotoError = function( line, col ) {
   1.214 -        var editor = document.getElementById("editorJava").codeMirror;   
   1.215 -        editor.setCursor({ line: line - 1, ch : col - 1 });
   1.216 -        editor.focus();
   1.217 -    };
   1.218 -    
   1.219 -    $scope.tab = "html";
   1.220 -    $scope.html= templateHtml;  
   1.221 -    $scope.java = templateJava;  
   1.222 -    
   1.223 -    $scope.$watch( "html", $scope.debounce( $scope.post, 2000 ) );
   1.224 -    $scope.$watch( "java", $scope.debounce( $scope.post, 2000 ) );
   1.225 -    $scope.post();
   1.226 -    
   1.227 -}