Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define([
- 'app',
- 'angular',
- 'controllers/MercadoLibre/Items/CategoriesController',
- 'directives/chosen',
- 'directives/maskmoney',
- 'directives/no-validate',
- 'factories/alert',
- 'factories/mercado-libre/item-tax',
- 'factories/loader',
- 'factories/validator',
- 'plugins/ckpath',
- 'directives/ckeditor',
- 'plugins/ng-ckeditor-config'
- ],
- /**
- * @param app
- * @param angular
- */
- function(app, angular)
- {
- var controller = function($scope, $http, $resource, $itemTax, $validator, $state, $loader, $alert, $translate)
- {
- $scope.editorOptions = {
- language: 'pt-br'
- };
- $scope.choicedColor = null;
- $scope.mlAttributes = false;
- $scope.simple = [];
- var getSimple = function(id)
- {
- $resource('/catalog/groups/:id/simple').get({id: id}, function(json)
- {
- $scope.simple = json.rows;
- });
- };
- $scope.item = {
- _attributes: [],
- _attributesHead: []
- };
- $scope.item.condition = "new";
- $scope.item.excludeNo = null;
- $scope.item.excludeNe = null;
- $scope.item.excludesArea = [];
- $scope.config = {};
- $scope.templates = [];
- $resource('/mercado-libre/items/template').query(function(json)
- {
- $scope.templates = json;
- });
- $resource('/mercado-libre').get(function(json)
- {
- $scope.config = json;
- });
- $scope.getSuggestions = function(value)
- {
- return $http.get('/product/suggestions', {
- params: {type: $scope.item.tipo, name: value}
- }).then(function(result)
- {
- return result.data;
- });
- };
- $scope.suggestionSelect = function(suggestion)
- {
- $scope.item.title = suggestion.nome.substr(0, 60);
- $scope.item.price = (
- suggestion.valorVenda && suggestion.valorVenda > 0
- ) ? suggestion.valorVenda : suggestion.valorOriginal;
- $scope.item.height = parseInt(suggestion.altura);
- $scope.item.width = parseInt(suggestion.largura);
- $scope.item.length = parseInt(suggestion.espessura);
- $scope.item.weight = parseInt(suggestion.peso);
- $scope.item.availableQuantity = parseInt(suggestion.quantidade);
- $scope.item.idProd = suggestion.id;
- $scope.item._link = suggestion;
- if ($scope.item.tipo == 'grupo') {
- getSimple($scope.item.idProd);
- } else {
- $scope.simple = [];
- }
- };
- $scope.changeTemplate = function(id)
- {
- if (!$scope.item || !$scope.item._link) {
- $alert.danger(null, "ML.ITEM.ERROR.TEMPLATE_NO_LINK");
- } else if (id > 0) {
- $resource('/mercado-libre/items/template/:id').get({
- id: id,
- productId: $scope.item.idProd,
- productType: $scope.item.tipo
- }, function(json)
- {
- $scope.item.description = json.template;
- });
- } else {
- delete $scope.item.description;
- }
- };
- var maxPicturesPerItem = 6;
- $scope.$watch('item.categoryId', function(category)
- {
- if (typeof category != "undefined") {
- $resource('/mercado-libre/items/categories').get({
- id: category
- }, function(json)
- {
- if ($scope.item.pictures) {
- var maxPicturesPerItem = json.settings.max_pictures_per_item;
- var spliced = ($scope.item.pictures.length - 1) - maxPicturesPerItem;
- if ($scope.item.pictures.length >= json.settings.max_pictures_per_item) {
- $scope.item.pictures.splice(0, spliced);
- }
- }
- });
- }
- });
- $scope.toggleImage = function(image)
- {
- if (!$scope.item.pictures) {
- $scope.item.pictures = [];
- }
- var index = $scope.item.pictures.indexOf(image.url);
- if (index > -1) {
- $scope.item.pictures.splice(index, 1);
- } else {
- if ($scope.item.pictures.length >= maxPicturesPerItem) {
- $scope.item.pictures.splice(0, 1);
- }
- $scope.item.pictures.push(image.url);
- }
- $scope.item._pictures = $scope.item.pictures.join(',');
- };
- $scope.chooseColor = function(index, attrId, valId, attrName, attrRgb)
- {
- var chooseColorButtonId = "#chooseColor_" + index;
- var colorButton = angular.element( document.querySelector( chooseColorButtonId ) );
- var colorHtml = '<div ' +
- 'style="height:20px; width:20px; ' +
- 'float:left; background-color: ' + attrRgb + '; ' +
- 'margin-right: 5px;" class="square-color"></div>' + attrName;
- $scope.attribute[attrId] = valId;
- colorButton.html(colorHtml);
- var chooseColorSelectId = "#toggleSelectColor_" + index;
- var isOpen = angular.element( document.querySelector( chooseColorSelectId ) );
- isOpen.removeClass('open');
- };
- $scope.chooseAttr = function(index, attrId, valId, attrName)
- {
- var chooseAttrButtonId = "#chooseAttr_" + index;
- var attrButton = angular.element( document.querySelector( chooseAttrButtonId ) );
- var attrHtml = '<a href="" class="ng-binding">' + attrName + '</a>';
- attrButton.html(attrHtml);
- $scope.attribute[attrId] = valId;
- var chooseAttrSelectId = "#toggleSelectAttr_" + index;
- var isOpen = angular.element( document.querySelector( chooseAttrSelectId ) );
- isOpen.removeClass('open');
- }
- $scope.itemTax = 0;
- $scope.itemSaleTax = 0;
- $scope.$watchCollection('[item.listingTypeId, item.price, item.availableQuantity]', function(newValue)
- {
- var listingTypeId = newValue[0];
- var price = isNaN(newValue[1]) ? 0 : newValue[1];
- var quantity = isNaN(newValue[2]) ? 0 : newValue[2];
- var tax = $itemTax(price, quantity, listingTypeId);
- $scope.itemTax = tax.item;
- $scope.itemSaleTax = tax.sale;
- });
- $scope.create = function()
- {
- var element = angular.element(document.getElementsByName('formMlItemNew'));
- var scope = angular.element(element).scope();
- if ($scope.item.length && $scope.item.width && $scope.item.height && $scope.item.weight) {
- $scope.item.shippingDimensions = [
- $scope.item.length,
- $scope.item.width,
- $scope.item.height
- ].join('x') + ',' + $scope.item.weight;
- } else {
- delete $scope.item.shippingDimensions;
- }
- if ($scope.item.excludeNorthRegion) {
- $scope.item.excludesArea.push("BR-NO", "BR-NE");
- }
- $loader.show();
- $resource('/mercado-libre/items').create($scope.item, function(json)
- {
- if (json.$success) {
- $alert.success("COMMON.SUCCESS.", "ML.ITEM.SUCCESS.PROCESSING_NEW");
- $state.go('mercado-libre-items', {}, {reload: true, inherit: false, notify: true});
- } else {
- $scope.errors = $validator.parseErrors(scope, 'formMlItemNew', json.$errors);
- angular.element('html, body').animate({
- scrollTop: angular.element('.main-content').offset().top
- }, 400);
- $loader.hide();
- }
- });
- };
- $scope.populateDimensions = function()
- {
- if (!$scope.item || !$scope.item._link) {
- $alert.danger(null, "ML.ITEM.ERROR.DIMENSIONS_NO_LINK");
- } else {
- $scope.item.height = parseInt($scope.item._link.altura);
- $scope.item.width = parseInt($scope.item._link.largura);
- $scope.item.length = parseInt($scope.item._link.espessura);
- $scope.item.weight = parseInt($scope.item._link.peso);
- }
- };
- $scope.$watchCollection('[item.categoryId, item.tipo, item.idProd]', function(newValues)
- {
- var categoryId = newValues[0];
- var type = newValues[1];
- var link = newValues[2];
- $scope.mlAttributes = false;
- if (categoryId && type && link) {
- var element = angular.element('div[data-ng-controller="MercadoLibre/Items/CategoriesController"]');
- var scope = element.scope();
- var categorie = scope.getOne(categoryId);
- if (
- categorie &&
- ['variations', 'attributes'].indexOf(categorie.attribute_types) > -1 &&
- type == 'grupo'
- ) {
- $scope.mlAttributes = categorie._attributes;
- }
- }
- });
- $scope.attrProductId = '';
- $scope.attribute = {};
- $scope.newAttribute = function(attrProductId, attribute)
- {
- var element = angular.element('div[data-ng-controller="MercadoLibre/Items/CategoriesController"]');
- var scope = element.scope();
- var categorie = scope.getOne($scope.item.categoryId);
- var required = [];
- var attributes = [];
- angular.forEach(categorie._attributes, function(attr)
- {
- if (attr && attr.tags && attr.tags.required) {
- if (!attribute.hasOwnProperty(attr.id) ||
- !attribute[attr.id] ||
- attribute[attr.id] === 0 ||
- attribute[attr.id].length == 0
- ) {
- required.push(attr.name);
- } else {
- attributes.push(attr.name);
- }
- }
- });
- var errors = [];
- if (!attrProductId || attrProductId == 0) {
- errors.push('ML.ITEM.ERROR.ATTR_SELECT_PRODUCT');
- }
- if (required.length > 0) {
- errors.push(
- $translate.instant('ML.ITEM.ERROR.ATTR_SELECT_ATTRIBUTE', {
- attributes: required.join(', ')
- })
- );
- }
- if (errors.length > 0) {
- $alert.danger(null, errors);
- } else {
- var simple = null;
- angular.forEach($scope.simple, function(product)
- {
- if (product.id == attrProductId) {
- simple = product;
- }
- });
- angular.forEach($scope.mlAttributes, function(attribute, index)
- {
- var chooseAttrButtonClass = ".default-dropdown_" + index;
- var attrButton = angular.element( document.querySelector( chooseAttrButtonClass ) );
- var attrHtml = attribute.name + '<span class="caret arrow-dropdown"></span>';
- attrButton.html(attrHtml);
- });
- var price = null;
- if (simple) {
- price = simple.valorVenda;
- if (!price || price == 0) {
- price = simple.valorOriginal;
- }
- }
- $scope.item._attributes.push({
- productId: attrProductId,
- attribute: attribute,
- _product: simple,
- quantity: (simple) ? simple.quantidade : null,
- price: price
- });
- var formElement = angular.element(document.getElementsByName('formMlItemNew'));
- var formScope = angular.element(formElement).scope();
- if (formScope.formMlItemNew) {
- formScope.formMlItemNew.attrProductId.$setViewValue('');
- $scope.attribute = {};
- }
- }
- };
- $scope.filterSimple = function()
- {
- return function(smp)
- {
- var has = false;
- if ($scope.item && $scope.item._attributes) {
- angular.forEach($scope.item._attributes, function(attr)
- {
- if (attr.productId == smp.id) {
- has = true;
- }
- });
- }
- return !has;
- };
- };
- $scope.removeAttribute = function(index)
- {
- if ($scope.item && $scope.item._attributes) {
- $scope.item._attributes.splice(index, 1);
- }
- };
- $scope.attributeChangeQuantity = function()
- {
- if ($scope.item && $scope.item._attributes && $scope.item._attributes.length > 0) {
- var availableQuantity = 0;
- $scope.item._attributes.forEach(function(attr)
- {
- availableQuantity += parseInt(attr.quantity);
- });
- $scope.item.availableQuantity = availableQuantity;
- } else if ($scope.item._link) {
- $scope.item.availableQuantity = $scope.item._link.quantidade;
- }
- };
- $scope.$watchCollection('item._attributes', function()
- {
- $scope.attributeChangeQuantity();
- });
- $scope.toggleImageAttribute = function(attribute, image)
- {
- if (!attribute.pictures) {
- attribute.pictures = [];
- }
- var index = attribute.pictures.indexOf(image.url);
- if (index > -1) {
- attribute.pictures.splice(index, 1);
- } else {
- if (attribute.pictures.length >= 6) {
- attribute.pictures.splice(0, 1);
- }
- attribute.pictures.push(image.url);
- }
- var pictures = [];
- if ($scope.item && $scope.item._attributes && $scope.item._attributes.length > 0) {
- $scope.item._attributes.forEach(function(attr)
- {
- if (attr.pictures) {
- attr.pictures.forEach(function(picture)
- {
- pictures.push(picture);
- });
- }
- });
- }
- $scope.item._attributePictures = pictures.join(',');
- };
- };
- app.register.controller('MercadoLibre/Items/NewController', [
- '$scope',
- '$http',
- '$resource',
- '$mercadoLibreItemTax',
- '$validator',
- '$state',
- '$loader',
- '$alert',
- '$translate',
- controller
- ]);
- }
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement