keyboard_arrow_up
").find(selector).html(); //angular.element(data).filter('.container').html();
/**
* We compile the html so that the directives start well
* and other angularjs mierdecicas
*/
if (compile) var result = $compile(result)($scope);
/**
* We return the compiled html
*/
return result;
}
/**
* This function is responsible for loading a url into a variable and returning it
*/
$scope.load_url_content = function(url) {
/**
* In this variable we keep what will be the response of the function
* false if it gives error or an attach_id if it has been successful.
*/
var result = false;
/**
* We create a deferred object that will help us to manage the promise.
*/
var defer = $q.defer();
/**
* This action helps us to execute code before it starts executing
* the function that connects to the url and extracts the content
*/
loader.show();
/**
* We make the call http
*/
var request = $http({
method: "get",
url: url
});
/**
* If the call has been successful ...
*/
request.then(function(response) {
/**
* We update the current code
*/
$scope.current_html = response.data;
/**
* We create a new doctype
*/
var parser = new DOMParser();
$scope.current_doc = parser.parseFromString(response.data, "text/html");
/**
* This action helps us to execute code at the moment in which
* the request ends with exit
*/
history.pushState({}, url, url);
/**
*
*/
result = response.data;
});
/**
* Check if an error has occurred
*/
request.catch(function(response) {
/**
* This action helps us to execute code at the moment in which
* the request ends with error
*/
/**
* We return the error message
*/
});
/**
* Actions to be carried out once the call is finished
*/
request.finally(function() {
/**
* This action helps us to execute code at the moment in which
* the request ends (either satisfactorily or note)
*/
if ( jQuery('#site-menu-block').hasClass("open")) {
jQuery('#site-menu-block').removeClass('open');
jQuery('html #site-wrap.overlay-open').removeClass('overlay-open');
}
jQuery('.site-menu-handle-close').hide();
jQuery('.site-menu-handle-open').show();
/**
* In the defer we define the result of this function
*/
if (result) defer.resolve(result)
else defer.reject(result);
});
/**
* we return the premise
*/
return defer.promise;
}
/**
* This function is responsible for adding to a link the bind to work with ajax
*/
$scope.add_ajax_to_link = function(link_object) {
/**
* We get the DOM element with its attributes
*/
link_object = angular.element(link_object);
/**
* We add the binding
*/
link_object.unbind("click").bind('click', function(e) {
/**
* If we are facing a valid url then we are going to add the bind that
* will transform it into an ajax link
*/
if ($scope.link_is_ajax_friendly(link_object)) {
/**
* We prevent the link from moving forward the load
*/
e.preventDefault();
/**
* We capture the url
*/
var url = link_object.context.attributes.href.value;
/**
* We load the url with ajax
*/
$scope.load_ajax_url(url);
}
});
}
/**
* This function is responsible for loading a url ajax and do everything necessary
* to update the page as we have configured it
*/
$scope.load_ajax_url = function(url) {
/**
* We contacted the url to obtain its content.
*/
var ajax_request = $scope.load_url_content(url);
/**
* In case of success ...
*/
ajax_request.then(function(data){
/**
* We update the header
*/
$scope.update_page_head();
$scope.update_page_footer();
/**
* We obtain the html content of the divs that interest us
*/
$scope.parse_html_contents(data);
/**
* We update the classes of the most important elements of the DOCTYPE html
* like the html and the body
*/
$scope.update_classes();
/**
* We update the divs
*/
$scope.update_ajax_parts();
/**
* We make the new links also be ajax
*/
$scope.convert_links_to_ajax();
/**
* This action helps us to execute code before it starts executing
* the function that connects to the url and extracts the content
*/
window.scrollTo(0, 0);
loader.hide();
setTimeout(function () {
// jquery ready (works?): https://stackoverflow.com/a/7135839
jQuery.ready()
// event DOMContentLoaded
var DOMContentLoaded_event = document.createEvent("Event");
DOMContentLoaded_event.initEvent("DOMContentLoaded", true, true);
window.document.dispatchEvent(DOMContentLoaded_event);
}, 1000);
});
}
/**
* This function is responsible for updating the classes of the elements
* most important of the document
*/
$scope.update_classes = function() {
jQuery("html").removeClass();
jQuery("html").addClass(jQuery($scope.current_doc).find('html').attr("class"));
jQuery("body").removeClass();
jQuery("body").addClass(jQuery($scope.current_doc).find('body').attr("class"));
}
/**
* This function is responsible for obtaining the content of the divs that we are from the html code
* looking to update.
*/
$scope.parse_html_contents = function(html) {
/**
* We're going for each of the divs we're looking for
*/
angular.forEach($scope.parts_to_update, function(part) {
/**
* We update your content with the content we have obtained
* of the html.
*/
part.content = $scope.get_data_div_content(part.selector, html, part.compile);
});
}
/**
* This function is responsible for updating the content of the ajax parts
*/
$scope.update_ajax_parts = function() {
/**
* We're going for each of the divs we're looking for
*/
angular.forEach($scope.parts_to_update, function(part) {
/**
* We add the compiled code to the div
*/
if (Object.keys(part.content).length > 1) jQuery(part.selector).html(part.content);
});
}
/**
* This function is responsible for evaluating if a url is valid to use ajax
* We should not transform into link ajax those that go to an external url
*/
$scope.link_is_ajax_friendly = function(link_element) {
/**
* If the hostname of the home element with our current url then we return
* true since it is an internal link
*/
if (window.location.hostname === link_element.context.hostname || !link_element.context.hostname.length ) {
/**
* Before returning a true let's check that it is not an administration URL
* in this case we must return false;
*/
if (link_element.context.href.indexOf('wp-admin') != -1) return false;
/**
* We must check if an extension exists at the termination of the url
* archive
*/
if (link_element.context.href.split('.').pop().length