lundi 29 juin 2015

Converting images to base64 within a loop before adding to jspdf - javascript


I have researched issues with the base64 conversion and jspdf function quite a bit. ( PS this is my first question on stackoverflow, please bare with any rookie mistakes ).

All seems to work fine with the below code except that the pdf is generated and saved before the loop where the images are converted to base64 and placed to the document is finished. I added a couple alerts to check timing. Would the solution be to check when the loop is finished, the images placed before continuing with the pdf function? if so, how? please help.

$(document).ready(function(){
    $("a#getdoc").click(function(){
    var doc = new jsPDF('landscape, in, legal');
    var myimages = 'img1.jpg|img2.jpg|img3.png';
    var myimgarray = myimages.split('|');

    function convertImgToBase64(url, callback, outputFormat){
        var canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0);
        return canvas.toDataURL("image/jpeg");
        var dataURL = canvas.toDataURL("image/jpeg");
        callback(dataURL);
            canvas = null; 
    }

for(var i = 0; i < myimgarray.length; i++)
{
  icount.count = i;
  var img = new Image();
  alert(checkoutimgarray);

  img.src = '/Portals/0/repair-images/' + myimgarray[i];
  img.onload = function(){
      newData = convertImgToBase64(img);
      alert(newData);
      doc.addImage(newData, 'JPEG', (icount * 100), 10, 70, 15); // would doc be undefined here? out of scope?
  };
}

doc.setFontSize(20);
doc.text(100, 20, "This is a test to see if images will show");
doc.save('My_file.pdf');  

  });
});


jquery tabs function on click


