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.
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 ) {
phrebejk@460
    74
    
phrebejk@461
    75
    $scope.reload= function() {
phrebejk@461
    76
        var frame = document.getElementById("result");        
phrebejk@461
    77
        frame.src = "result.html";
phrebejk@461
    78
        frame.contentDocument.location.reload(true);
phrebejk@461
    79
        frame.contentWindow.location.reload();
phrebejk@461
    80
    };
phrebejk@461
    81
    
phrebejk@461
    82
    $scope.post = function(html, java) {
phrebejk@461
    83
        return $http({url: ".",
phrebejk@461
    84
            method: "POST",
phrebejk@461
    85
            //headers: this.headers,
phrebejk@461
    86
            data: { html : $scope.html, java : $scope.java} 
phrebejk@461
    87
        }).success( $scope.reload );
phrebejk@460
    88
    };
phrebejk@460
    89
    
phrebejk@460
    90
    $scope.tab = "html";
phrebejk@460
    91
    $scope.html= "";  
phrebejk@460
    92
    $scope.java = "";  
phrebejk@460
    93
    
phrebejk@460
    94
    $scope.tabActive = function( tab ) {
phrebejk@460
    95
        return tab === $scope.tab ? "active" : "";
phrebejk@460
    96
    };
phrebejk@460
    97
    
phrebejk@461
    98
    // $scope.$watch( "html", htmlChange );
phrebejk@460
    99
    
phrebejk@460
   100
    
phrebejk@460
   101
}