Plugin Directory

Changeset 1555032


Ignore:
Timestamp:
12/14/2016 10:43:10 PM (9 years ago)
Author:
fifthestate
Message:

Effin' CORS

Location:
fifthestate/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • fifthestate/trunk/js/category-helper.js

    r1555009 r1555032  
    77    this.html('<input type=hidden name="category_id">');
    88    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 = {};
    169
    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 = {};
    2813
    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]);
    3821            }
    3922        }
     23        return JSON.parse(templStr, fmt);
     24    }
    4025
    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))
    5133            }
     34            worldCat.subcategories.push(continentCat);
    5235        }
    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);
    6054
    6155    return this;
    6256};
    6357
    64 function createDropdownsFromCategories(dropdownsRoot, parentId, categories, path) {
     58function createDropdownsFromCategories(dropdownsRoot, parent, categories, path) {
    6559    if(path == null) path = [];
    66     path.push(parentId);
     60    path.push(parent && parent._id);
    6761    var level = path.length;
    6862    if(level <= MAX_CATEGORY_LEVELS) {
     
    7064        if(filteredCategories.length > 0) {
    7165            var sElem = document.createElement("select");
    72             sElem.className = 'cat-'+parentId;
     66            sElem.className = 'cat-' + (parent && parent._id);
    7367            sElem.onchange = function(event) {
    7468                var selectedCat;
     
    8074                }
    8175                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];
    8477                }
    8578
    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);
    9680            };
    9781            dropdownsRoot.appendChild(sElem);
     82            if(parent != null) {
     83                sElem.options.add(new Option('-- Select a category --', null));
     84            }
    9885            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());
    10087                var option = new Option(cat.name, cat._id);
    10188                sElem.options.add(option);
     
    10592        }
    10693    } 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');
    10895    }
    10996    return [null, path];
     
    123110                        firstSelectableCategory = cat;
    124111                    }
    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;
    130117                }
    131118            }
  • fifthestate/trunk/settings.php

    r1555006 r1555032  
    160160    //GET category tree
    161161    $category_tree = curl_get( SITE_URL . '/data/categories.json', '' );
     162    $country_data = curl_get( SITE_URL . '/data/country_data.json', '' );
    162163    ?>
    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>
    164166    <form method="post" action="">
    165167        <?php
Note: See TracChangeset for help on using the changeset viewer.