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 // codeMirror.on("change", onChange(opts.onChange));
33 codeMirror.on("change", onChange);
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)
39 if (typeof aEvent !== "function")
42 var bound = _.bind( aEvent, scope );
44 codeMirror.on(events[i], bound);
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) {
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');
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);
67 $timeout(deferCodeMirror);
73 function DevCtrl( $scope, $http ) {
74 var templateHtml = "<html><body>\n"
75 + " <button id='btn'>Hello!</button>\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"
106 + " var vm = new bck2brwsr(ldCls);\n"
107 + " vm.loadClass('bck2brwsr.demo.Index');\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"
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();
129 $scope.post = function(html, java) {
130 return $http({url: ".",
132 //headers: this.headers,
133 data: { html : $scope.html, java : $scope.java}
134 }).success( $scope.reload );
138 $scope.html= templateHtml;
139 $scope.java = templateJava;
141 $scope.tabActive = function( tab ) {
142 return tab === $scope.tab ? "active" : "";
145 // $scope.$watch( "html", htmlChange );