Changeset 1555032
- Timestamp:
- 12/14/2016 10:43:10 PM (9 years ago)
- Location:
- fifthestate/trunk
- Files:
-
- 2 edited
-
js/category-helper.js (modified) (5 diffs)
-
settings.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
fifthestate/trunk/js/category-helper.js
r1555009 r1555032 7 7 this.html('<input type=hidden name="category_id">'); 8 8 var elem = this.get(0); 9 $.when(10 $.get(categoriesEndpoint),11 $.get(countryDataEndpoint)12 ).then(function(categoriesRes, countryDataRes) {13 categories = categoriesRes[0];14 countryData = countryDataRes[0];15 categoryMap = {};16 9 17 function formatDict(template, fmt) { 18 // NOTE: This part differs from the backend and feels hackier to me 19 // but is based on http://stackoverflow.com/a/5344074/1796894 20 var templStr = JSON.stringify(template); 21 for(var prop in fmt) { 22 if(fmt.hasOwnProperty(prop)) { 23 templStr = templStr.replace(new RegExp('{'+prop+'}', 'g'), fmt[prop]); 24 } 25 } 26 return JSON.parse(templStr, fmt); 27 } 10 categories = JSON.parse($("#category_tree").text()); 11 countryData = JSON.parse($("#country_data").text()); 12 categoryMap = {}; 28 13 29 function prepareWorldCategory(worldCat) { 30 worldCat.subcategories = []; 31 for(var continent of countryData.continents) { 32 var continentCat = formatDict(worldCat.continent_template, continent); 33 continentCat.subcategories = [] 34 for(var country of continent.countries) { 35 continentCat.subcategories.push(formatDict(worldCat.country_template, country)) 36 } 37 worldCat.subcategories.push(continentCat); 14 function formatDict(template, fmt) { 15 // NOTE: This part differs from the backend and feels hackier to me 16 // but is based on http://stackoverflow.com/a/5344074/1796894 17 var templStr = JSON.stringify(template); 18 for(var prop in fmt) { 19 if(fmt.hasOwnProperty(prop)) { 20 templStr = templStr.replace(new RegExp('{'+prop+'}', 'g'), fmt[prop]); 38 21 } 39 22 } 23 return JSON.parse(templStr, fmt); 24 } 40 25 41 function walker(categories, parent) { 42 for(var cat of categories) { 43 cat.parent = parent; 44 categoryMap[cat._id] = cat; 45 if(cat.type === 'world') { 46 // TODO: I am writing this twice QQ 47 // See categories.py on the backend 48 prepareWorldCategory(cat); 49 } 50 walker(cat.subcategories, cat); 26 function prepareWorldCategory(worldCat) { 27 worldCat.subcategories = []; 28 for(var continent of countryData.continents) { 29 var continentCat = formatDict(worldCat.continent_template, continent); 30 continentCat.subcategories = [] 31 for(var country of continent.countries) { 32 continentCat.subcategories.push(formatDict(worldCat.country_template, country)) 51 33 } 34 worldCat.subcategories.push(continentCat); 52 35 } 53 walker(categories); 54 }, function(err) { 55 console.error(err); 56 }).then(function() { 57 createDropdownsFromCategories(elem, 'root', categories); 58 selectFromCategories(elem, categories, currentCategoryId); 59 }); 36 } 37 38 function walker(categories, parent) { 39 for(var cat of categories) { 40 cat.parent = parent; 41 categoryMap[cat._id] = cat; 42 if(cat.type === 'world') { 43 // TODO: I am writing this twice QQ 44 // See categories.py on the backend 45 prepareWorldCategory(cat); 46 } 47 walker(cat.subcategories, cat); 48 } 49 } 50 walker(categories); 51 52 createDropdownsFromCategories(elem, null, categories); 53 selectFromCategories(elem, categories, currentCategoryId); 60 54 61 55 return this; 62 56 }; 63 57 64 function createDropdownsFromCategories(dropdownsRoot, parent Id, categories, path) {58 function createDropdownsFromCategories(dropdownsRoot, parent, categories, path) { 65 59 if(path == null) path = []; 66 path.push(parent Id);60 path.push(parent && parent._id); 67 61 var level = path.length; 68 62 if(level <= MAX_CATEGORY_LEVELS) { … … 70 64 if(filteredCategories.length > 0) { 71 65 var sElem = document.createElement("select"); 72 sElem.className = 'cat-' +parentId;66 sElem.className = 'cat-' + (parent && parent._id); 73 67 sElem.onchange = function(event) { 74 68 var selectedCat; … … 80 74 } 81 75 if(!selectedCat) { 82 console.warn('Could not find postable category '+event.target.value+' in categories of '+parentId); 83 selectedCat = filteredCategories[0]; 76 selectedCat = categoryMap[parent && parent._id]; 84 77 } 85 78 86 if(selectedCat.subcategories.length <= 0) { 87 selectCategory(dropdownsRoot, selectedCat); 88 } else if(selectedCat.subcategories.length > 0) { 89 var successfullySelected = selectFromCategories(dropdownsRoot, selectedCat.subcategories); 90 if(!successfullySelected) { 91 selectCategory(dropdownsRoot, selectedCat); 92 } 93 } else { 94 console.error(selectedCat.name+' selected somehow?'); 95 } 79 selectCategory(dropdownsRoot, selectedCat); 96 80 }; 97 81 dropdownsRoot.appendChild(sElem); 82 if(parent != null) { 83 sElem.options.add(new Option('-- Select a category --', null)); 84 } 98 85 for(var cat of filteredCategories) { 99 [cat.selectElem, cat.path] = createDropdownsFromCategories(dropdownsRoot, cat ._id, cat.subcategories, path.slice());86 [cat.selectElem, cat.path] = createDropdownsFromCategories(dropdownsRoot, cat, cat.subcategories, path.slice()); 100 87 var option = new Option(cat.name, cat._id); 101 88 sElem.options.add(option); … … 105 92 } 106 93 } else { 107 console.warn('List for '+ parentId+' found at level '+level+' which is too deep');94 console.warn('List for '+(parent && parent._id)+' found at level '+level+' which is too deep'); 108 95 } 109 96 return [null, path]; … … 123 110 firstSelectableCategory = cat; 124 111 } 125 if(selectedCategoryId && selectedCategoryId === cat._id) {126 foundSelectedCategory = true;127 selectCategory(dropdownsRoot, cat);128 break;129 }112 } 113 if(selectedCategoryId && selectedCategoryId === cat._id) { 114 foundSelectedCategory = true; 115 selectCategory(dropdownsRoot, cat); 116 break; 130 117 } 131 118 } -
fifthestate/trunk/settings.php
r1555006 r1555032 160 160 //GET category tree 161 161 $category_tree = curl_get( SITE_URL . '/data/categories.json', '' ); 162 $country_data = curl_get( SITE_URL . '/data/country_data.json', '' ); 162 163 ?> 163 <!-- <div id="category_tree" style="display:none"><?php echo $category_tree ?></div> --> 164 <div id="category_tree" style="display:none"><?php echo $category_tree ?></div> 165 <div id="country_data" style="display:none"><?php echo $country_data ?></div> 164 166 <form method="post" action=""> 165 167 <?php
Note: See TracChangeset
for help on using the changeset viewer.