|
@@ -1,10 +1,11 @@
|
|
|
var BASE_URL = 'http://192.168.1.127/api/';
|
|
var BASE_URL = 'http://192.168.1.127/api/';
|
|
|
|
|
+
|
|
|
function http(url, params) {
|
|
function http(url, params) {
|
|
|
return new Promise(function(resolve, reject) {
|
|
return new Promise(function(resolve, reject) {
|
|
|
httpRequest = new XMLHttpRequest();
|
|
httpRequest = new XMLHttpRequest();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
httpRequest.onreadystatechange = function() {
|
|
httpRequest.onreadystatechange = function() {
|
|
|
- if (httpRequest.readyState == XMLHttpRequest.DONE ) {
|
|
|
|
|
|
|
+ if (httpRequest.readyState == XMLHttpRequest.DONE) {
|
|
|
if (httpRequest.status == 200) {
|
|
if (httpRequest.status == 200) {
|
|
|
resolve(JSON.parse(httpRequest.responseText));
|
|
resolve(JSON.parse(httpRequest.responseText));
|
|
|
} else {
|
|
} else {
|
|
@@ -12,7 +13,7 @@ function http(url, params) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
httpRequest.open('GET', url, true);
|
|
httpRequest.open('GET', url, true);
|
|
|
httpRequest.setRequestHeader('lang', 'ES')
|
|
httpRequest.setRequestHeader('lang', 'ES')
|
|
|
var x = params ? params : null;
|
|
var x = params ? params : null;
|
|
@@ -21,12 +22,12 @@ function http(url, params) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
http('http://192.168.1.127/api/products/listcategories').then(function(data) {
|
|
http('http://192.168.1.127/api/products/listcategories').then(function(data) {
|
|
|
-
|
|
|
|
|
-}, function(err){
|
|
|
|
|
|
|
+
|
|
|
|
|
+}, function(err) {
|
|
|
console.log("err", err);
|
|
console.log("err", err);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-function logError (err) {
|
|
|
|
|
|
|
+function logError(err) {
|
|
|
console.log('Error: ', err);
|
|
console.log('Error: ', err);
|
|
|
}
|
|
}
|
|
|
var sections = [
|
|
var sections = [
|
|
@@ -34,12 +35,15 @@ var sections = [
|
|
|
'productos'
|
|
'productos'
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
-function initProducts(category) {
|
|
|
|
|
- http(BASE_URL + 'products/category/' + category).then(function (products) {
|
|
|
|
|
- products.forEach(function (product) {
|
|
|
|
|
- productsListed_.push(product);
|
|
|
|
|
- })
|
|
|
|
|
- }, logError)
|
|
|
|
|
|
|
+function getProducts(category) {
|
|
|
|
|
+ if (R.isNil(data.allProducts[category]) || R.isEmpty(data.allProducts[category])) {
|
|
|
|
|
+ http(BASE_URL + 'products/category/' + category).then(function(xs) {
|
|
|
|
|
+ data.allProducts[category] = xs;
|
|
|
|
|
+ data.products = xs;
|
|
|
|
|
+ }, logError)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ data.products = data.allProducts[category];
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
var selectedSection_ = {
|
|
var selectedSection_ = {
|
|
@@ -47,15 +51,16 @@ var selectedSection_ = {
|
|
|
productos: false
|
|
productos: false
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-var productsListed_ = [];
|
|
|
|
|
|
|
+var selectedCategory_ = 'proteccion intensiva';
|
|
|
|
|
+
|
|
|
rivets.configure({
|
|
rivets.configure({
|
|
|
executeFunctions: true
|
|
executeFunctions: true
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
function getCategories() {
|
|
function getCategories() {
|
|
|
- if(productCategories_.length === 0) {
|
|
|
|
|
- http(BASE_URL + 'products/listcategories').then(function (data) {
|
|
|
|
|
- data.forEach(function (category) {
|
|
|
|
|
|
|
+ if (productCategories_.length === 0) {
|
|
|
|
|
+ http(BASE_URL + 'products/listcategories').then(function(data) {
|
|
|
|
|
+ data.forEach(function(category) {
|
|
|
productCategories_.push(category)
|
|
productCategories_.push(category)
|
|
|
})
|
|
})
|
|
|
}, logError)
|
|
}, logError)
|
|
@@ -67,49 +72,68 @@ var productCategories_ = [];
|
|
|
var app = document.getElementById('app');
|
|
var app = document.getElementById('app');
|
|
|
|
|
|
|
|
rivets.binders['src-strict'] = function(el, value) {
|
|
rivets.binders['src-strict'] = function(el, value) {
|
|
|
- var img = new Image()
|
|
|
|
|
-
|
|
|
|
|
- img.onload = function() {
|
|
|
|
|
- $(el).attr('src', value)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ var img = new Image()
|
|
|
|
|
+ img.onload = function() {
|
|
|
|
|
+ $(el).attr('src', value)
|
|
|
|
|
+ }
|
|
|
|
|
+ img.src = value
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- img.src = value
|
|
|
|
|
|
|
+var data = {
|
|
|
|
|
+ allProducts: {},
|
|
|
|
|
+ products: [],
|
|
|
|
|
+ selectedCategory: ''
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
var view = rivets.bind(app, {
|
|
var view = rivets.bind(app, {
|
|
|
sections: sections,
|
|
sections: sections,
|
|
|
selectedSection: selectedSection_,
|
|
selectedSection: selectedSection_,
|
|
|
productCategories: productCategories_,
|
|
productCategories: productCategories_,
|
|
|
- productsListed: productsListed_,
|
|
|
|
|
- selectSection: function (event, scope) {
|
|
|
|
|
|
|
+ data: data,
|
|
|
|
|
+ selectedCategory: selectedCategory_,
|
|
|
|
|
+ selectSection: function(event, scope) {
|
|
|
var newValue = event.target.text;
|
|
var newValue = event.target.text;
|
|
|
window.history.pushState({}, newValue, '#' + newValue)
|
|
window.history.pushState({}, newValue, '#' + newValue)
|
|
|
chooseSection(newValue);
|
|
chooseSection(newValue);
|
|
|
event.target.className = 'selected';
|
|
event.target.className = 'selected';
|
|
|
scope.getData[newValue]();
|
|
scope.getData[newValue]();
|
|
|
},
|
|
},
|
|
|
- selectCategory: function (event, scope) {
|
|
|
|
|
|
|
+ selectCategory: function(event, scope) {
|
|
|
var category = productCategories_.find(R.propEq('name', event.target.text));
|
|
var category = productCategories_.find(R.propEq('name', event.target.text));
|
|
|
- initProducts(category._id)
|
|
|
|
|
|
|
+ var categoryList = getElement('ul#product-category-nav > li > a')
|
|
|
|
|
+ data.selectedCategory = category;
|
|
|
|
|
+ getProducts(category._id);
|
|
|
|
|
+ addSelectedClass(categoryList, category.name);
|
|
|
},
|
|
},
|
|
|
getData: {
|
|
getData: {
|
|
|
productos: getCategories,
|
|
productos: getCategories,
|
|
|
- empresa: function(){}
|
|
|
|
|
|
|
+ empresa: function() {}
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+function getElement(selector) {
|
|
|
|
|
+ return window.document.querySelectorAll(selector);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
function chooseSection(section) {
|
|
function chooseSection(section) {
|
|
|
- var navbarList = window.document.querySelectorAll('ul.navbar-list > li > a');
|
|
|
|
|
- navbarList.forEach(function (el) {
|
|
|
|
|
- var isSelected = el.text === section;
|
|
|
|
|
|
|
+ var navbarList = getElement('ul.navbar-list > li > a');
|
|
|
|
|
+ addSelectedClass(navbarList, section, selectedSection_)
|
|
|
|
|
+}
|
|
|
|
|
+var elements = [];
|
|
|
|
|
+
|
|
|
|
|
+function addSelectedClass(elemList, x, opt) {
|
|
|
|
|
+ console.log('elemList: ', elemList);
|
|
|
|
|
+ elemList.forEach(function(el) {
|
|
|
|
|
+ var isSelected = el.text === x;
|
|
|
el.className = isSelected ? 'selected' : '';
|
|
el.className = isSelected ? 'selected' : '';
|
|
|
- selectedSection_[el.text] = isSelected;
|
|
|
|
|
|
|
+ if (opt) {
|
|
|
|
|
+ opt[el.text] = isSelected;
|
|
|
|
|
+ }
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
-var elements = [];
|
|
|
|
|
|
|
|
|
|
-window.onscroll = function () {
|
|
|
|
|
- elements = elements.reduce(function (acc, e) {
|
|
|
|
|
|
|
+window.onscroll = function() {
|
|
|
|
|
+ elements = elements.reduce(function(acc, e) {
|
|
|
if (isInViewport(e)) {
|
|
if (isInViewport(e)) {
|
|
|
e.className = 'discover-li'
|
|
e.className = 'discover-li'
|
|
|
return acc;
|
|
return acc;
|
|
@@ -119,7 +143,7 @@ window.onscroll = function () {
|
|
|
}
|
|
}
|
|
|
}, [])
|
|
}, [])
|
|
|
if (elements.length === 0) {
|
|
if (elements.length === 0) {
|
|
|
- window.onscroll = function () {};
|
|
|
|
|
|
|
+ window.onscroll = function() {};
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -127,11 +151,11 @@ function isInViewport(element) {
|
|
|
var rect = element.getBoundingClientRect();
|
|
var rect = element.getBoundingClientRect();
|
|
|
var html = document.documentElement;
|
|
var html = document.documentElement;
|
|
|
return (rect.top >= 0 &&
|
|
return (rect.top >= 0 &&
|
|
|
- rect.left >= 0 &&
|
|
|
|
|
- rect.bottom <= (window.innerHeight || html.clientHeight) &&
|
|
|
|
|
- rect.right <= (window.innerWidth || html.clientWidth))
|
|
|
|
|
|
|
+ rect.left >= 0 &&
|
|
|
|
|
+ rect.bottom <= (window.innerHeight || html.clientHeight) &&
|
|
|
|
|
+ rect.right <= (window.innerWidth || html.clientWidth))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-window.onload = function () {
|
|
|
|
|
|
|
+window.onload = function() {
|
|
|
chooseSection(window.location.hash.substr(1));
|
|
chooseSection(window.location.hash.substr(1));
|
|
|
}
|
|
}
|