move some js to have more structure

This commit is contained in:
rabuzarus 2016-05-06 16:48:06 +02:00
parent 485b1e5521
commit 047722dc37
4 changed files with 269 additions and 266 deletions

View file

@ -163,148 +163,6 @@ function getContactData(purl, url, actionOnSuccess) {
}
getContactData.cache = {};
// current time in milliseconds, to send each request to make sure
// we 're not getting 304 response
function timeNow() {
return new Date().getTime();
}
String.prototype.normalizeLink = function () {
var ret = this.replace('https:', 'http:');
var ret = ret.replace('//www', '//');
return ret.rtrim();
};
function cleanContactUrl(url) {
var parts = parseUrl(url);
if(! ("scheme" in parts) || ! ("host" in parts)) {
return url;
}
var newUrl =parts["scheme"] + "://" + parts["host"];
if("port" in parts) {
newUrl += ":" + parts["port"];
}
if("path" in parts) {
newUrl += parts["path"];
}
// if(url != newUrl) {
// console.log("Cleaned contact url " + url + " to " + newUrl);
// }
return newUrl;
}
function parseUrl (str, component) { // eslint-disable-line camelcase
// discuss at: http://locutusjs.io/php/parse_url/
// original by: Steven Levithan (http://blog.stevenlevithan.com)
// reimplemented by: Brett Zamir (http://brett-zamir.me)
// input by: Lorenzo Pisani
// input by: Tony
// improved by: Brett Zamir (http://brett-zamir.me)
// note 1: original by http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
// note 1: blog post at http://blog.stevenlevithan.com/archives/parseuri
// note 1: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
// note 1: Does not replace invalid characters with '_' as in PHP,
// note 1: nor does it return false with
// note 1: a seriously malformed URL.
// note 1: Besides function name, is essentially the same as parseUri as
// note 1: well as our allowing
// note 1: an extra slash after the scheme/protocol (to allow file:/// as in PHP)
// example 1: parse_url('http://user:pass@host/path?a=v#a')
// returns 1: {scheme: 'http', host: 'host', user: 'user', pass: 'pass', path: '/path', query: 'a=v', fragment: 'a'}
// example 2: parse_url('http://en.wikipedia.org/wiki/%22@%22_%28album%29')
// returns 2: {scheme: 'http', host: 'en.wikipedia.org', path: '/wiki/%22@%22_%28album%29'}
// example 3: parse_url('https://host.domain.tld/a@b.c/folder')
// returns 3: {scheme: 'https', host: 'host.domain.tld', path: '/a@b.c/folder'}
// example 4: parse_url('https://gooduser:secretpassword@www.example.com/a@b.c/folder?foo=bar')
// returns 4: { scheme: 'https', host: 'www.example.com', path: '/a@b.c/folder', query: 'foo=bar', user: 'gooduser', pass: 'secretpassword' }
var query
var mode = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.mode') : undefined) || 'php'
var key = [
'source',
'scheme',
'authority',
'userInfo',
'user',
'pass',
'host',
'port',
'relative',
'path',
'directory',
'file',
'query',
'fragment'
]
// For loose we added one optional slash to post-scheme to catch file:/// (should restrict this)
var parser = {
php: new RegExp([
'(?:([^:\\/?#]+):)?',
'(?:\\/\\/()(?:(?:()(?:([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
'()',
'(?:(()(?:(?:[^?#\\/]*\\/)*)()(?:[^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
].join('')),
strict: new RegExp([
'(?:([^:\\/?#]+):)?',
'(?:\\/\\/((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
'((((?:[^?#\\/]*\\/)*)([^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
].join('')),
loose: new RegExp([
'(?:(?![^:@]+:[^:@\\/]*@)([^:\\/?#.]+):)?',
'(?:\\/\\/\\/?)?',
'((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?)',
'(((\\/(?:[^?#](?![^?#\\/]*\\.[^?#\\/.]+(?:[?#]|$)))*\\/?)?([^?#\\/]*))',
'(?:\\?([^#]*))?(?:#(.*))?)'
].join(''))
}
var m = parser[mode].exec(str)
var uri = {}
var i = 14
while (i--) {
if (m[i]) {
uri[key[i]] = m[i]
}
}
if (component) {
return uri[component.replace('PHP_URL_', '').toLowerCase()]
}
if (mode !== 'php') {
var name = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.queryKey') : undefined) || 'queryKey'
parser = /(?:^|&)([^&=]*)=?([^&]*)/g
uri[name] = {}
query = uri[key[12]] || ''
query.replace(parser, function ($0, $1, $2) {
if ($1) {
uri[name][$1] = $2
}
})
}
delete uri.source
return uri
}
// trim function to replace whithespace after the string
String.prototype.rtrim = function() {
var trimmed = this.replace(/\s+$/g, '');
return trimmed;
};
// Get hover-card template data and the contact-data and transform it with
// the help of jSmart. At the end we have full html content of the hovercard
function getHoverCardContent(purl, url, callback) {

127
js/modal.js Normal file
View file

@ -0,0 +1,127 @@
/*
* @brief contains functions for bootstrap modal handling
*/
// Clear bs modal on close
// We need this to prevent that the modal displays old content
$('body').on('hidden.bs.modal', '.modal', function () {
$(this).removeData('bs.modal');
$("#modal-title").empty();
$('#modal-body').empty();
});
/**
* @brief Add first h3 element as modal title
*
* Note: this should be really done in the template
* and is the solution where we havent done it until this
* moment or where it isn't possible because of design
*/
function loadModalTitle() {
// clear the text of the title
//$("#modal-title").empty();
// hide the first h3 child element of the modal body
$("#modal-body > h3").first().hide();
// get the text of the first h3 child element
var title = $("#modal-body > h3").first().text();
// and append it to modal title
if (title!=="") {
$("#modal-title").append(title);
}
}
function addToModal(url) {
var char = qOrAmp(url);
var url = url + char + 'mode=modal';
var modal = $('#modal').modal();
modal
.find('#modal-body')
.load(url, function (responseText, textStatus) {
if ( textStatus === 'success' ||
textStatus === 'notmodified')
{
modal.show();
//Get first h3 element and use it as title
loadModalTitle();
}
});
};
function editpost(url) {
var modal = $('#jot-modal').modal();
var url = url + " #profile-jot-form";
//var rand_num = random_digits(12);
$("#jot-perms-lnk").hide();
// rename the the original div jot-preview-content because the edit function
// does load the content for the modal from another source and preview won't work
// if this div would exist twice
// $("#jot-content #profile-jot-form").attr("id","#profile-jot-form-renamed");
// $("#jot-content #jot-preview-content").attr("id","#jot-preview-content-renamed");
// For editpost we load the modal html form the edit page. So we would have two jot forms in
// the page html. To avoid js conflicts we move the original jot to the end of the page
// so the editpost jot would be the first jot in html structure.
// After closing the modal we move the original jot back to it's orginal position in the html structure.
//
// Note: For now it seems to work but this isn't optimal because we have doubled ID names for the jot div's.
// We need to have a better solution for this in the future.
$("section #jot-content #profile-jot-form").appendTo("footer #cache-container");
jotreset();
modal
.find('#jot-modal-body')
.load(url, function (responseText, textStatus) {
if ( textStatus === 'success' ||
textStatus === 'notmodified')
{
// get the item type and hide the input for title and category if it isn't needed
var type = $(responseText).find("#profile-jot-form input[name='type']").val();
if(type === "wall-comment" || type === "remote-comment")
{
$("#profile-jot-form #jot-title-wrap").hide();
$("#profile-jot-form #jot-category-wrap").hide();
}
modal.show();
$("#jot-popup").show();
}
});
}
function jotreset() {
// Clear bs modal on close
// We need this to prevent that the modal displays old content
$('body').on('hidden.bs.modal', '#jot-modal', function () {
$(this).removeData('bs.modal');
$("#jot-perms-lnk").show();
$("#profile-jot-form #jot-title-wrap").show();
$("#profile-jot-form #jot-category-wrap").show();
// the following was commented out because it is needed anymore
// because we changed the behavior at an other place
// var rand_num = random_digits(12);
// $('#jot-title, #jot-category, #profile-jot-text').val("");
// $( "#profile-jot-form input[name='type']" ).val("wall");
// $( "#profile-jot-form input[name='post_id']" ).val("");
// $( "#profile-jot-form input[name='post_id_random']" ).val(rand_num);
$("#jot-modal-body").empty();
// rename the div #jot-preview-content-renamed back to it's original
// name. Have a look at function editpost() for further explanation
//$("#jot-content #profile-jot-form-renamed").attr("id","#profile-jot-form");
//$("#jot-content #jot-preview-content-renamed").attr("id","#jot-preview-content");
// Move the original jot back to it's old place in the html structure
// For explaination have a look at function editpost()
$("footer #cache-container #profile-jot-form").appendTo("section #jot-content");
});
}

View file

@ -16,14 +16,6 @@ $(document).ready(function(){
return false;
});
// Clear bs modal on close
// We need this to prevent that the modal displays old content
$('body').on('hidden.bs.modal', '.modal', function () {
$(this).removeData('bs.modal');
$("#modal-title").empty();
$('#modal-body').empty();
});
// add the class "selected" to group widges li if li > a does have the class group-selected
if( $("#sidebar-group-ul li a").hasClass("group-selected")) {
$("#sidebar-group-ul li a.group-selected").parent("li").addClass("selected");
@ -267,122 +259,6 @@ function loadScript(url, callback) {
head.appendChild(script);
}
/**
* @brief Add first h3 element as modal title
*
* Note: this should be really done in the template
* and is the solution where we havent done it until this
* moment or where it isn't possible because of design
*/
function loadModalTitle() {
// clear the text of the title
//$("#modal-title").empty();
// hide the first h3 child element of the modal body
$("#modal-body > h3").first().hide();
// get the text of the first h3 child element
var title = $("#modal-body > h3").first().text();
// and append it to modal title
if (title!=="") {
$("#modal-title").append(title);
}
}
function addToModal(url) {
var char = qOrAmp(url);
var url = url + char + 'mode=modal';
var modal = $('#modal').modal();
modal
.find('#modal-body')
.load(url, function (responseText, textStatus) {
if ( textStatus === 'success' ||
textStatus === 'notmodified')
{
modal.show();
//Get first h3 element and use it as title
loadModalTitle();
}
});
};
function editpost(url) {
var modal = $('#jot-modal').modal();
var url = url + " #profile-jot-form";
//var rand_num = random_digits(12);
$("#jot-perms-lnk").hide();
// rename the the original div jot-preview-content because the edit function
// does load the content for the modal from another source and preview won't work
// if this div would exist twice
// $("#jot-content #profile-jot-form").attr("id","#profile-jot-form-renamed");
// $("#jot-content #jot-preview-content").attr("id","#jot-preview-content-renamed");
// For editpost we load the modal html form the edit page. So we would have two jot forms in
// the page html. To avoid js conflicts we move the original jot to the end of the page
// so the editpost jot would be the first jot in html structure.
// After closing the modal we move the original jot back to it's orginal position in the html structure.
//
// Note: For now it seems to work but this isn't optimal because we have doubled ID names for the jot div's.
// We need to have a better solution for this in the future.
$("section #jot-content #profile-jot-form").appendTo("footer #cache-container");
jotreset();
modal
.find('#jot-modal-body')
.load(url, function (responseText, textStatus) {
if ( textStatus === 'success' ||
textStatus === 'notmodified')
{
// get the item type and hide the input for title and category if it isn't needed
var type = $(responseText).find("#profile-jot-form input[name='type']").val();
if(type === "wall-comment" || type === "remote-comment")
{
$("#profile-jot-form #jot-title-wrap").hide();
$("#profile-jot-form #jot-category-wrap").hide();
}
modal.show();
$("#jot-popup").show();
}
});
}
function jotreset() {
// Clear bs modal on close
// We need this to prevent that the modal displays old content
$('body').on('hidden.bs.modal', '#jot-modal', function () {
$(this).removeData('bs.modal');
$("#jot-perms-lnk").show();
$("#profile-jot-form #jot-title-wrap").show();
$("#profile-jot-form #jot-category-wrap").show();
// the following was commented out because it is needed anymore
// because we changed the behavior at an other place
// var rand_num = random_digits(12);
// $('#jot-title, #jot-category, #profile-jot-text').val("");
// $( "#profile-jot-form input[name='type']" ).val("wall");
// $( "#profile-jot-form input[name='post_id']" ).val("");
// $( "#profile-jot-form input[name='post_id_random']" ).val(rand_num);
$("#jot-modal-body").empty();
// rename the div #jot-preview-content-renamed back to it's original
// name. Have a look at function editpost() for further explanation
//$("#jot-content #profile-jot-form-renamed").attr("id","#profile-jot-form");
//$("#jot-content #jot-preview-content-renamed").attr("id","#jot-preview-content");
// Move the original jot back to it's old place in the html structure
// For explaination have a look at function editpost()
$("footer #cache-container #profile-jot-form").appendTo("section #jot-content");
});
}
function random_digits(digits) {
var rn = "";
var rnd = "";
@ -509,3 +385,144 @@ function filter_replace(item) {
a.on('textComplete:select', function(e, value, strategy) { $(".dropdown-menu.textcomplete-dropdown.media-list").show(); });
};
})( jQuery );
// current time in milliseconds, to send each request to make sure
// we 're not getting 304 response
function timeNow() {
return new Date().getTime();
}
String.prototype.normalizeLink = function () {
var ret = this.replace('https:', 'http:');
var ret = ret.replace('//www', '//');
return ret.rtrim();
};
function cleanContactUrl(url) {
var parts = parseUrl(url);
if(! ("scheme" in parts) || ! ("host" in parts)) {
return url;
}
var newUrl =parts["scheme"] + "://" + parts["host"];
if("port" in parts) {
newUrl += ":" + parts["port"];
}
if("path" in parts) {
newUrl += parts["path"];
}
// if(url != newUrl) {
// console.log("Cleaned contact url " + url + " to " + newUrl);
// }
return newUrl;
}
function parseUrl (str, component) { // eslint-disable-line camelcase
// discuss at: http://locutusjs.io/php/parse_url/
// original by: Steven Levithan (http://blog.stevenlevithan.com)
// reimplemented by: Brett Zamir (http://brett-zamir.me)
// input by: Lorenzo Pisani
// input by: Tony
// improved by: Brett Zamir (http://brett-zamir.me)
// note 1: original by http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
// note 1: blog post at http://blog.stevenlevithan.com/archives/parseuri
// note 1: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
// note 1: Does not replace invalid characters with '_' as in PHP,
// note 1: nor does it return false with
// note 1: a seriously malformed URL.
// note 1: Besides function name, is essentially the same as parseUri as
// note 1: well as our allowing
// note 1: an extra slash after the scheme/protocol (to allow file:/// as in PHP)
// example 1: parse_url('http://user:pass@host/path?a=v#a')
// returns 1: {scheme: 'http', host: 'host', user: 'user', pass: 'pass', path: '/path', query: 'a=v', fragment: 'a'}
// example 2: parse_url('http://en.wikipedia.org/wiki/%22@%22_%28album%29')
// returns 2: {scheme: 'http', host: 'en.wikipedia.org', path: '/wiki/%22@%22_%28album%29'}
// example 3: parse_url('https://host.domain.tld/a@b.c/folder')
// returns 3: {scheme: 'https', host: 'host.domain.tld', path: '/a@b.c/folder'}
// example 4: parse_url('https://gooduser:secretpassword@www.example.com/a@b.c/folder?foo=bar')
// returns 4: { scheme: 'https', host: 'www.example.com', path: '/a@b.c/folder', query: 'foo=bar', user: 'gooduser', pass: 'secretpassword' }
var query
var mode = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.mode') : undefined) || 'php'
var key = [
'source',
'scheme',
'authority',
'userInfo',
'user',
'pass',
'host',
'port',
'relative',
'path',
'directory',
'file',
'query',
'fragment'
]
// For loose we added one optional slash to post-scheme to catch file:/// (should restrict this)
var parser = {
php: new RegExp([
'(?:([^:\\/?#]+):)?',
'(?:\\/\\/()(?:(?:()(?:([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
'()',
'(?:(()(?:(?:[^?#\\/]*\\/)*)()(?:[^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
].join('')),
strict: new RegExp([
'(?:([^:\\/?#]+):)?',
'(?:\\/\\/((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?',
'((((?:[^?#\\/]*\\/)*)([^?#]*))(?:\\?([^#]*))?(?:#(.*))?)'
].join('')),
loose: new RegExp([
'(?:(?![^:@]+:[^:@\\/]*@)([^:\\/?#.]+):)?',
'(?:\\/\\/\\/?)?',
'((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?)',
'(((\\/(?:[^?#](?![^?#\\/]*\\.[^?#\\/.]+(?:[?#]|$)))*\\/?)?([^?#\\/]*))',
'(?:\\?([^#]*))?(?:#(.*))?)'
].join(''))
}
var m = parser[mode].exec(str)
var uri = {}
var i = 14
while (i--) {
if (m[i]) {
uri[key[i]] = m[i]
}
}
if (component) {
return uri[component.replace('PHP_URL_', '').toLowerCase()]
}
if (mode !== 'php') {
var name = (typeof require !== 'undefined' ? require('../info/ini_get')('locutus.parse_url.queryKey') : undefined) || 'queryKey'
parser = /(?:^|&)([^&=]*)=?([^&]*)/g
uri[name] = {}
query = uri[key[12]] || ''
query.replace(parser, function ($0, $1, $2) {
if ($1) {
uri[name][$1] = $2
}
})
}
delete uri.source
return uri
}
// trim function to replace whithespace after the string
String.prototype.rtrim = function() {
var trimmed = this.replace(/\s+$/g, '');
return trimmed;
};

View file

@ -160,6 +160,7 @@ $("nav").bind('nav-update', function(e,data)
<script src="<?=$frio?>/frameworks/flexMenu/flexmenu.custom.js"></script>
<script src="<?=$frio?>/frameworks/jsmart/jsmart.custom.js"></script>
<script src="<?=$frio?>/js/theme.js"></script>
<script src="<?=$frio?>/js/modal.js"></script>
<script src="<?=$frio?>/js/hovercard.js"></script>