Wednesday, 30 July 2014

Creating a Star Control as element Tag or Attribute in HTML using Angular Js

In this article we are going to see how to create a Star Rating control in the HTML as Element or as Attribute using the Angular js. To do this we are going to take create the Directive in Angular js, First Let we see what kind of element or Attribute we are going to create and what kind of output it will give ?

Below Code Snippet is we trying to do



Output:




From the Code we can render a Star Control, using the some of the properties it will have some  behavior change.

custom-rating is the act as element or attribute for render a star control, it have few options
readonly : set true for this attribute if you want the star control as readonly
max : set the number of star you want to render
star-size : size of the star control
rating-value : specify the rating value of a control
on event is there to trigger for the change in the star value when user clicked, then use the 
on-rating-changed with a function passing parameter in the name of rating , which consists the value, now you can trigger the value to the server.

Now we start to build the directive.

Add the following jquery in the HTML.


    <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>

    <script type="text/javascript" src="js/angular.min.js"></script>

Then start writing coding in Angular.

Create a Module and controller

First step is create a module in separate js file and include in the HTML.

        var myApp = angular.module('myApp', []);

        myApp.controller('BlogController', function ($scope, $window) {
            $scope.sample = 'Blog Testing';

            $scope.rating = 5;
            $scope.rating2 = 1;

            $scope.saveRating = function (rating) {
                console.log('Rating selected ' + rating);
            };

            $scope.Sample = function (rating) {
                console.log('sample ' + rating);
            };

        });


Create a Directive

        myApp.directive('customRating', function () {
            return {
                restrict: 'AE',
                template: '<ul class="rating">' +
                         '<li ng-repeat="star in stars"  ng-class="star" ng-                                                click="toggle($index)">\u2605</li>' +
                         '</ul>',
                scope:
                {
                    ratingValue: '=',
                    max: '=',
                    readonly: '@',
                    starSize: '=',
                    onRatingChanged: '&'
                },
                link: function (scope, element, attrs) {

                   
                    var updateStars = function () {
                        scope.stars = [];
                        for (var i = 0; i < scope.max; i++) {
                            scope.stars.push({ filled: i < scope.ratingValue });
                        }
                    };

                    element.css('font-size', scope.starSize);

                   
                    scope.toggle = function (index) {
                        if (scope.readonly && scope.readonly === 'true') {
                            return;
                        }
                        scope.ratingValue = index + 1;

                        scope.onRatingChanged({ rating: index + 1 });
                    };

                   
                    scope.$watch('ratingValue', function (oldval, newval) {
                        if (newval) {
                            updateStars();
                        }
                    })

                }

            }
        });



So now when include the custom js in the Html, we can able to render the Star control .Now when ever user change the value in one star it affects others because of binding, at the same time we are trigger the changes to the server.

From this article you can learn how to render a star control tag in Html using Angular Js.

Friday, 25 July 2014

Installing the Extension in Visual studio 2013 results in The Extension Manifest in Invalid [Resolved]

In this article we are going to see some problems and there solutions, for example you may had came across a error when installing a extension in Visual studio 2013 like "Extension Manifest is Invalid".




Why this error occur ?
  This is because in your system , you may have various versions of visual studio when double click the extension it target the wrong  version , i.e you may have visual studio 2013 in ur sysetm , later you are installing Visual studio 2010 for some purpose now this causes the porblem.

To solve this problem many of them will re install the visual studio 2013, then it will  work , to avoid this we can do this simple steps.

Steps:

1. Right click the extension and select Open with options.



2. In the above options select more Options to browse
3.Then scroll down to click the Look for another app in this PC to Browse 




4. Go to the Following Vs installation path 
   "{{installtion Drive}}\Program Files(x86)\Microsoft Visual studio 12.0\Common 7\IDE\VSIXinstaller.exe"        to browse for vsix installer.





5.After select this you can see the icon for the Extension will change as like this 


From this article you can solve some problem in the installation of extension in the visual studio 2013.

Unable to connect TFS with in VS2013 in windows 8.1 - Resolved

    In this article we are going to see some problem and it's solutions, sometimes you can notice that unable to connect TFS with in Visual studio 2013 in windows 8.1, But it will work in some other visual studio versions.

Error Message will be like this format :
Unable to connect to this Team Foundation Server http://{{server}}:8080/tfs

Why this error occur ? In depth you can see a another error message that specifies "key not valid to use in specified state when entering source control

To Avoid this error and connect the TFS successfully with visual studio follow the below steps.


Solution:

1. Close all instance of the Visual studio 2013.
2. Delete the following Key "TokenStorage" from RegEdit, to do that follow the below steps
3. press Windows + R
4. Type RegEdit in run window and enter.
5. Now Register Editor will launch.
6. Iterate to the following key in Regedit and Delete the Key
7. HKEY_CURRENT_USER\Software\Microsoft\VSCommon\12.0\ClientServices\TokenStorage


