lib/popcorn: introduce Angular.js example
authorAlexandre Terrasa <alexandre@moz-code.org>
Mon, 16 May 2016 21:14:12 +0000 (17:14 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 19 May 2016 21:33:53 +0000 (17:33 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/popcorn/examples/angular/example_angular.nit [new file with mode: 0644]
lib/popcorn/examples/angular/www/index.html [new file with mode: 0644]
lib/popcorn/examples/angular/www/javascripts/ng-example.js [new file with mode: 0644]
lib/popcorn/examples/angular/www/views/index.html [new file with mode: 0644]
lib/popcorn/tests/res/test_example_angular.res [new file with mode: 0644]
lib/popcorn/tests/test_example_angular.nit [new file with mode: 0644]

diff --git a/lib/popcorn/examples/angular/example_angular.nit b/lib/popcorn/examples/angular/example_angular.nit
new file mode 100644 (file)
index 0000000..ba7f4f2
--- /dev/null
@@ -0,0 +1,44 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014-2015 Alexandre Terrasa <alexandre@moz-code.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import popcorn
+
+class CounterAPI
+       super Handler
+
+       var counter = 0
+
+       fun json_counter: JsonObject do
+               var json = new JsonObject
+               json["label"] = "Visitors"
+               json["value"] = counter
+               return json
+       end
+
+       redef fun get(req, res) do
+               res.json(json_counter)
+       end
+
+       redef fun post(req, res) do
+               counter += 1
+               res.json(json_counter)
+       end
+end
+
+var app = new App
+app.use("/counter", new CounterAPI)
+app.use("/*", new StaticHandler("www/"))
+app.listen("localhost", 3000)
diff --git a/lib/popcorn/examples/angular/www/index.html b/lib/popcorn/examples/angular/www/index.html
new file mode 100644 (file)
index 0000000..59bc4d1
--- /dev/null
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html lang='en' ng-app='ng-example'>
+       <head>
+               <base href='/'>
+               <title>ng-example</title>
+       </head>
+       <body>
+               <div ng-view></div>
+
+               <script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js'></script>
+               <script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular-route.js'></script>
+               <script src='/javascripts/ng-example.js'></script>
+       </body>
+</html>
diff --git a/lib/popcorn/examples/angular/www/javascripts/ng-example.js b/lib/popcorn/examples/angular/www/javascripts/ng-example.js
new file mode 100644 (file)
index 0000000..f9270d2
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+(function() {
+       angular
+
+       .module('ng-example', ['ngRoute'])
+
+       .factory('CounterModel', ['$http', function($http) {
+               return {
+                       load: function(cb, cbErr) {
+                               $http.get('/counter')
+                                       .success(cb)
+                                       .error(cbErr);
+                       },
+                       increment: function(cb, cbErr) {
+                               $http.post('/counter')
+                                       .success(cb)
+                                       .error(cbErr);
+                       }
+               };
+       }])
+
+       .controller('CounterCtrl', ['CounterModel', function(CounterModel) {
+               var $this = this;
+
+               this.loadCounter = function() {
+                       CounterModel.load(
+                               function(data) {
+                                       $this.counter = data;
+                               }, function(err) {
+                                       $this.error = err;
+                               });
+               };
+
+               this.incrementCounter = function() {
+                       CounterModel.increment(
+                               function(data) {
+                                       $this.counter = data;
+                               }, function(err) {
+                                       $this.error = err;
+                               });
+               };
+
+               this.loadCounter();
+       }])
+
+       .config(function($routeProvider, $locationProvider) {
+               $routeProvider
+                       .when('/', {
+                               templateUrl: 'views/index.html',
+                               controller: 'CounterCtrl',
+                               controllerAs: 'counterCtrl'
+                       })
+                       .otherwise({
+                               redirectTo: '/'
+                       });
+               $locationProvider.html5Mode(true);
+       });
+})();
diff --git a/lib/popcorn/examples/angular/www/views/index.html b/lib/popcorn/examples/angular/www/views/index.html
new file mode 100644 (file)
index 0000000..058e799
--- /dev/null
@@ -0,0 +1,9 @@
+<h1>Nit &hearts; Angular.JS</h1>
+
+<p>Click the button to increment the counter.</p>
+
+<form>
+       <label>{{counterCtrl.counter.label}}</label>
+       <input type="number" ng-model="counterCtrl.counter.value" readonly />
+       <button ng-click="counterCtrl.incrementCounter()">Increment!</button>
+</form>
diff --git a/lib/popcorn/tests/res/test_example_angular.res b/lib/popcorn/tests/res/test_example_angular.res
new file mode 100644 (file)
index 0000000..c07813f
--- /dev/null
@@ -0,0 +1,7 @@
+
+[Client] curl -s localhost:*****/counter
+{"label":"Visitors","value":0}
+[Client] curl -s localhost:*****/counter -X POST
+{"label":"Visitors","value":1}
+[Client] curl -s localhost:*****/counter
+{"label":"Visitors","value":1}
\ No newline at end of file
diff --git a/lib/popcorn/tests/test_example_angular.nit b/lib/popcorn/tests/test_example_angular.nit
new file mode 100644 (file)
index 0000000..d1ab509
--- /dev/null
@@ -0,0 +1,47 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import example_angular
+import base_tests
+
+class TestClient
+       super ClientThread
+
+       redef fun main do
+               system "curl -s {host}:{port}/counter"
+               system "curl -s {host}:{port}/counter -X POST"
+               system "curl -s {host}:{port}/counter"
+               return null
+       end
+end
+
+var app = new App
+app.use("/counter", new CounterAPI)
+app.use("/*", new StaticHandler("www/"))
+
+var host = test_host
+var port = test_port
+
+var server = new AppThread(host, port, app)
+server.start
+0.1.sleep
+
+var client = new TestClient(host, port)
+client.start
+client.join
+0.1.sleep
+
+exit 0