Errors courtesy Jarda.
3 // Declare app level module which depends on filters, and services
4 angular.module('bck2brwsr', []).
5 directive('uiCodemirror', ['$timeout', function($timeout) {
8 var events = ["cursorActivity", "viewportChange", "gutterClick", "focus", "blur", "scroll", "update"];
12 link: function(scope, elm, attrs, ngModel) {
13 var options, opts, onChange, deferCodeMirror, codeMirror, timeoutId, val;
15 if (elm[0].type !== 'textarea') {
16 throw new Error('uiCodemirror3 can only be applied to a textarea element');
19 options = /* uiConfig.codemirror || */ {};
20 opts = angular.extend({}, options, scope.$eval(attrs.uiCodemirror));
22 onChange = function(instance, changeObj) {
23 val = instance.getValue();
24 $timeout.cancel(timeoutId);
25 timeoutId = $timeout(function() {
26 ngModel.$setViewValue(val);
30 deferCodeMirror = function() {
31 codeMirror = CodeMirror.fromTextArea(elm[0], opts);
32 elm[0].codeMirror = codeMirror;
33 // codeMirror.on("change", onChange(opts.onChange));
34 codeMirror.on("change", onChange);
36 for (var i = 0, n = events.length, aEvent; i < n; ++i) {
37 aEvent = opts["on" + events[i].charAt(0).toUpperCase() + events[i].slice(1)];
38 if (aEvent === void 0)
40 if (typeof aEvent !== "function")
43 var bound = _.bind( aEvent, scope );
45 codeMirror.on(events[i], bound);
48 // CodeMirror expects a string, so make sure it gets one.
49 // This does not change the model.
50 ngModel.$formatters.push(function(value) {
51 if (angular.isUndefined(value) || value === null) {
54 else if (angular.isObject(value) || angular.isArray(value)) {
55 throw new Error('ui-codemirror cannot use an object or an array as a model');
60 // Override the ngModelController $render method, which is what gets called when the model is updated.
61 // This takes care of the synchronizing the codeMirror element with the underlying model, in the case that it is changed by something else.
62 ngModel.$render = function() {
63 codeMirror.setValue(ngModel.$viewValue);
68 $timeout(deferCodeMirror);
74 function DevCtrl( $scope, $http ) {
75 var templateHtml = "<html><body>\n"
76 + " <button id='btn'>Hello!</button>\n"
98 + " <script src=\"/bck2brwsr.js\"></script>\n"
99 + " <script type=\"text/javascript\">\n"
100 + " function ldCls(res) {\n"
101 + " var request = new XMLHttpRequest();\n"
102 + " request.open('GET', '/classes/' + res, false);\n"
103 + " request.send();\n"
104 + " var arr = eval('(' + request.responseText + ')');\n"
107 + " var vm = new bck2brwsr(ldCls);\n"
108 + " vm.loadClass('bck2brwsr.demo.Index');\n"
110 + "</body></html>\n";
111 var templateJava = "package bck2brwsr.demo;\n"
112 + "import org.apidesign.bck2brwsr.htmlpage.api.*;\n"
113 + "import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;\n"
114 + "@Page(xhtml=\"index.html\", className=\"Index\")\n"
115 + "class YourFirstHTML5PageInRealLanguage {\n"
116 + " @On(event=CLICK, id=\"btn\") static void clcs() {\n"
117 + " Element.alert(\"Hello World!\");\n"
118 + " Index.BTN.setDisabled(true);\n"
123 $scope.reload = function() {
124 $scope.errors = null;
125 var frame = document.getElementById("result");
126 frame.src = "result.html";
127 frame.contentDocument.location.reload(true);
128 frame.contentWindow.location.reload();
131 $scope.fail = function( data ) {
132 $scope.errors = eval( data );
135 $scope.post = function(html, java) {
136 return $http({url: ".",
138 //headers: this.headers,
139 data: { html : $scope.html, java : $scope.java}
140 }).success( $scope.reload ).error( $scope.fail );
143 $scope.errorClass = function( kind ) {
152 $scope.gotoError = function( line, col ) {
153 var editor = document.getElementById("editorJava").codeMirror;
154 editor.setCursor({ line: line - 1, ch : col });
159 $scope.html= templateHtml;
160 $scope.java = templateJava;
162 $scope.tabActive = function( tab ) {
163 return tab === $scope.tab ? "active" : "";
166 // $scope.$watch( "html", htmlChange );