Advertisement
Guest User

Untitled

a guest
Sep 15th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. define([
  2.     'app',
  3.     'angular',
  4.     'controllers/MercadoLibre/Items/CategoriesController',
  5.     'directives/chosen',
  6.     'directives/maskmoney',
  7.     'directives/no-validate',
  8.     'factories/alert',
  9.     'factories/mercado-libre/item-tax',
  10.     'factories/loader',
  11.     'factories/validator',
  12.     'plugins/ckpath',
  13.     'directives/ckeditor',
  14.     'plugins/ng-ckeditor-config'
  15. ],
  16.     /**
  17.      * @param app
  18.      * @param angular
  19.      */
  20.     function(app, angular)
  21.     {
  22.         var controller = function($scope, $http, $resource, $itemTax, $validator, $state, $loader, $alert, $translate)
  23.         {
  24.             $scope.editorOptions = {
  25.                 language: 'pt-br'
  26.             };
  27.  
  28.             $scope.choicedColor = null;
  29.  
  30.             $scope.mlAttributes = false;
  31.  
  32.             $scope.simple = [];
  33.             var getSimple = function(id)
  34.             {
  35.                 $resource('/catalog/groups/:id/simple').get({id: id}, function(json)
  36.                 {
  37.                     $scope.simple = json.rows;
  38.                 });
  39.             };
  40.  
  41.             $scope.item = {
  42.                 _attributes: [],
  43.                 _attributesHead: []
  44.             };
  45.  
  46.             $scope.item.condition = "new";
  47.  
  48.             $scope.item.excludeNo = null;
  49.             $scope.item.excludeNe = null;
  50.  
  51.             $scope.item.excludesArea = [];
  52.  
  53.             $scope.config = {};
  54.  
  55.             $scope.templates = [];
  56.             $resource('/mercado-libre/items/template').query(function(json)
  57.             {
  58.                 $scope.templates = json;
  59.             });
  60.  
  61.             $resource('/mercado-libre').get(function(json)
  62.             {
  63.                 $scope.config = json;
  64.             });
  65.  
  66.             $scope.getSuggestions = function(value)
  67.             {
  68.                 return $http.get('/product/suggestions', {
  69.                     params: {type: $scope.item.tipo, name: value}
  70.                 }).then(function(result)
  71.                 {
  72.                     return result.data;
  73.                 });
  74.             };
  75.  
  76.             $scope.suggestionSelect = function(suggestion)
  77.             {
  78.                 $scope.item.title = suggestion.nome.substr(0, 60);
  79.                 $scope.item.price = (
  80.                     suggestion.valorVenda && suggestion.valorVenda > 0
  81.                 ) ? suggestion.valorVenda : suggestion.valorOriginal;
  82.                 $scope.item.height = parseInt(suggestion.altura);
  83.                 $scope.item.width = parseInt(suggestion.largura);
  84.                 $scope.item.length = parseInt(suggestion.espessura);
  85.                 $scope.item.weight = parseInt(suggestion.peso);
  86.                 $scope.item.availableQuantity = parseInt(suggestion.quantidade);
  87.                 $scope.item.idProd = suggestion.id;
  88.                 $scope.item._link = suggestion;
  89.  
  90.                 if ($scope.item.tipo == 'grupo') {
  91.                     getSimple($scope.item.idProd);
  92.                 } else {
  93.                     $scope.simple = [];
  94.                 }
  95.             };
  96.  
  97.             $scope.changeTemplate = function(id)
  98.             {
  99.                 if (!$scope.item || !$scope.item._link) {
  100.                     $alert.danger(null, "ML.ITEM.ERROR.TEMPLATE_NO_LINK");
  101.                 } else if (id > 0) {
  102.                     $resource('/mercado-libre/items/template/:id').get({
  103.                         id: id,
  104.                         productId: $scope.item.idProd,
  105.                         productType: $scope.item.tipo
  106.                     }, function(json)
  107.                     {
  108.                         $scope.item.description = json.template;
  109.                     });
  110.                 } else {
  111.                     delete $scope.item.description;
  112.                 }
  113.             };
  114.  
  115.             var maxPicturesPerItem = 6;
  116.             $scope.$watch('item.categoryId', function(category)
  117.             {
  118.                 if (typeof category != "undefined") {
  119.                     $resource('/mercado-libre/items/categories').get({
  120.                         id: category
  121.                     }, function(json)
  122.                     {
  123.                         if ($scope.item.pictures) {
  124.                             var maxPicturesPerItem = json.settings.max_pictures_per_item;
  125.  
  126.                             var spliced = ($scope.item.pictures.length - 1) - maxPicturesPerItem;
  127.                             if ($scope.item.pictures.length >= json.settings.max_pictures_per_item) {
  128.                                 $scope.item.pictures.splice(0, spliced);
  129.                             }
  130.                         }
  131.                     });
  132.                 }
  133.             });
  134.  
  135.             $scope.toggleImage = function(image)
  136.             {
  137.                 if (!$scope.item.pictures) {
  138.                     $scope.item.pictures = [];
  139.                 }
  140.  
  141.                 var index = $scope.item.pictures.indexOf(image.url);
  142.                 if (index > -1) {
  143.                     $scope.item.pictures.splice(index, 1);
  144.                 } else {
  145.                     if ($scope.item.pictures.length >= maxPicturesPerItem) {
  146.                         $scope.item.pictures.splice(0, 1);
  147.                     }
  148.  
  149.                     $scope.item.pictures.push(image.url);
  150.                 }
  151.  
  152.                 $scope.item._pictures = $scope.item.pictures.join(',');
  153.             };
  154.  
  155.             $scope.chooseColor = function(index, attrId, valId, attrName, attrRgb)
  156.             {
  157.                 var chooseColorButtonId = "#chooseColor_" + index;
  158.                 var colorButton = angular.element( document.querySelector( chooseColorButtonId ) );
  159.  
  160.                 var colorHtml = '<div ' +
  161.                                     'style="height:20px; width:20px; ' +
  162.                                     'float:left; background-color: ' + attrRgb + '; ' +
  163.                                     'margin-right: 5px;" class="square-color"></div>' + attrName;
  164.  
  165.                 $scope.attribute[attrId] = valId;
  166.                 colorButton.html(colorHtml);
  167.  
  168.                 var chooseColorSelectId = "#toggleSelectColor_" + index;
  169.                 var isOpen = angular.element( document.querySelector( chooseColorSelectId ) );
  170.                 isOpen.removeClass('open');
  171.             };
  172.  
  173.             $scope.chooseAttr = function(index, attrId, valId, attrName)
  174.             {
  175.                 var chooseAttrButtonId = "#chooseAttr_" + index;
  176.                 var attrButton = angular.element( document.querySelector( chooseAttrButtonId ) );
  177.                 var attrHtml = '<a href="" class="ng-binding">' + attrName + '</a>';
  178.                 attrButton.html(attrHtml);
  179.                 $scope.attribute[attrId] = valId;
  180.  
  181.                 var chooseAttrSelectId = "#toggleSelectAttr_" + index;
  182.                 var isOpen = angular.element( document.querySelector( chooseAttrSelectId ) );
  183.                 isOpen.removeClass('open');
  184.             }
  185.  
  186.             $scope.itemTax = 0;
  187.             $scope.itemSaleTax = 0;
  188.             $scope.$watchCollection('[item.listingTypeId, item.price, item.availableQuantity]', function(newValue)
  189.             {
  190.                 var listingTypeId = newValue[0];
  191.                 var price = isNaN(newValue[1]) ? 0 : newValue[1];
  192.                 var quantity = isNaN(newValue[2]) ? 0 : newValue[2];
  193.  
  194.                 var tax = $itemTax(price, quantity, listingTypeId);
  195.                 $scope.itemTax = tax.item;
  196.                 $scope.itemSaleTax = tax.sale;
  197.             });
  198.  
  199.             $scope.create = function()
  200.             {
  201.                 var element = angular.element(document.getElementsByName('formMlItemNew'));
  202.                 var scope = angular.element(element).scope();
  203.  
  204.                 if ($scope.item.length && $scope.item.width && $scope.item.height && $scope.item.weight) {
  205.                     $scope.item.shippingDimensions = [
  206.                         $scope.item.length,
  207.                         $scope.item.width,
  208.                         $scope.item.height
  209.                     ].join('x') + ',' + $scope.item.weight;
  210.                 } else {
  211.                     delete $scope.item.shippingDimensions;
  212.                 }
  213.  
  214.                 if ($scope.item.excludeNorthRegion) {
  215.                     $scope.item.excludesArea.push("BR-NO", "BR-NE");
  216.                 }
  217.  
  218.                 $loader.show();
  219.                 $resource('/mercado-libre/items').create($scope.item, function(json)
  220.                 {
  221.                     if (json.$success) {
  222.                         $alert.success("COMMON.SUCCESS.", "ML.ITEM.SUCCESS.PROCESSING_NEW");
  223.                         $state.go('mercado-libre-items', {}, {reload: true, inherit: false, notify: true});
  224.                     } else {
  225.                         $scope.errors = $validator.parseErrors(scope, 'formMlItemNew', json.$errors);
  226.  
  227.                         angular.element('html, body').animate({
  228.                             scrollTop: angular.element('.main-content').offset().top
  229.                         }, 400);
  230.                         $loader.hide();
  231.                     }
  232.                 });
  233.             };
  234.  
  235.             $scope.populateDimensions = function()
  236.             {
  237.                 if (!$scope.item || !$scope.item._link) {
  238.                     $alert.danger(null, "ML.ITEM.ERROR.DIMENSIONS_NO_LINK");
  239.                 } else {
  240.                     $scope.item.height = parseInt($scope.item._link.altura);
  241.                     $scope.item.width = parseInt($scope.item._link.largura);
  242.                     $scope.item.length = parseInt($scope.item._link.espessura);
  243.                     $scope.item.weight = parseInt($scope.item._link.peso);
  244.                 }
  245.             };
  246.  
  247.             $scope.$watchCollection('[item.categoryId, item.tipo, item.idProd]', function(newValues)
  248.             {
  249.                 var categoryId = newValues[0];
  250.                 var type = newValues[1];
  251.                 var link = newValues[2];
  252.  
  253.                 $scope.mlAttributes = false;
  254.                 if (categoryId && type && link) {
  255.                     var element = angular.element('div[data-ng-controller="MercadoLibre/Items/CategoriesController"]');
  256.                     var scope = element.scope();
  257.  
  258.                     var categorie = scope.getOne(categoryId);
  259.  
  260.                     if (
  261.                         categorie &&
  262.                         ['variations', 'attributes'].indexOf(categorie.attribute_types) > -1 &&
  263.                         type == 'grupo'
  264.                     ) {
  265.                         $scope.mlAttributes = categorie._attributes;
  266.                     }
  267.                 }
  268.             });
  269.  
  270.             $scope.attrProductId = '';
  271.             $scope.attribute = {};
  272.             $scope.newAttribute = function(attrProductId, attribute)
  273.             {
  274.                 var element = angular.element('div[data-ng-controller="MercadoLibre/Items/CategoriesController"]');
  275.                 var scope = element.scope();
  276.                 var categorie = scope.getOne($scope.item.categoryId);
  277.  
  278.                 var required = [];
  279.                 var attributes = [];
  280.                 angular.forEach(categorie._attributes, function(attr)
  281.                 {
  282.                     if (attr && attr.tags && attr.tags.required) {
  283.                         if (!attribute.hasOwnProperty(attr.id) ||
  284.                             !attribute[attr.id] ||
  285.                             attribute[attr.id] === 0 ||
  286.                             attribute[attr.id].length == 0
  287.                         ) {
  288.                             required.push(attr.name);
  289.                         } else {
  290.                             attributes.push(attr.name);
  291.                         }
  292.                     }
  293.                 });
  294.  
  295.                 var errors = [];
  296.                 if (!attrProductId || attrProductId == 0) {
  297.                     errors.push('ML.ITEM.ERROR.ATTR_SELECT_PRODUCT');
  298.                 }
  299.  
  300.                 if (required.length > 0) {
  301.                     errors.push(
  302.                         $translate.instant('ML.ITEM.ERROR.ATTR_SELECT_ATTRIBUTE', {
  303.                             attributes: required.join(', ')
  304.                         })
  305.                     );
  306.                 }
  307.  
  308.  
  309.                 if (errors.length > 0) {
  310.                     $alert.danger(null, errors);
  311.                 } else {
  312.                     var simple = null;
  313.                     angular.forEach($scope.simple, function(product)
  314.                     {
  315.                         if (product.id == attrProductId) {
  316.                             simple = product;
  317.                         }
  318.                     });
  319.  
  320.                     angular.forEach($scope.mlAttributes, function(attribute, index)
  321.                     {
  322.                         var chooseAttrButtonClass = ".default-dropdown_" + index;
  323.                         var attrButton = angular.element( document.querySelector( chooseAttrButtonClass ) );
  324.                         var attrHtml = attribute.name + '<span class="caret arrow-dropdown"></span>';
  325.  
  326.                         attrButton.html(attrHtml);
  327.                     });
  328.  
  329.                     var price = null;
  330.                     if (simple) {
  331.                         price = simple.valorVenda;
  332.                         if (!price || price == 0) {
  333.                             price = simple.valorOriginal;
  334.                         }
  335.                     }
  336.  
  337.                     $scope.item._attributes.push({
  338.                         productId: attrProductId,
  339.                         attribute: attribute,
  340.                         _product: simple,
  341.                         quantity: (simple) ? simple.quantidade : null,
  342.                         price: price
  343.                     });
  344.  
  345.                     var formElement = angular.element(document.getElementsByName('formMlItemNew'));
  346.                     var formScope = angular.element(formElement).scope();
  347.                     if (formScope.formMlItemNew) {
  348.                         formScope.formMlItemNew.attrProductId.$setViewValue('');
  349.                         $scope.attribute = {};
  350.                     }
  351.                 }
  352.             };
  353.  
  354.             $scope.filterSimple = function()
  355.             {
  356.                 return function(smp)
  357.                 {
  358.                     var has = false;
  359.                     if ($scope.item && $scope.item._attributes) {
  360.                         angular.forEach($scope.item._attributes, function(attr)
  361.                         {
  362.                             if (attr.productId == smp.id) {
  363.                                 has = true;
  364.                             }
  365.                         });
  366.                     }
  367.  
  368.                     return !has;
  369.                 };
  370.             };
  371.  
  372.             $scope.removeAttribute = function(index)
  373.             {
  374.                 if ($scope.item && $scope.item._attributes) {
  375.                     $scope.item._attributes.splice(index, 1);
  376.                 }
  377.             };
  378.  
  379.             $scope.attributeChangeQuantity = function()
  380.             {
  381.                 if ($scope.item && $scope.item._attributes && $scope.item._attributes.length > 0) {
  382.                     var availableQuantity = 0;
  383.                     $scope.item._attributes.forEach(function(attr)
  384.                     {
  385.                         availableQuantity += parseInt(attr.quantity);
  386.                     });
  387.  
  388.                     $scope.item.availableQuantity = availableQuantity;
  389.                 } else if ($scope.item._link) {
  390.                     $scope.item.availableQuantity = $scope.item._link.quantidade;
  391.                 }
  392.             };
  393.  
  394.             $scope.$watchCollection('item._attributes', function()
  395.             {
  396.                 $scope.attributeChangeQuantity();
  397.             });
  398.  
  399.             $scope.toggleImageAttribute = function(attribute, image)
  400.             {
  401.                 if (!attribute.pictures) {
  402.                     attribute.pictures = [];
  403.                 }
  404.  
  405.                 var index = attribute.pictures.indexOf(image.url);
  406.                 if (index > -1) {
  407.                     attribute.pictures.splice(index, 1);
  408.                 } else {
  409.                     if (attribute.pictures.length >= 6) {
  410.                         attribute.pictures.splice(0, 1);
  411.                     }
  412.  
  413.                     attribute.pictures.push(image.url);
  414.                 }
  415.  
  416.                 var pictures = [];
  417.                 if ($scope.item && $scope.item._attributes && $scope.item._attributes.length > 0) {
  418.                     $scope.item._attributes.forEach(function(attr)
  419.                     {
  420.                         if (attr.pictures) {
  421.                             attr.pictures.forEach(function(picture)
  422.                             {
  423.                                 pictures.push(picture);
  424.                             });
  425.                         }
  426.                     });
  427.                 }
  428.                 $scope.item._attributePictures = pictures.join(',');
  429.             };
  430.         };
  431.  
  432.         app.register.controller('MercadoLibre/Items/NewController', [
  433.             '$scope',
  434.             '$http',
  435.             '$resource',
  436.             '$mercadoLibreItemTax',
  437.             '$validator',
  438.             '$state',
  439.             '$loader',
  440.             '$alert',
  441.             '$translate',
  442.             controller
  443.         ]);
  444.     }
  445. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement