Commit cc2b03361dbe55a6feec9649b193f71083d03e27

Authored by development server
0 parents
Exists in master

initial commit

.gitignore
... ... @@ -0,0 +1,2 @@
  1 +/node_modules/
  2 +/vendor/
... ...
Gruntfile.js
... ... @@ -0,0 +1,24 @@
  1 +'use strict';
  2 +
  3 +module.exports = function (grunt) {
  4 + //load grunt tasks
  5 + grunt.loadNpmTasks('grunt-contrib-uglify');
  6 + grunt.initConfig({
  7 + uglify: {
  8 + options: {
  9 + compress: true,
  10 + output: {
  11 + comments: false
  12 + }
  13 + },
  14 + dist: {
  15 + files: {
  16 + 'dist/cookie_utils.min.js': ['src/cookie_utils.js'],
  17 + 'dist/page_loader.min.js': ['src/page_loader.js']
  18 + }
  19 + }
  20 + }
  21 + });
  22 + grunt.registerTask('default', ['uglify']);
  23 + grunt.registerTask('dist', ['uglify']);
  24 +}
... ...
README.md
... ... @@ -0,0 +1,20 @@
  1 +# Webapp tools - common javascript utilities
  2 +
  3 +This presents a collection of javascript libraries to use on web applications. It is designed with the [FCCN's webapp skeleton project](https://github.com/fccn/webapp-skeleton.git) in mind. The following utilities are provided:
  4 +
  5 + - **page loader** - jquery extension for loading and refreshing page content via ajax calls
  6 + - **cookie utilities** - collection of functions to manage cookies
  7 +
  8 +## Installation
  9 +
  10 +You can install this collection in your project using composer:
  11 +```
  12 +composer require fccn/webapp-tools/common-js
  13 +
  14 +```
  15 +
  16 +## Configuration
  17 +
  18 +
  19 +
  20 +## Usage
... ...
composer.json
... ... @@ -0,0 +1,18 @@
  1 +{
  2 + "name": "fccn/webapp-tools/common-js",
  3 + "description": "Collection of javascript libraries for web applications",
  4 + "keywords": ["fccn", "webapp-tools", "javascript", "utilities"],
  5 + "license": "MIT",
  6 + "authors": [
  7 + {
  8 + "name": "Paulo Costa",
  9 + "email": "paulo.costa@fccn.pt"
  10 + }
  11 + ],
  12 + "require": {
  13 + },
  14 + "config": {
  15 + "process-timeout" : 0
  16 + }
  17 +
  18 +}
... ...
dist/cookie_utils.min.js
... ... @@ -0,0 +1 @@
  1 +function readCookie(e){for(var t=e+"=",n=document.cookie.split(";"),o=0;o<n.length;o++){for(var i=n[o];" "==i.charAt(0);)i=i.substring(1,i.length);if(0==i.indexOf(t))return i.substring(t.length,i.length)}return null}function createCookie(e,t,n){var o;if(n){var i=new Date;i.setTime(i.getTime()+24*n*60*60*1e3),o="; expires="+i.toGMTString()}else o="";document.cookie=escape(e)+"="+escape(t)+o+"; path=/"}function deleteCookie(e){document.cookie=e+"=; expires=Thu, 01 Jan 1970 00:00:01 GMT;"}
0 2 \ No newline at end of file
... ...
dist/page_loader.min.js
... ... @@ -0,0 +1 @@
  1 +function putData(t,n){$.ajax({processData:!1,url:t,type:"PUT",data:JSON.stringify(n),contentType:"application/json",dataType:"JSON",success:function(t){console.log("putData Success: "+JSON.stringify(t))},error:function(t){console.log("putData Error: "+JSON.stringify(t))}})}function getData(t,n){$.ajax({url:t,type:"GET",success:function(t){n&&n(t.result)},error:function(t){console.log("getData Error:"+JSON.stringify(t)),data={error:1},n&&n(data)}})}function cancelAutoRefresh(t){clearInterval(t)}function openSocket(t){var n=io.connect(t,{secure:!0,transports:["websocket"]});return n.on("connection",function(n){console.log("connected to: "+t),n.on("disconnect",function(){console.log("disconnected from: "+t)})}),n}function sendData(t,n,o){t.emit(n,o)}function pullData(t,n,o,e){var r=t,a=n,c=o;return sendData(r,a,c),window.setInterval(function(){sendData(r,a,c)},e)}jQuery.fn.extend({check:function(){return this.each(function(){this.checked=!0})},uncheck:function(){return this.each(function(){this.checked=!1})},updateContent:function(){$(this).getContent($(this).data("url"))},getContent:function(t,n){$(this).data("url",t);var o=$(this);o.html('<div class="loader"></div>'),$.ajax({url:t,type:"GET",success:function(t){o.html(t.result.html),n&&n()},error:function(t){console.log("$().getContent Error: "+JSON.stringify(t))}})},postContent:function(t,n,o){$(this).data("url",t);var e=$(this);$.ajax({url:t,type:"POST",data:n,success:function(t){e.html(t.result.html),o&&o()},error:function(t){console.log("$().getContent Error: "+JSON.stringify(t))}})},getData:function(t,n){$(this).data("url",t);var o=$(this);$.ajax({url:t,type:"GET",success:function(t){console.log(o),console.log(JSON.stringify(t.result)),o.data("result",t.result),n&&n(t.result)},error:function(t){console.log("$().getData Error: "+JSON.stringify(t)),data={error:1},o.data("result",data),n&&n(data)}})},autoRefreshContent:function(t,n,o){var e=$(this);if(e.getContent(t,o),0!=n)return window.setInterval(function(){e.getContent(t,o)},n)},autoRefreshData:function(t,n,o){var e=$(this);return e.getData(t,o),window.setInterval(function(){e.getData(t,o)},n)},bindContent:function(t,n){var o=$(this);t.on(n,function(t){o.html(t.html)})},bindData:function(t,n,o){t.on(n,function(t){o(t)})}});
0 2 \ No newline at end of file
... ...
package.json
... ... @@ -0,0 +1,11 @@
  1 +{
  2 + "name": "webapp-tools-common-js",
  3 + "version": "1.0.0",
  4 + "license": "MIT",
  5 + "repository": "https://gitlab.fccn.pt/stv-servico-tecnico-de-video/wt-common-js.git",
  6 + "devDependencies": {
  7 + "grunt": "~0.4.5",
  8 + "grunt-contrib-uglify": "^3.1.0"
  9 + },
  10 + "dependencies": {}
  11 +}
... ...
src/cookie_utils.js
... ... @@ -0,0 +1,29 @@
  1 +function readCookie(name) {
  2 + var nameEQ = name + "=";
  3 + var ca = document.cookie.split(';');
  4 +
  5 + for (var i = 0; i < ca.length; i++) {
  6 + var c = ca[i];
  7 + while (c.charAt(0) == ' ') c = c.substring(1, c.length);
  8 + if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  9 + }
  10 + return null;
  11 +}
  12 +
  13 +function createCookie(name, value, days) {
  14 + var expires;
  15 +
  16 + if (days) {
  17 + var date = new Date();
  18 + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  19 + expires = "; expires=" + date.toGMTString();
  20 + } else {
  21 + expires = "";
  22 + }
  23 +
  24 + document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
  25 +}
  26 +
  27 +function deleteCookie(name) {
  28 + document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  29 +}
... ...
src/page_loader.js
... ... @@ -0,0 +1,209 @@
  1 +/*
  2 + * Page loader and jquery extension for loading and refreshing page content
  3 + */
  4 +jQuery.fn.extend({
  5 +
  6 + check: function() {
  7 + return this.each(function() {
  8 + this.checked = true;
  9 + });
  10 + },
  11 +
  12 + uncheck: function() {
  13 + return this.each(function() {
  14 + this.checked = false;
  15 + });
  16 + },
  17 +
  18 + updateContent: function() {
  19 + $(this).getContent($(this).data("url"));
  20 + },
  21 +
  22 + getContent: function(url, func) {
  23 + $(this).data("url", url);
  24 + var element = $(this);
  25 + element.html('<div class="loader"></div>');
  26 + $.ajax({
  27 + url: url,
  28 + type: 'GET',
  29 + success: function(data) {
  30 + //called when successful
  31 + //console.log(element);
  32 + //console.log(JSON.stringify(data.result.html));
  33 + element.html(data.result.html);
  34 + if (func)
  35 + func();
  36 + },
  37 + error: function(e) {
  38 + //called when there is an error
  39 + console.log("$().getContent Error: " + JSON.stringify(e));
  40 + }
  41 + });
  42 + },
  43 +
  44 + postContent: function(url, post_data, func) {
  45 + $(this).data("url", url);
  46 + var element = $(this);
  47 + $.ajax({
  48 + url: url,
  49 + type: 'POST',
  50 + data: post_data,
  51 + success: function(data) {
  52 + //called when successful
  53 + //console.log(element);
  54 + //console.log(JSON.stringify(data.result.html));
  55 + element.html(data.result.html);
  56 + if (func)
  57 + func();
  58 + },
  59 + error: function(e) {
  60 + //called when there is an error
  61 + console.log("$().getContent Error: " + JSON.stringify(e));
  62 + }
  63 + });
  64 + },
  65 +
  66 + getData: function(url, func) {
  67 + $(this).data("url", url);
  68 + var element = $(this);
  69 + $.ajax({
  70 + url: url,
  71 + type: 'GET',
  72 + success: function(data) {
  73 + //called when successful
  74 + console.log(element);
  75 + console.log(JSON.stringify(data.result));
  76 + element.data("result", data.result);
  77 + if (func)
  78 + func(data.result);
  79 + },
  80 + error: function(e) {
  81 + //called when there is an error
  82 + console.log("$().getData Error: " + JSON.stringify(e));
  83 + data = {
  84 + error: 1
  85 + };
  86 + element.data("result", data);
  87 + if (func)
  88 + func(data);
  89 + }
  90 + });
  91 + },
  92 +
  93 + autoRefreshContent: function(url, milliseconds, func) {
  94 + var element = $(this);
  95 + element.getContent(url, func);
  96 + if (milliseconds == 0) return;
  97 + return window.setInterval(function() {
  98 + element.getContent(url, func);
  99 + }, milliseconds);
  100 + },
  101 +
  102 + autoRefreshData: function(url, milliseconds, func) {
  103 + var element = $(this);
  104 + element.getData(url, func);
  105 + return window.setInterval(function() {
  106 + element.getData(url, func);
  107 + }, milliseconds);
  108 + },
  109 +
  110 + bindContent: function(socket, context) {
  111 + var element = $(this);
  112 + // console.log('bindContent received!');
  113 + socket.on(context, function(msg) {
  114 + // console.log('bindContent message: ' + msg);
  115 + element.html(msg.html);
  116 + });
  117 + },
  118 +
  119 + bindData: function(socket, context, cbfunc) {
  120 + // console.log('bindData received!');
  121 + socket.on(context, function(msg) {
  122 + // console.log('bindData message: ' + msg);
  123 + cbfunc(msg);
  124 + });
  125 + }
  126 +});
  127 +
  128 +function putData(url, data) {
  129 +
  130 + $.ajax({
  131 + processData: false,
  132 + url: url,
  133 + type: 'PUT',
  134 + data: JSON.stringify(data),
  135 + contentType: 'application/json',
  136 + dataType: 'JSON',
  137 + success: function(data) {
  138 + //called when successful
  139 + console.log("putData Success: " + JSON.stringify(data));
  140 + },
  141 + error: function(e) {
  142 + //called when there is an error
  143 + console.log("putData Error: " + JSON.stringify(e));
  144 + }
  145 + });
  146 +} // put comma here if you want to add more functions
  147 +
  148 +
  149 +function getData(url, func) {
  150 + $.ajax({
  151 + url: url,
  152 + type: 'GET',
  153 + success: function(data) {
  154 + //called when successful
  155 + if (func)
  156 + func(data.result);
  157 + },
  158 + error: function(e) {
  159 + //called when there is an error
  160 + console.log("getData Error:" + JSON.stringify(e));
  161 + data = {
  162 + error: 1
  163 + };
  164 + if (func)
  165 + func(data);
  166 + }
  167 + });
  168 +};
  169 +
  170 +
  171 +function cancelAutoRefresh(handle) {
  172 + clearInterval(handle);
  173 +}
  174 +
  175 +// URL : https://{hostname}:{port}/{namespace}
  176 +function openSocket(url) {
  177 + var socket = io.connect(url, {
  178 + secure: true,
  179 + transports: ['websocket']
  180 + });
  181 +
  182 + socket.on('connection', function(socket) {
  183 + console.log('connected to: ' + url);
  184 +
  185 + socket.on('disconnect', function() {
  186 + console.log('disconnected from: ' + url);
  187 + });
  188 + });
  189 +
  190 + return socket;
  191 +}
  192 +
  193 +function sendData(socket, context, data) {
  194 + socket.emit(context, data);
  195 +}
  196 +
  197 +function pullData(socket, context, data, milliseconds) {
  198 +
  199 + var localSocket = socket;
  200 + var localContext = context;
  201 + var localData = data;
  202 +
  203 + sendData(localSocket, localContext, localData);
  204 +
  205 + return window.setInterval(function() {
  206 + sendData(localSocket, localContext, localData)
  207 + }, milliseconds);
  208 +
  209 +};
... ...