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