Only problem that i have is when page load, all 4(four) tabs have .current class on it. Want i want is that 1st tab (data-tab="tab-1) have class .current on page load and all other to be hidden. Here is my full project section

$('ul.tabs li').click(function(){
        var tab_id = $(this).attr('data-tab');

        $('ul.tabs li').removeClass('current');
        $('.tab-content').removeClass('current');

        $(this).addClass('current');
        $("#"+tab_id).addClass('current');
    });
.tabs li.current {
  color: red;
}
.tab-content {
  display: none;
 }
.tab-content.current {
  display: block;
}
<script src="http://ift.tt/1oMJErh"></script>
<section class="innovation">
  <div class="categories-wrap">
    <ul class="tabs">
      <li class="tab-link" data-tab="tab-1">
        <i class="sprite-call-black"></i>
        <h4>Pristine coverage</h4>
        <p>Enjoy your calls without interuptions like dropped calls, poor sound quality, and delayed video.</p>
      </li>
      <li class="tab-link" data-tab="tab-2">
        <i class="sprite-call-black"></i>
        <h4>Chat messaging</h4>
        <p>Chat in real time with connections all around the world. </p>
      </li>
      <li class="tab-link" data-tab="tab-3">
        <i class="sprite-call-black"></i>
        <h4>Video calling</h4>
        <p>WiFi paired with reliable cellular service is how we’ve got you covered in more places than ever before.</p>
      </li>
      <li class="tab-link" data-tab="tab-4">
        <i class="sprite-call-black"></i>
        <h4>Photo share</h4>
        <p>WiFi paired with reliable cellular service is how we’ve got you covered in more places than ever before.</p>
      </li>
    </ul>
  </div>
  <div class="tabs-wrap">
    <div id="tab-1" class="tab-content current">
      <img src="" alt="Pristine Coverage">
    </div>
    <div id="tab-2" class="tab-content current">
      <img src="" alt="Chat Messaging">
    </div>
    <div id="tab-3" class="tab-content current">
      <img src="" alt="Video Calling">
    </div>
    <div id="tab-4" class="tab-content current">
      <img src="" alt="Photo Share">
    </div>
  </div>
</section>

Cordova Hybrid App Design - Controllers


I am trying to develop an Application using Cordova framework. My application needs to have many controllers to work. So keeping all the Controllers in the same file will be Bulky as the Code size will increase. So maintainability of the code will be lost.

So for each Controller which Programming Design approach is suggested:

1. Having one main App.js file and writing all the Controllers inside it. And finally importing only App.js file in index.html file.

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

myApp.controller('ControllerA', ['$scope', function($scope) {

}]);

myApp.controller('ControllerB', ['$scope', function($scope) {

}]);

myApp.controller('ControllerC', ['$scope', function($scope) {

}]);

2. Having multiple js files for each controller and importing all in the main index.html file:

Main App.js file:

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

ControllerA.js file:

myApp.controller('ControllerA', ['$scope', function($scope) {

}]);

ControllerB.js file:

myApp.controller('ControllerB', ['$scope', function($scope) {

}]);

ControllerC.js file:

myApp.controller('ControllerC', ['$scope', function($scope) {

}]);


Reduce in Javascript


So I want to convert:

FROM:
{
    emailNotify: {
        EQ: true
    },
    foo: {
        bar: false
    }
}

TO:

Result : [{'condition': 'EQ', 'attribute': emailNotify, 'value': true}, {'condition': 'bar', 'attribute': foo, 'value': false}]

I tried the following code:

var fromObj={
   emailNotify: {
        EQ: true
    },
    foo: {
        bar: false
    }
};


console.log(Object.keys(fromObj));
var result = (Object.keys(fromObj)).reduce(function(p,c,i,a){
    var newObj={};
    newObj["condition"]=Object.keys(fromObj[c])[0];
    newObj["attribute"]=c;
    newObj["value"]=fromObj[c][Object.keys(fromObj[c])[0]];
    p.push(newObj);
    return p;
},[]);


console.log("result", result);

Is this the way you to would do it as well? I believe I'm not using reduce correctly?!

PS: I get the right result! Just wanted to know if it is the elengant way or not?!


Breaking a String Expression and Check for Duplicate


It's JavaScript.

I have a string which is a result of some process:

(A:x OR A:y) AND (B:m)

I want to check for duplication and mark it as duplicate even when the process results in

(A:y OR A:x) AND (B:m)

The process just changed the order of values for A.

P.S: Unfortunately this result is always a string and not some JSON type.

Question: What can be the most efficient solution to check for duplication for the pattern of string mentioned about. split(' ') might work but I am interested in the most efficient possible solution in JS.

  • same letters (e.g. A will have 'OR's b/w there values like 'x' & 'y'above)
  • different letters (e.g. A & B will have 'AND' between them)

Thanks a bunch for looking it.


Object with one key as default (getter/setter)


I know with all the new ECMAScript6 stuff we're having some amazing things happening. My goal was to do something like this I don't know if its possible:

var angEl = {
  get(): blah,
  scope: this.scope(),
  injector: this.injector()
}

Right now I do it like this:

var angEl = blah;
var angElProps = {
  scope: angEl.scope(),
  injector: angEl.injector()
}

Just wondering if there is any cool new hip way to accheive this.


Angularjs Multidimensional array and two relative select boxes


I have a multidimensional array that holds the product names and versions. I want to create an interface that lets the user select the product from a select box, and then the version number in the second select box. The second select box should only show the versions numbers of the product that the user selected in the first select box.

This is my mutidimensional array:

[Object]0: 
         name: "Product 1"
         versions: [Array]0:
                           number: "1.0"
                           number: "1.5.2"
        1: 
         name: "Product 2"
         versions: [Array]0:
                           number: "0.0"
                           number: "0.5"

The user has the option to choose multiple products, so I created an array to hold the users selection.

my controller is setup like this:

app.controller('mainController', function ($scope) {

    $scope.products = [{id: 1, name: '', versions: []}];

  $scope.packages = [];
  $scope.packages[0] = { id: 1, name: 'Product 1', versions: [{number: 1.0}, {number: 1.5}, {number: 2.0}]};
  $scope.packages[1] = { id: 2, name: 'Product 2', versions: [{number: 0.1}, {number: 0.2}, {number: 0.3}]};

    $scope.addProduct = function(){
    var id = $scope.products.length + 1;
    $scope.products.push({id: id, name: "", version: []});

  };
});

And the select boxes are setup like this with angularjs:

    <div  ng-repeat="product in products">
       <label>Product</label>
       <select ng-model="product.product" ng-options="package.name for package in packages" class="form-control"></select>

       <label>Version</label>
       <select ng-model="product.versions" ng-options="version.number for version in product.versions" class="form-control"></select>
</div>
<button ng-click="addProduct()">Add Product</button>

What I tried to do was setup the ng-options to select the versions object of the current product. But this doesn't work.

I created a jsFiddle of what I currently have: http://ift.tt/1U1n0L7

I would really appreciate any suggestions on how to link the version select box with the product selected.

TIA