From this article you can solve problems related to TFS connect with visual studio.

Wednesday, 16 July 2014

Get the Current Goe Location using Google API in HTML

                     In this article we are going to see how to get the current geo location code , using the google API, For this we have to enable the Google GeoCode API service , then click the enable button when the first time browser raises a pop up to show map, To build a map like below one, we have to do the following steps. Use the mouse to zoom in and out



Start wirh HTML and then Javascript,
   <form id="mapform" runat="server">
        <div id="mapcanvas" style="width: 500px; height: 400px"></div>

    </form>

Create a Div element to place the map.

Next to get the google maps , we need a server call api key from google, for this do the simple three steps.
Go to the

  1. Visit the APIs console at https://code.google.com/apis/console and log in with your Google Account.
  2. Click the Services link from the left-hand menu in the APIs Console, then activate the Geocoding API service.
  3. Once the service has been activated, your API key is available from the API Access page, in the Simple API Access section. Geocoding API applications use the Key for server apps.
Then Use that key in the script call. like below.

<script type="text/javascript" 
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCZE2VTm32UQuqTgb3KgP4&sensor=false"></script>


    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>

Then create a JavaScript to get the current location.
First check whether the Geo location is supported in browser,  for this we can test in navigator.geolocation.
Once it is passed, then bind a callback function, which have a position as input paramter. From the position we can get the coordinates.

Now the Next thing is Draw the map using the Geocodes, so to do this we have to create a Google.Maps.Map instance and Marker.

First pass the latitude and longitude as parameter to a instance. then pass that instance to a options parameter center:, Next get the element tag where to show the map, along with options in Map instance. now use the marker instance to set the map.Next create a infowindow with a content parameter.

 <script type="text/javascript">
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(success);
        }
        else {
            alert("GeoLocation is not supported in your browser");
        }

        function success(position) {
            debugger;
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            var city = position.coords.locality;
            var myLatlng = new google.maps.LatLng(latitude, longitude);
            var myOptions = {
                center: myLatlng,
                zoom: 12,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions);
            var marker = new google.maps.Marker({
                position: myLatlng,
                title: "lat: " + latitude + " long: " + longitude
            });

            marker.setMap(map);
            var infowindow = new google.maps.InfoWindow({ content: "<b>Address</b><br/>                Latitude:" + latitude + "<br /> Longitude:" + longitude + "" });
            infowindow.open(map, marker);
        }


    </script>


 From this post you can learn how to get the location of current place using google GeoCode api.

Saturday, 5 July 2014

Databinding using Handlebars js

In this article we are going to see how to create a sample web page using handlebars.js,  For this i am taking the cookbook as sample, one template is written for recipes.


First open a Notepad++, create a html file name first.html and save it under a folder MVC.

Download the Jquery.js from the following link Jquery place inside a folder MVC.
Download  the Handlebars.js from the following link Handlebars place MVC.

Now link the scripts in the Head tag,

<head>
    <script src="jquery-1.11.1.min.js" type="text/javascript"></script>
    <script src="handlebars-v1.3.0.js" type="text/javascript"></script>   
</head>
   

Then create a Handler Template inside a script tag, mention the type as "text/x-handlebars-template".



