15 awesome Sublime Text plugins for web development

http://tutorialzine.com/2016/10/15-awesome-sublime-text-plugins-for-web-development/

MEAN.JS 0.4.2 + Leaflet

MEAN.JS + Leaflet screenshot

MEAN.JS is one of the “Open-Source Full-Stack solutions” for developing MEAN applications freely available. There’s other equally acceptable solutions out there, each of them with their particularities.

MEAN applications are those that integrates MongoDB, ExpressJS, AngularJS and Node.js, and it’s a configuration that it’s being used more and more frequently in the last years. There are many options available, sometimes with very similar names (MEAN.io from Linnovate, and MEAN.JS as a community-governed project from the same creator: Amos Haviv) or technologies still in development (Angular2 is used in some generators even when it’s still in beta at the moment of writing this article). The speed in which a stack solution, generator, framework, or any other starting point for MEAN development, is released and became outdated is astonishing fast. Sometimes, even updates implies a new approach that requires the change of the structure of the code. This makes very confusing for any coder willing to start learning with the tools available.

Leaflet is an “Open-Source JavaScript library for mobile-friendly interactive maps”. It’s very powerful and easy to use, and with the addition of Stamen we can make eye-candy maps very easily.

So, getting to the point. What’s the easiest way to integrate MEAN.JS and Leaflet? In around 5 minutes, and with a little help of copy-paste, we can have a working example.

Installing MEAN.JS

You will require a working installation of node.js.  That’s out of the scope in this article, but in treehouse is easily explained. You’ll need a reboot in your machine in order to be sure that the folders are properly added to the path.

The best starting point is using the yo scaffolding tool:

npm install -g yo
npm install -g generator-meanjs

Then, we need to create a fresh copy of MEAN.JS. In this example, I will be using MEAN 0.4.2. That’s the version you should initiate if you want to be sure that my tutorial is working. I’ll ask the generator to create all the available examples. They are quite convenient in order to understand how the code should be structured.

yo meanjs

Let’s say you create a project folder named mean-leaf. Then, you’ll need to get into that folder.

cd mean-leaf

You will need a working global installation of Grunt and Bower. So, if you need to install them:

npm install -g grunt-cli
npm install -g bower

We add the angular-leaflet-directive:

bower install angular-leaflet-directive --save

And now, we use Grunt to run the application:

grunt

If you have the error “You need to have Ruby and Sass installed and in your PATH for this task to work”, just execute the following and run grunt again:

sudo gem install sass

Adding the Leaflet maps

With the previous steps, MEAN.js should be up and running. Now, we are going to integrate the Leaflet map. More examples can be found in the angular-leaflet-directive Github page, but I found the “Center example” the most appropriate for having an initial taste.

Remember that you’ll need the angular-leaflet-directive installed as mentioned above.

Adding the required JavaScript and CSS files

We’ll need to add the leaflet CSS and JavaScript files to config/assets/default.js. I found quite useful using the CDN versions, so that’s what I’ll show here. The code will be more or less this (just showing the modified sections):

// (...)

css: [
 'modules/*/client/css/*.css',
 'https://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet.css'
 ],

// (...)

js: [
 'modules/core/client/app/config.js',
 'modules/core/client/app/init.js',
 'modules/*/client/*.js',
 'modules/*/client/**/*.js',
 'https://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet.js'
 ],

// (...)

This works the same with any other external CSS or JavaScript file you want to add.

Using the directive

In the file modules/core/client/app/config.js we need to add the directive to the dependences. We will just add it to the array in line 7:

// (...)

var applicationModuleVendorDependencies = ['ngResource', 'ngAnimate', 'ngMessages', 'ui.router', 'ui.bootstrap', 'ui.utils', 'angularFileUpload', 'leaflet-directive'];

// (...)

Now, we need to add the JavaScript to /config/assets/default.js. Is the same file we used previously, but now we’re adding a line in the client > lib > js section. After the changes, it will be something like this:

// (...)

js: [
 'public/lib/angular/angular.js',
 'public/lib/angular-resource/angular-resource.js',
 'public/lib/angular-animate/angular-animate.js',
 'public/lib/angular-messages/angular-messages.js',
 'public/lib/angular-ui-router/release/angular-ui-router.js',
 'public/lib/angular-ui-utils/ui-utils.js',
 'public/lib/angular-bootstrap/ui-bootstrap-tpls.js',
 'public/lib/angular-file-upload/angular-file-upload.js',
 'public/lib/owasp-password-strength-test/owasp-password-strength-test.js',
 'public/lib/angular-leaflet-directive/dist/angular-leaflet-directive.min.js'
 ],

// (...)

Inserting a map

We will add the following code somewhere in our view /modules/core/client/views/home.client.view.html to insert the map:

<div>

<div>
 <leaflet center="center" markers="markers" url-hash-center="yes" tiles ="tiles" height="400"></leaflet>
 </div>
 <br/>
 <div>
 <form class="text-center">
 Latitude :
 <input type="number" step="any" ng-model="center.lat"> Longitude :
 <input type="number" step="any" ng-model="center.lng"> Zoom :
 <input type="number" step="any" ng-model="center.zoom">
 </form>

</div>

</div>

But that’s is not enough. We need do some modifications to the code of the controller modules/core/client/controllers/home.client.controller.js (be very careful with the indentation here, WordPress is breaking the code and it’s important):

'use strict';

angular.module('core').controller('HomeController', ['$scope', 'Authentication',
 function($scope, Authentication) {
 // This provides Authentication context.
 $scope.authentication = Authentication;

angular.extend($scope, {
 center: {
 lat: 51.528308,
 lng: -0.3817736,
 zoom: 7
 },
 defaults: {
 scrollWheelZoom: false
 },
 tiles: {
 Name: 'Stamen Toner Lite',
 url: 'http://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}.{ext}',
 options: {
 ext: 'png',
 attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
 }
 }
 });
 }
]);

This should be enough. Just run grunt and visit your website. If you have errors with eslint and the indentation, just edit modules/core/client/controllers/home.client.controller.js and make the modifications displayed in the console.

The result should be something like the following screenshot:

MEAN.JS + Leaflet screenshot

The example used for this article, and many others, are available in the angular-leaflet-directive examples gallery, and also in their Github.