angularjs_socketio - bigktmbig/MyExperienceAlgorithm GitHub Wiki

function get_data () { http.get(api_url, function (data) { $timeout(function (){ $rootScope.$broadcast('get_data', data); }); }) }

var destroy_data = $scope.$on('get_data', function () { //do something }); $scope.$on('$destroy', destroy_data);

** EXPORT PDF ** $scope.exportData = function(){

        const PAGE_HEIGHT = 787;                                                                                                                                                            
        const PAGE_WIDTH = 500;
        // const PAGE_HEIGHT = 700;
        // const PAGE_WIDTH = 500;
        const content = [];
        const splitImage = function(img, content, callback) {
            return function () {
                const canvas = document.createElement('canvas');
                const ctx    = canvas.getContext('2d');
                const printHeight = img.height * PAGE_WIDTH / img.width;

                canvas.width = PAGE_WIDTH;

                for (let pages = 0; printHeight > pages * PAGE_HEIGHT; pages++) {
                    canvas.height = Math.min(PAGE_HEIGHT, printHeight - pages * PAGE_HEIGHT);
                    ctx.drawImage(img, 0, -pages * PAGE_HEIGHT, canvas.width, printHeight);
                    content.push({ image: canvas.toDataURL(), margin: [0, 5], width: PAGE_WIDTH });
                }

                callback();
            }
        };

        html2canvas(document.getElementById('extable')).then(function(canvas) {
            var data = canvas.toDataURL();
            var width = canvas.width;
            var height = canvas.height;
            var image = data;
            var printHeight = height * PAGE_WIDTH / width;

            console.log(PAGE_WIDTH);
            console.log(PAGE_HEIGHT);
            console.log(printHeight);
            if (printHeight > PAGE_HEIGHT) {
                var img = new Image();
                img.onload = splitImage(img, content, function () {
                    var docDefinition = {                          
                        content: content
                    };
                    pdfMake.createPdf(docDefinition).download("barcode.pdf");
                });
                img.src = image;
                return;
            }

            content.push({ image, margin: [0, 5], width: PAGE_WIDTH });
            var docDefinition = {                          
                content: content
            };
            pdfMake.createPdf(docDefinition).download("barcode.pdf");
        });
    }
  • export custom size: $scope.exportData = function(){

          const PAGE_HEIGHT = $scope.barcode_size?(1*$scope.barcode_size.height/0.264583):(22/0.264583);                                                                                                                                                            
          const PAGE_WIDTH = $scope.barcode_size?(1*$scope.barcode_type*$scope.barcode_size.width/0.264583):(35/0.264583);;
          const content = [];
          const splitImage = function(img, content, callback) {
              return function () {
                  const canvas = document.createElement('canvas');
                  const ctx    = canvas.getContext('2d');
    
                  for (let pages = 0; img.height > pages * PAGE_HEIGHT; pages++) {
                      canvas.height = Math.min(PAGE_HEIGHT, img.height - pages * PAGE_HEIGHT);
                      ctx.drawImage(img, 0, -pages * PAGE_HEIGHT, canvas.width, img.height);
                      content.push({ image: canvas.toDataURL(), margin: [0, 0], width: PAGE_WIDTH, height: PAGE_HEIGHT });
                  }
                  callback();
              }
          };
    
          html2canvas(document.getElementById('export_barcode')).then(function(canvas) {
              var data = canvas.toDataURL();
              var width = canvas.width;
              var height = canvas.height;
              var image = data;
              if (height > PAGE_HEIGHT) {
                  var img = new Image();
                  img.onload = splitImage(img, content, function () {
                      var docDefinition = {                          
                          content: content,
                          pageSize: {
                              width: PAGE_WIDTH,
                              height: PAGE_HEIGHT
                          },
                          pageMargins: [0,0,0,0]
                      };
    
                      pdfMake.createPdf(docDefinition).download("barcode.pdf");
                  });
                  img.src = image;
                  return;
              }
    
              content.push({ image, margin: [0, 5], width: PAGE_WIDTH });
              var docDefinition = {                          
                  content: content
              };
              pdfMake.createPdf(docDefinition).download("barcode.pdf");
          });
      }
    

handle api async app.get('/api/update_capital_cost_checkout', function(req, res) { Check_out.find(function(err, check_outs) { if (err) res.send(err);

        async.waterfall([
            function(callback) {

                var arr_ser_tmp = [];
                check_outs.forEach(function(checkout) {
                    arr_ser_tmp.push({checkout_id: checkout._id, services: checkout.services});
                });
                callback(null, arr_ser_tmp);
            },
            function(checkouts, callback) {

                Service_store.find(function(errtmp, service_store_tmp) {
                    if (errtmp)
                        res.send(errtmp);
                    callback(null, checkouts, service_store_tmp);
                });
            },
            function(checkouts, service_store_tmp, callback) {

                checkouts.forEach(function(checkout_tmp) {

                    async.waterfall([
                        function(callback) {

                            var arr_ser_store = [];
                            var total_mua = 0;
                            var total_chiphi = 0;
                            checkout_tmp.services.forEach(function(service_store) {
                                service_store_tmp.forEach(function(service_st) {

                                    if(service_st.service_id && service_store.service_id == service_st.service_id) {
                                        total_mua += service_st.price_mua*service_store.qty*1;
                                        total_chiphi += service_st.price_chiphi*service_store.qty*1;
                                        arr_ser_store.push({
                                            root_id: service_st.service_id,
                                            service_id: service_st._id,
                                            price_mua: service_st.price_mua,
                                            price_chiphi: service_st.price_chiphi,
                                            qty: service_store.qty*1
                                        });
                                        return;
                                    }
                                });
                            });
                            callback(null, total_mua, total_chiphi, arr_ser_store);
                        },
                        function(total_mua, total_chiphi, arr_ser_store, callback) {
                            Check_out.findOne({
                                _id : checkout_tmp.checkout_id
                            }, function(err, checkout_update) {

                                checkout_update['total_mua'] = total_mua;
                                checkout_update['total_chiphi'] = total_chiphi;
                                checkout_update['services_inventory'] = arr_ser_store;
                                checkout_update.save(function(error, checkout_save){
                                });
                            });
                            callback(null, 'done');
                        }
                        ], function (err, done) {
                            callback(null, done);
                        });
                });
            }
            ], function (err, result) {
                res.json(result);
            });
    });
});

================================================= arr with count in every object=>order by arr follow a number $scope.print_barcode = function (bt) { $scope.arr_print_bc = [];

        var services = $scope.arr_print_services;
        var length = services?services.length:0;
        var barcodeType = bt*1;
        var print_idx = 0;
        var tmp_idx = 0;

        $scope.arr_print_bc[print_idx] = [];

        for (let i = 0; i < length; i++) {
            if (services[i].checked == true) {
                var service_qty = services[i]?services[i].qty_pr*1:0;

                for (let i_f = tmp_idx; i_f <= service_qty; i_f++) {

                    if(i_f==service_qty) {
                        tmp_idx = 0;
                        break;
                    }else {
                        if($scope.arr_print_bc[print_idx].length==barcodeType) {
                            ++print_idx;
                            $scope.arr_print_bc[print_idx] = [];
                        }
                        $scope.arr_print_bc[print_idx].push({
                            name: services[i].name,
                            price: services[i].price,
                            code: services[i].code
                        });
                    }


                }
            }
        }
        $ngBootbox.customDialog({
            scope : $scope,
            backdrop: true,
            className: 'barcode-pop rp-pop',
            templateUrl: 'tpl/custom/barcode/print_barcode.html',
            title: "In Mã Vạch",
            onEscape: function() {
                $ngBootbox.hideAll();
            }
        });

        $ngBootbox.customDialog({
            scope : $scope,
            backdrop: true,
            className: 'preview-pop rp-pop',
            templateUrl: 'tpl/custom/barcode/preview_barcode.html',
            title: "In Mã Vạch",
            onEscape: function() {
                $ngBootbox.hideAll();
            }
        });
    }

============================================================2019/01/28 filter or in repeat | filter:search:['color','name'] ============================================================2019/01/28 angular.forEach(roomTo.bill, function(serviceOrg) { angular.forEach(arrMoveTmp, function(serviceMov, idx, obj) { if(serviceOrg._id == serviceMov._id){ serviceOrg.qty += serviceMov.qty * 1; obj.splice(idx, 1); } }); }); ========================================================2019/03/14 set scope ng-change="set_scope('start_search',start_search)"

$scope.set_scope = function(key,value){ $scope[key] = value ; } ===================================================2019/03/19 encodeURI and stringify $scope.exportListServices = function () { window.location.href = '/api/export-services?data=' + encodeURI(JSON.stringify($scope.dataFilter)); } ===================================================== Merge 2 arr to 1 with custom field

    $scope.mergeArrayNote = function() {
        var arrNote = [];
        $scope.arrShop.map(function (shop, i) {
            $scope.arrNotification.map(function (notification, i2) {
                arrNote.push({
                    shop: {
                        _id: shop._id, 
                        name: shop.name, 
                    },
                    notification: {
                        _id: notification._id
                    },
                    created : (new Date()).getTime(),
                    updated : (new Date()).getTime(),
                    isRead : false
                });
            });
        });
        return arrNote;
    }