<script id="sampletemplate" type="text/x-handlebars-template"> 
    <div id="content">
    <center>
    <h1  style="margin-left:10px;color:white">&Xi;RecipeMessages
    <span class="small">Messages search powered by <a id="Logo">   
    </center>
    </h1>
    <div style="margin-left:137px;height:150px:width:100px">
    <img style="color:orange;width:310px" src="images/SplashScreen.png"></img>
    </div>
    {{#each recipes}}
    <div class="" style="float:left;background-color:whitesmoke;
                  padding:10px;margin-left:15px;margin-right:15px">
        <img class="Thumb" src="{{Image}}" alt="{{Name}}"/>
       
       <h3>{{Name}} - 

        <span style="color:orange;font-size:15px">
            {{#if Source}} 
                    {{Source.Name}} 
            {{else}} No  
            {{/if}}
        </span>

        </h3>

        <p style="width:230px">{{Description}}</p>
        <h5 style="color:blue">{{getflavor Flavors}}</h5>

        <h5>{{Yield}}</h5>
        <p>Incredients :</p>
        <ul>
        {{#each Ingredients}}
            <li>{{this}}</li>
        {{/each}}
        </ul>
    </div>
    {{/each}}
    </div>
</script>

From the above template you can notice that double curl braces {{}} the parametere present inside is the exact json property name, and some of keywords are used like #each for foreach iteration of an array, #if for check the condition

Next, Compile the template in the script and bind the data with an anonymous function.



<script>
(function(){

    Handlebars.registerHelper('getflavor',function(flavorcontext){     
        return "This dish has a "+flavorcontext.type+" with "+flavorcontext.flavor+" Flavor.";
    });

    var context ={
        recipes : [
            {  
              "Image":"images/1.jpg",
              "Name":"Briyaani",
              "Description":"Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
              "Source":{"Name":"All Recipes"},
              "Flavors":{"type":"Non-Veg","flavor":"Garam"},
              "Yield":"10 pieces of chicken",
              "Ingredients":[ "600 g Ground pork",
                                "1 tsp Salt",
                                "1 tsp sugar",
                                "1/2 tsp Ground black pepper",
                                "2 tsp Cornstarch",
                                "1 tbsp Sesame oil",
                                "1 handful Sliced dried mushrooms ",
                                "soaked and well drained",
                                "18 Quail eggs"                         
                               
                              ]
            },
            {  
              "Image":"images/2.jpg",
              "Name":"Noodles",
              "Description":"Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
              "Flavors":{"type":"Veg","flavor":"Sweet"},
              "Yield":"1/2 kg noodles",
              "Ingredients":[ "8 Shrimp",
                                "2 peices of Bread",
                                "1 Egg",
                                "Salt",
                                "4 Tablespoons Soy sauce ",
                                "2 tablespoons Peanut butter ",
                                "1 tablespoon Honey ",
                                "2 teaspoons White vinegar",
                                "1/8 teaspoon Garlipowder",                              
                              ]
            }
        ]
    };
   
   
    var template = Handlebars.compile($('#sampletemplate').html());
    var returnvalue = template(context);
    $('#container').html(returnvalue);

})();
</script>


Above you can see a context is created and binded to the compiled code using handlebars.


Output:








Html:


<!DOCTYPE html>
<html>
<head>
    <script src="jquery-1.11.1.min.js" type="text/javascript"></script>
    <script src="handlebars-v1.3.0.js" type="text/javascript"></script>
   
    <style type="text/css">
      .small{
      font-size:15px;
      color:white;   
      }
      .Thumb
      {
       height:150px;
       width:250px
      }
    </style>
   
</head>
<body>

<div style="background-color:Orange;height:1000px;width:630px" id="container">

</div>
<br />
<input type="button" value="Generate"></input>

<script id="sampletemplate" type="text/x-handlebars-template"> 
    <div id="content">
    <center>
    <h1  style="margin-left:10px;color:white">&Xi;RecipeMessages
    <span class="small">Messages search powered by <a id="Logo">   
    </center>
    </h1>
    <div style="margin-left:137px;height:150px:width:100px">
    <img style="color:orange;width:310px" src="images/SplashScreen.png"></img>
    </div>
    {{#each recipes}}
    <div class="" style="float:left;background-color:whitesmoke;padding:10px;margin-left:15px;margin-right:15px">
        <img class="Thumb" src="{{Image}}" alt="{{Name}}"/>
        <h3>{{Name}} - <span style="color:orange;font-size:15px">{{#if Source}} {{Source.Name}} {{else}} No  {{/if}}</span></h3>
        <p style="width:230px">{{Description}}</p>
        <h5 style="color:blue">{{getflavor Flavors}}</h5>
        <h5>{{Yield}}</h5>
        <p>Incredients :</p>
        <ul>
        {{#each Ingredients}}
            <li>{{this}}</li>
        {{/each}}
        </ul>
    </div>
    {{/each}}
    </div>
</script>

<script>
(function(){

    Handlebars.registerHelper('getflavor',function(flavorcontext){     
        return "This dish has a "+flavorcontext.type+" with "+flavorcontext.flavor+" Flavor.";
    });

    var context ={
        recipes : [
            {  
              "Image":"images/1.jpg",
              "Name":"Briyaani",
              "Description":"Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
              "Source":{"Name":"All Recipes"},
              "Flavors":{"type":"Non-Veg","flavor":"Garam"},
              "Yield":"10 pieces of chicken",
              "Ingredients":[ "600 g Ground pork",
                                "1 tsp Salt",
                                "1 tsp sugar",
                                "1/2 tsp Ground black pepper",
                                "2 tsp Cornstarch",
                                "1 tbsp Sesame oil",
                                "1 handful Sliced dried mushrooms ",
                                "soaked and well drained",
                                "18 Quail eggs"                        
                               
                              ]
            },
            {  
              "Image":"images/2.jpg",
              "Name":"Noodles",
              "Description":"Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
              "Flavors":{"type":"Veg","flavor":"Sweet"},
              "Yield":"1/2 kg noodles",
              "Ingredients":[ "8 Shrimp",
                                "2 peices of Bread",
                                "1 Egg",
                                "Salt",
                                "4 Tablespoons Soy sauce ",
                                "2 tablespoons Peanut butter ",
                                "1 tablespoon Honey ",
                                "2 teaspoons White vinegar",
                                "1/8 teaspoon Garlic powder",                              
                              ]
            }
        ]
    };
   
   
    var template = Handlebars.compile($('#sampletemplate').html());
    var returnvalue = template(context);
    $('#container').html(returnvalue);

})();
</script>

</body>
</html>



From this article you can see how to create a sample webpage using handlebar js.