launcher/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js
author phrebejk
Tue, 15 Jan 2013 16:56:18 +0100
branchdew
changeset 461 ccc3fd318cb1
parent 460 c0f1788183dd
child 466 d6b1f996c0d8
permissions -rw-r--r--
Web development server initial prototype.
     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     
    75     $scope.reload= function() {
    76         var frame = document.getElementById("result");        
    77         frame.src = "result.html";
    78         frame.contentDocument.location.reload(true);
    79         frame.contentWindow.location.reload();
    80     };
    81     
    82     $scope.post = function(html, java) {
    83         return $http({url: ".",
    84             method: "POST",
    85             //headers: this.headers,
    86             data: { html : $scope.html, java : $scope.java} 
    87         }).success( $scope.reload );
    88     };
    89     
    90     $scope.tab = "html";
    91     $scope.html= "";  
    92     $scope.java = "";  
    93     
    94     $scope.tabActive = function( tab ) {
    95         return tab === $scope.tab ? "active" : "";
    96     };
    97     
    98     // $scope.$watch( "html", htmlChange );
    99     
   100     
   101 }