Monday 30 December 2019

Setting up Webpack

webpack.config.js

var path = require('path');var webpack = require('webpack');
module.exports = {
  mode: 'development',  entry: './js/main.js',  output: {
    path: path.resolve(__dirname, 'build'),    filename: 'main.bundle.js'  },  module: {
    rules: [
      {
        test: /\.js$/,        loader: 'babel-loader',        query: {
          presets: ['@babel/preset-env']
        }
      }
    ]
  },  stats: {
    colors: true  },  devtool: 'source-map',};

npm install @babel/core --save
npm install @babel/preset-env --save





Saturday 2 February 2013

HTML5 Linking external JavaScript

In order to make JavaScript works externally you need to specify within the JS function to do it once the content is available.

js:
document.addEventListener('DOMContentLoaded', drawCanvas, false);
function drawCanvas() {
    var c = document.getElementById("myCanvas");
    var ctx = c.getContext("2d");
    ctx.fillStyle = "#FF0000";
    ctx.fillRect(0,0,150,75);
}

html:
<html>
<head>
  <script src="common/js.js"></script>
</head>
<body>
  <canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000"></canvas>
</body>
</html>

Thursday 15 March 2012

Difference betwen two dates in JavaScript

// LM is the number of days in one year = 365.25/12 days
var ds, aux, D0, D1, DD, DM, DY, LM = 30.4375;

// The returned format date is dd-mm-yy
// JavaScript Date object uses yy-mm-dd
ds = getValue("CrmForm_contract_sdate");
var aux = ds.split("-");
ds = aux[2] + "-" + aux[1] + "-" + aux[0];
D0 = new Date(ds.replace(/\D+/g, "/"));

ds = getValue("Calculator_contract_edate");
aux = ds.split("-");
ds = aux[2] + "-" + aux[1] + "-" + aux[0];
D1 = new Date(ds.replace(/\D+/g, "/"));

// D0 and D1 have the representation of the Date in miliseconds
// Now a day has 24*60*60*1000 miliseconds
DD = Math.round((D1 - D0) / 86400000);
DM = DD / LM | 0;
DD = Math.round(DD - DM * LM);
DY = DM / 12 | 0;
DM %= 12;

aux = DY + "Year";
if (DY>1 || DY==0)
    aux += "s, ";
else
    aux += ", ";
aux += DM + "Month";
if (DM>1 || DM==0)
    aux += "s, ";
else
    aux += ", ";
aux += DD + "Day";
if (DD>1 || DD==0)
    aux += "s, ";
else
    aux += ", ";

$("#lnyears").html("<font color=\"red\" size=\"3\">" + aux + "</font>");

Monday 20 February 2012

Setting up Git to manage a website project

In the server:

This is where your code lives, in your DocumentRoot:
$ cd /var/www
$ mkdir mysite

This is the remote repository:
$ cd /home/user
$ mkdir mysite.git && mysite.git
$ git init --bare
Initialized empty Git repository in /home/user/mysite.git/
$ vi mysite.git/hooks/post-receive
~
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
~
~
~

And in the local machines:

$ cd /var/www
$ mkdir mysite && cd mysite
$ git init
Initialized empty Git repository in /var/www/mysite/.git/

$ git remote add web ssh://<user>@<server_url>/home/user/mysite.git
$ git push web +master:refs/heads/master

$ echo 'Hello, world!' > index.html
$ git add index.html
$ git commit -m "first commit"
$ git push web

Then:
Another user could clone the remote repository:
git clone ssh://<user>@<server_url>/home/user/mysite.git


Monday 13 February 2012

Accessing MySQL from PHP error

I was getting this weird error on Yii:
CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2013] Lost connection to MySQL server at 'reading initial communication packet', system error: 111

If you are accessing MySQL from PHP and both reside on the same server you must use localhost as the server name on the MySQL configuration file. Like this one I had on my Yii application.

On my Yii config/main.php I changed this:
'connectionString' => 'mysql:host=<host_url>;dbname=<db_name>',

For this:
'connectionString' => 'mysql:host=localhost;dbname=<db_name>',

This post saved my life: MySQL Forums