Advertisement
3gsxarakiri

Untitled

Nov 27th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class LocaleGenerator {
  2.             constructor(parent){
  3.                 this.parent = parent;
  4.                 this.count = this.parent.children().length;
  5.                 this.locales = this.prepareLocales();
  6.                 this.queriesSets = this.prepareQueriesSets();
  7.                 this.existLanguages = [];
  8.  
  9.                 let fg = this.parent.children();
  10.                 fg.each((elem,val)=>{
  11.                     this.existLanguages.push($(val).find('option:selected').first().text())
  12.                 });
  13.             }
  14.  
  15.             generate(){
  16.                 if(this.existLanguages.length === this.locales.length){
  17.                     return alert('No available languages');
  18.                 }
  19.  
  20.                 let form_group = $('<div class="form-group">'),
  21.                     label = $('<label class="control-label col-lg-2 required">').text(this.count),
  22.                     div_10 = $('<div class="col-lg-10">'),
  23.                     div = $('<div>').attr('id', `app_basebundle_site_locales_${this.count}`);
  24.  
  25.                 this.parent.append(
  26.                     form_group.append(
  27.                         label,
  28.                         div_10.append(
  29.                             div.append(
  30.                                 this.renderSelect('Language'),
  31.                                 this.renderCheckBox('Enabled'),
  32.                                 this.renderCheckBox('Default'),
  33.                                 this.renderSelect('Queries')
  34.                             )
  35.                         )
  36.                     )
  37.                 );
  38.             }
  39.  
  40.             renderCheckBox($type){
  41.                 let div_fp = $('<div class="form-group">'),
  42.                     div_checkbox = $('<div class="checkbox">'),
  43.                     label = '',
  44.                     input = '';
  45.  
  46.                 if($type === 'Default'){
  47.                     label = $('<label>').attr('for', `app_basebundle_site_locales_${this.count}_default`);
  48.                     input = $('<input type="checkbox" value="0">').attr({
  49.                         'name' : `app_basebundle_site[locales][${this.count}][default]`,
  50.                         'id' : `app_basebundle_site_locales_${this.count}_default`
  51.                     });
  52.                 }
  53.  
  54.                 if($type === 'Enabled'){
  55.                     label = $('<label>').attr('for', `app_basebundle_site_locales_${this.count}_enabled`);
  56.                     input = $('<input type="checkbox" value="1" checked>').attr({
  57.                         'name' : `app_basebundle_site[locales][${this.count}][enabled]`,
  58.                         'id' : `app_basebundle_site_locales_${this.count}_enabled`
  59.                     });
  60.                 }
  61.  
  62.                 return div_fp.append(
  63.                     div_checkbox.append(
  64.                         label.append(input, $type)
  65.                     )
  66.                 );
  67.             }
  68.  
  69.             renderSelect($type){
  70.                 let label = '',
  71.                     select = '',
  72.                     div_fp = $('<div class="form-group">'),
  73.                     div_col_lg_10 = $('<div class="col-lg-10">');
  74.  
  75.                 if($type === 'Language'){
  76.  
  77.                     label = $('<label class="control-label col-lg-2 required">')
  78.                         .text('Language')
  79.                         .attr('for',`app_basebundle_site_locales_${this.count}_langId`);
  80.  
  81.                     select = $('<select class="form-control" required disabled>').attr({
  82.                         'id' : `app_basebundle_site_locales_${this.count}_langId`,
  83.                         'name' : `app_basebundle_site[locales][${this.count}][langId]`,
  84.                         'disabled' : 'disabled',
  85.                         'required' : 'required',
  86.                     });
  87.  
  88.                     for (let i in this.locales){
  89.                         if($.inArray(this.locales[i].name, this.existLanguages) === -1){
  90.                             select.append(
  91.                                 $('<option>').attr('selected', 'selected').val(this.locales[i].id).text(this.locales[i].name)
  92.                             );
  93.                         } else {
  94.                             select.append(
  95.                                 $('<option>').val(this.locales[i].id).text(this.locales[i].name)
  96.                             );
  97.                         }
  98.                     }
  99.                 }
  100.  
  101.                 if($type === 'Queries'){
  102.                     label = $('<label class="control-label col-lg-2 required">')
  103.                         .text('Queries set id')
  104.                         .attr('for',`app_basebundle_site_locales_${this.count}_queriesSetId`);
  105.  
  106.                     select = $('<select class="form-control" required>').attr({
  107.                         'name' : `app_basebundle_site[locales][${this.count}][queriesSetId]`,
  108.                         'id' : `app_basebundle_site_locales_${this.count}_queriesSetId`
  109.                     });
  110.  
  111.                     for (let i in this.queriesSets){
  112.                         select.append(
  113.                             $('<option>').val(this.queriesSets[i].id).text(this.queriesSets[i].name)
  114.                         );
  115.                     }
  116.                 }
  117.  
  118.                 return div_fp.append(
  119.                     label, div_col_lg_10.append(select)
  120.                 );
  121.             }
  122.  
  123.             prepareLocales(){
  124.                 let firstLocale = this.parent.find($('.form-group')).first(),
  125.                     firstSelect = firstLocale.find($('select')).first(),
  126.                     options = firstSelect.find($('option')),
  127.                     languages = [];
  128.  
  129.                 options.each((elem, value) => {
  130.                     let val = $(value);
  131.                     languages.push({
  132.                         'id' : val.val(),
  133.                         'name' : val.text()
  134.                     });
  135.                 });
  136.  
  137.                 return languages;
  138.             }
  139.  
  140.             prepareQueriesSets(){
  141.                 let firstLocale = this.parent.find($('.form-group')).first(),
  142.                     firstSelect = firstLocale.find($('select')).last(),
  143.                     options = firstSelect.find($('option')),
  144.                     queriesSets = [];
  145.  
  146.                 options.each((elem, value) => {
  147.                     let val = $(value);
  148.                     queriesSets.push({
  149.                         'id' : val.val(),
  150.                         'name' : val.text()
  151.                     });
  152.                 });
  153.  
  154.                 return queriesSets;
  155.             }
  156.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement