Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*global $:false */
  2. /*global _:false */
  3. /*global define:false */
  4. define([
  5.     "backbone",
  6.     "form2js",
  7.     "utils",
  8.     "bootstrapFileInput",
  9.     "text!../../templates/addEditCategoryTemplate.html",
  10.     "text!../../templates/addProperty.html",
  11.     "text!../../templates/approvalTemplate.html",
  12.     "text!../../templates/editCategoryTemplateFormContent.html",
  13.     "text!../../templates/unavailableAlertTemplate.html",
  14.     "text!../../templates/optionSelectionTemplate.html",
  15.     "text!../../templates/itemSpecificInput.html",
  16.     "text!../../templates/categoryModalFooter.html",
  17.     "text!../../templates/addEditCategoryForm.html",
  18.     "text!../../templates/addPropertyButtonAndResetMaster.html",
  19.     ], function (Backbone, form2js, Utils, bootstrapFileInput, addEditCategoryTemplate,
  20.         addProperty, approvalTemplate, editCategoryTemplateFormContent, unavailableAlertTemplate,
  21.         optionSelectionTemplate, itemSpecificInput, categoryModalFooter, addEditCategoryForm,
  22.         addPropertyButtonAndResetMaster) {
  23.  
  24.         "use strict";
  25.  
  26.         var AddCategoryView = Backbone.View.extend({
  27.             el: "#main-container",
  28.             templates: _.template(addProperty),
  29.             addEditCategoryTmpl: _.template(addEditCategoryTemplate),
  30.             unavailableAlertTemplate: _.template(unavailableAlertTemplate),
  31.             needsApproval: _.template(approvalTemplate),
  32.             editCatTempForm: _.template(editCategoryTemplateFormContent),
  33.             modalFooter: _.template(categoryModalFooter),
  34.             addEditForm: _.template(addEditCategoryForm),
  35.             addPropButton: _.template(addPropertyButtonAndResetMaster),
  36.             optionSelectionTemplate: _.template(optionSelectionTemplate),
  37.  
  38.             clicks: 0,
  39.             currentPage: 1,
  40.             emptyModalCategory: {
  41.                 modalTitle: "Add a new",
  42.                 modalFooter: "Save",
  43.                 modalId: "add",
  44.                 method: "post",
  45.                 id: "",
  46.                 name: "",
  47.                 needs_approval: false,
  48.                 authenticity_token: "",
  49.                 hide: false,
  50.             },
  51.  
  52.         initialize: function(scoping) {
  53.             var self = this,
  54.             type = "add",
  55.             reset = false;
  56.  
  57.             if (scoping) {
  58.                 this.scoping = scoping.scoping;
  59.             }
  60.  
  61.             this.$el.find(".add-category-modal").remove();
  62.  
  63.             this.$el.append(this.addEditCategoryTmpl(this.emptyModalCategory));
  64.  
  65.             $(".add-category-modal").modal("show");
  66.  
  67.             $(".add-category-modal .modal-content")
  68.                 .append(this.editCatTempForm(this.emptyModalCategory));
  69.             $(".add-form").append(self.addEditForm(self.emptyModalCategory));
  70.  
  71.              //Styling for input type file
  72.             $("#add-image-inputs").bootstrapFileInput();
  73.             $("#add-approval").append(this.needsApproval());
  74.  
  75.             $(".add-category-modal").find(".add-nested-fields-container")
  76.             .append(self.addPropButton({
  77.                 modalId: self.emptyModalCategory.modalId,
  78.                 reset: reset
  79.             }));
  80.  
  81.             $(".add-category-modal .modal-footer")
  82.             .append(this.modalFooter(self.emptyModalCategory));
  83.  
  84.             this.imageChanged("add");
  85.  
  86.             //Adding dynamic field
  87.             $("#add-category-form")
  88.             /*.bootstrapValidator({
  89.                 feedbackIcons: {
  90.                     valid: "glyphicon glyphicon-ok",
  91.                     invalid: "glyphicon glyphicon-remove",
  92.                     validating: "glyphicon glyphicon-refresh"
  93.                 },
  94.                 fields: {
  95.                     name: {
  96.                         validators: {
  97.                             notEmpty: {
  98.                                 message: "Name is required field",
  99.                             }
  100.                         }
  101.                     }
  102.                 }
  103.  
  104.             })*/
  105.             .off("click", ".add_add_nested_fields")
  106.             .off("click", ".remove_nested_fields_add")
  107.             .off("click", ".reset-master-property")
  108.             .on("click", ".reset-master-property", function() {
  109.  
  110.                 $("#add-category-form")
  111.                 .find(".master-radio")
  112.                 .prop("checked", false);
  113.             })
  114.             .on("click", ".add_add_nested_fields", function() {
  115.  
  116.                 var properties = $("#add-category-form").find(".property"),
  117.                     emptyInputs =false;
  118.                 if (properties.length === 0) {
  119.                         self.clicks++;
  120.                         $(".add-property-category").append(self.templates({
  121.                             modalId: self.emptyModalCategory.modalId,
  122.                             clicks: self.clicks
  123.                         }));
  124.                         properties = 0;
  125.                 } else if (properties.length > 0) {
  126.                     var prevProperty = $(properties[properties.length-1]),
  127.                         name = prevProperty.find(".property_name").val(),
  128.                         properties_type = prevProperty.find(".property_type").val(),
  129.                         select_value = prevProperty.find(".select").val();
  130.  
  131.                         if(properties_type === "select") {
  132.                             if( name === "" || properties_type === "" ||
  133.                                 select_value === "") {
  134.                                 emptyInputs = true;
  135.                             } else {
  136.                                 emptyInputs = false;
  137.                             }
  138.                         } else {
  139.                              if( name === "" || properties_type === "") {
  140.                                 emptyInputs = true;
  141.                             } else {
  142.                                 emptyInputs = false;
  143.                             }
  144.                         }
  145.  
  146.                     if (emptyInputs !== true) {
  147.                         self.clicks++;
  148.  
  149.                         $(".add-property-category").append(self.templates({
  150.                             modalId: self.emptyModalCategory.modalId,
  151.                             clicks: self.clicks
  152.                         }));
  153.                     } else {
  154.                         var message = "You must complete the required fields " +
  155.                             "before adding a new property.";
  156.                         Utils.addNoty("error", message);
  157.                     }
  158.                     properties = 0;
  159.                 }
  160.                 $("#add-category-form").bootstrapValidator("destroy", true);
  161.  
  162.                 $("#add-category-form")
  163.                     .bootstrapValidator({
  164.                     feedbackIcons: {
  165.                         valid: "glyphicon glyphicon-ok",
  166.                         invalid: "glyphicon glyphicon-remove",
  167.                         validating: "glyphicon glyphicon-refresh"
  168.                     },
  169.                     fields: {
  170.                         name: {
  171.                             validators: {
  172.                                 notEmpty: {
  173.                                     message: "Name is required field",
  174.                                 }
  175.                             }
  176.                         },
  177.                         properties_name:{
  178.                         selector:".property_name",
  179.                             validators: {
  180.                                 notEmpty: {
  181.                                 message: "Property name is required field",
  182.                                 }
  183.                             }
  184.                         },
  185.                         properties_type:{
  186.                             selector:".property_type",
  187.                             validators: {
  188.                                 notEmpty: {
  189.                                 message: "Property type is required field"
  190.                                 }
  191.                             }
  192.                         },
  193.                         select_values:{
  194.                             selector: ".select",
  195.                             validators: {
  196.                                 notEmpty: {
  197.                                 message: "Select values is required field"
  198.                                 }
  199.                             }
  200.                         },
  201.                     }
  202.  
  203.                 }).on("success.form.bv", function() {
  204.                     self.createCategory();
  205.                     return false;
  206.                 });
  207.                 self.radioListener("add");
  208.  
  209.                 self.propertiesType(type);
  210.  
  211.                 var $template = $(".property"),
  212.  
  213.                 $optionName = $template.find(".property_name"),
  214.                 $optionType = $template.find(".property_type"),
  215.                 $optionSelect = $template.find(".select");
  216.  
  217.                 var categoryForm = $("#add-category-form");
  218.                 categoryForm
  219.                     .data("bootstrapValidator")
  220.                     .addField($optionName)
  221.                     .addField($optionType)
  222.                     .addField($optionSelect);
  223.  
  224.             })
  225.             .on("click", ".remove_nested_fields_add", function() {
  226.  
  227.                 if ($(".add-category-modal .property").length === 1) {
  228.  
  229.                     $("#add-category-form").find(".add-nested-fields-container").html("");
  230.                     $("#add-category-form").find(".add-nested-fields-container").append(self.addPropButton({
  231.                         modalId:self.emptyModalCategory.modalId,
  232.                         reset: false
  233.                     }));
  234.                 }
  235.  
  236.                 self.propertiesType(type);
  237.  
  238.                 var $row = $(this).closest("div"),
  239.                     $optionName = $row.find(".property_name"),
  240.                     $optionType = $row.find(".property_type"),
  241.                     $optionSelect = $row.find(".select");
  242.  
  243.                 $row.remove();
  244.                 $("#add-category-form")
  245.                     .data("bootstrapValidator")
  246.                     .removeField($optionName)
  247.                     .removeField($optionType)
  248.                     .removeField($optionSelect);
  249.             });
  250.            /* .on("success.form.bv", function() {
  251.                 self.createCategory();
  252.                 return false;
  253.             });*/
  254.         },
  255.  
  256.         updateCategories: function(data, self) {
  257.             Utils.saveCategoriesCallback(data, self);
  258.         },
  259.  
  260.         addNotificationSuccess: function(message) {
  261.             Utils.addNoty("success", message);
  262.         },
  263.  
  264.         createCategory: function() {
  265.  
  266.             console.log("post");
  267.             var data = form2js("add-category-form", ".", true,
  268.                 function(node) {
  269.                     if (node.id && node.id.match(/callbackTest/)) {
  270.                         return { name: node.id, value: node.innerHTML };
  271.                     }
  272.  
  273.                 }),
  274.                 url = "/Categories",
  275.                 self = this,
  276.                 formObj = $("#add-category-form"),
  277.                 select_values = $("#add-category-form").find(".select");
  278.  
  279.             $.each(select_values, function(ind) {
  280.                 data.properties_attributes[ind].select_values =
  281.                     select_values[ind].value.split(", ");
  282.             });
  283.             Utils.dismissModal(false);
  284.             Utils.doPost(url, data, function(resp) {
  285.                 Utils.getAllCategoriesCall(self.updateCategories, self);
  286.  
  287.                 Utils.getAllCategoriesCall(function(data) {
  288.                     Utils.saveCategoriesCallback(data);
  289.                     self.setNewCategories(resp);
  290.                 }, Utils);
  291.  
  292.                 Utils.uploadImage(formObj, url, resp.id, function() {
  293.                     Backbone.pubSub.trigger("addCategory");
  294.                     self.addNotificationSuccess(Utils.error.success.categoryCreate);
  295.                 });
  296.  
  297.                 $(".add-category-modal").modal("hide");
  298.             }, function(jqXHR) {
  299.                     var message = jqXHR.responseJSON.error;
  300.                     Utils.addNoty("error", message);
  301.             });
  302.         },
  303.  
  304.         imageChanged: function(action) {
  305.  
  306.             //Preview for add/edit category.
  307.             $("#" + action + "-image-inputs").change(function() {
  308.                 var file = document.getElementById(action + "-image-inputs")
  309.                         .files[0],
  310.                 readImg = new FileReader();
  311.                 readImg.readAsDataURL(file);
  312.                 readImg.onload = function(e) {
  313.                     $(".img-"+ action + "-category").attr("src",e.target.result)
  314.                     .fadeIn();
  315.                 };
  316.  
  317.             });
  318.         },
  319.  
  320.         radioListener: function(modal) {
  321.  
  322.             var $radioInputs,
  323.                 self = this;
  324.             //If the method is call from the add modal.
  325.             if (modal == "add") {
  326.                 //Find the master radio buttons - add modal
  327.                 $radioInputs = $(".add-property-category").find(".master-radio");
  328.             }
  329.  
  330.             $radioInputs.on("change", function() {
  331.  
  332.                 $radioInputs.not($(this)).removeAttr("checked");
  333.  
  334.                 if ($(this).val()) {
  335.  
  336.                     $("." + modal + "-category-modal")
  337.                     .find(".add-nested-fields-container").html("");
  338.                     $("." + modal + "-category-modal")
  339.                     .find(".add-nested-fields-container")
  340.                     .append(self.addPropButton({
  341.                         modalId: modal,
  342.                         reset: true
  343.                     }));
  344.                 }
  345.                 $.each($radioInputs, function() {
  346.                     var $this = $(this),
  347.                     $hiddenSibling = $this.siblings(".master-radio-hidden");
  348.  
  349.                     if($this.is(":checked")) {
  350.  
  351.                         $hiddenSibling.attr("disabled", "disabled");
  352.                     } else {
  353.                         $hiddenSibling.removeAttr("disabled");
  354.                     }
  355.                 });
  356.             });
  357.         },
  358.  
  359.         propertiesType: function(actionType) {
  360.             $(".property_type")
  361.             .on("change", function() {
  362.                 var input = $(this).parents(".property").find(".select"),
  363.                     form,
  364.                     id,
  365.                     idAdd;
  366.  
  367.                 if( actionType == "add") {
  368.                     form = $("#add-category-form");
  369.                 }
  370.  
  371.                 if ($(this).val() === "text") {
  372.                     /*if (typeof form.data("bootstrapValidator") == "undefined") {
  373.                         Utils.validator(form,  function() {
  374.  
  375.                         }, Utils.error.add.categoryName);
  376.  
  377.                     }*/
  378.                     var invalid=$("#add-category-form").data("bootstrapValidator")
  379.                                 .validate().$invalidFields;
  380.                     $("#save-category").removeAttr("disabled");
  381.  
  382.                     console.log(invalid.length);
  383.  
  384.                     form.data("bootstrapValidator")
  385.                         .updateStatus("select_values","NOT_VALIDATED");
  386.                     input.siblings().hide();
  387.                     input.attr("disabled", "disabled");
  388.                     form.data("bootstrapValidator").removeField(input);
  389.                     input.val("");
  390.  
  391.                 } else {
  392.                     console.log("select");
  393.  
  394.                     var invalid = $("#add-category-form").data("bootstrapValidator")
  395.                                 .validate().$invalidFields;
  396.  
  397.                     console.log(invalid.length);
  398.  
  399.                     /*if (typeof form.data("bootstrapValidator") == "undefined") {
  400.                         Utils.validator(form,  function() {
  401.  
  402.                         }, Utils.error.add.categoryName);
  403.                     }*/
  404.                     input.attr("name", "select_values");
  405.                     form.data("bootstrapValidator").addField(input);
  406.                     form.data("bootstrapValidator").validateField("select_values");
  407.                     input.removeAttr("disabled");
  408.                 }
  409.             });
  410.         },
  411.  
  412.         setNewCategories: function(resp) {
  413.             var self = this,
  414.                 $dropdown = $("." + this.scoping + "-category");
  415.  
  416.  
  417.             this.categories = Utils.getLocalData("categories");
  418.             Utils.subStringFunction(self, self.categories, "name", 25);
  419.  
  420.             $dropdown.html(self.optionSelectionTemplate({
  421.                 data: self.categories,
  422.                 selectedType: "Select a category"
  423.             }));
  424.  
  425.             $dropdown.find("option[value=" + resp.id + "]").prop("selected", true);
  426.             $dropdown.trigger("change");
  427.         }
  428.     });
  429.     return AddCategoryView;
  430. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement