launcher/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js
branchdew
changeset 460 c0f1788183dd
child 461 ccc3fd318cb1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/launcher/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js	Tue Jan 15 15:56:45 2013 +0100
     1.3 @@ -0,0 +1,91 @@
     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 +                    // codeMirror.on("change", onChange(opts.onChange));
    1.36 +                    codeMirror.on("change", onChange);
    1.37 +
    1.38 +                    for (var i = 0, n = events.length, aEvent; i < n; ++i) {
    1.39 +                        aEvent = opts["on" + events[i].charAt(0).toUpperCase() + events[i].slice(1)];
    1.40 +                        if (aEvent === void 0)
    1.41 +                            continue;
    1.42 +                        if (typeof aEvent !== "function")
    1.43 +                            continue;
    1.44 +                                                
    1.45 +                        var bound = _.bind( aEvent, scope );
    1.46 +                        
    1.47 +                        codeMirror.on(events[i], bound);
    1.48 +                    }
    1.49 +
    1.50 +                    // CodeMirror expects a string, so make sure it gets one.
    1.51 +                    // This does not change the model.
    1.52 +                    ngModel.$formatters.push(function(value) {
    1.53 +                        if (angular.isUndefined(value) || value === null) {
    1.54 +                            return '';
    1.55 +                        }
    1.56 +                        else if (angular.isObject(value) || angular.isArray(value)) {
    1.57 +                            throw new Error('ui-codemirror cannot use an object or an array as a model');
    1.58 +                        }
    1.59 +                        return value;
    1.60 +                    });
    1.61 +
    1.62 +                    // Override the ngModelController $render method, which is what gets called when the model is updated.
    1.63 +                    // This takes care of the synchronizing the codeMirror element with the underlying model, in the case that it is changed by something else.
    1.64 +                    ngModel.$render = function() {
    1.65 +                        codeMirror.setValue(ngModel.$viewValue);
    1.66 +                    };
    1.67 +
    1.68 +                };
    1.69 +
    1.70 +                $timeout(deferCodeMirror);
    1.71 +
    1.72 +            }
    1.73 +        };
    1.74 +}]);
    1.75 +
    1.76 +function DevCtrl( $scope ) {
    1.77 +    
    1.78 +    var htmlChange = function() {
    1.79 +        var frame = document.getElementById("result");
    1.80 +        frame.contentDocument.write($scope.html);        
    1.81 +    };
    1.82 +    
    1.83 +    $scope.tab = "html";
    1.84 +    $scope.html= "";  
    1.85 +    $scope.java = "";  
    1.86 +    
    1.87 +    $scope.tabActive = function( tab ) {
    1.88 +        return tab === $scope.tab ? "active" : "";
    1.89 +    };
    1.90 +    
    1.91 +    $scope.$watch( "html", htmlChange );
    1.92 +    
    1.93 +    
    1.94 +}