Hello i'd like to put condition for my code in javascript if the code has already called, so after the _edg.source it wont to called again:
for (var j in GexfJS.graph.edgeList) {
var _edg = GexfJS.graph.edgeList[j]
if ( (_edg.target == _curra) && (_edg.source != _nodeIndex) && (_edg.target != _n)) {
var _nod = GexfJS.graph.nodeList[_edg.source];
if(_n != _nod){
_str += '<li><div class="smallpill" style="background: ' + _nod.color.base +'"></div>' + _nod.label + 'b' + ( GexfJS.params.showEdgeWeight && _edg.weight ? ' [' + _edg.weight + ']' : '') + '</li>';
}
}
}
}
try to use condition with an object and set the true value after the value has called, insert this code after the var _nod
check={};
if(check[_nod.label] != true){
......
}
check[_nod.label] = true;
Related
I want to create dots in breadcrumb if they exceeds some specific limit of numbers. I added script, to create breadcrumb but it's not working.
I am stuck where i have to add dots so that when bread crumb item exceeds it hides and create dots
function getBreadCrumb() {
if (window.ActiveTab != 'local') {
$('#fr-h-r-3 .breadcrumb.fr-breadcrumb').html('');
return false
}
/*alert(window.ActiveTab);
if(window.ActiveTab == 'fb'){
$('#fr-h-r-3 .breadcrumb.fr-breadcrumb').html('<li>Facebook-Images</li>');
return false;
}else if(window.ActiveTab == 'ig'){
$('#fr-h-r-3 .breadcrumb.fr-breadcrumb').html('<li>Instagram-Images</li>');
return false
}*/
var currentPath = b.opts.userFolderDefaultPath;
if (b.opts.imageManagerFolders.length > 0) {
currentPath = b.opts.userFolderDefaultPath + b.opts.imageManagerFolders.join('/') + '/';
}
var sArray = currentPath.split('/');
var fArray = currentPath.split('/');
fArray.pop();
fArray.shift();
sArray.shift();
sArray.shift();
sArray.shift();
sArray.pop();
var html = '<li><span class="fa fa-home"></span></li>';
/*console.log('sArray: '+sArray);
console.log('fArray: '+fArray);*/
function getPath(i) {
var hisPath = '';
for (var j = 2; j < (i + 3); j++) {
if (fArray[j] != 'undefined') {
hisPath += fArray[j] + '/';
}
}
return hisPath;
}
if (sArray.length > 6) {
as = 4;
al = (sArray.length - 3)
}
sts = false;
for (var i = 0; i < sArray.length; i++) {
if ((i + 1) != sArray.length) {
if (typeof as != 'undefined' && i == as) {
sts = true;
}
if (typeof al != 'undefined' && i == al) sts = false;
if (sts == false) {
html += '<li><a onClick="$(document).trigger(\'ImageManager.LoadBreadCrumbFolder\',[this]);" href="javascript:void(0);" data-name="' + sArray[i] + '" data-hist="' + getPath(i) + '" data-path="/' + fArray[0] + '/' + fArray[1] + '/' + getPath(i) + '">' + sArray[i] + '</a></li>';
}
} else {
html += '<li class="active">' + sArray[i] + '</li>';
}
//html += '<li class="active">'+sArray[i]+'</li>';
}
$('#fr-h-r-3 .breadcrumb.fr-breadcrumb').html(html);
}
Thank you for your efforts, i was working on this, i solved this by only adding this code into function.
if(typeof as != 'undefined' && i==as) { sts = true; html += '<li>...</li>'; }
where the line is
if (typeof as != 'undefined' && i == as) {
sts = true;
}
this script is generating a pagination you can see in script. and the final output is this.
simple pagination without dots.
checkttttNew-Folder-1New-Folder-1New-Folder-1
when exceeds 6 li then it creates dots. like this.
checkttttNew-Folder-1New-Folder-1...New-Folder-1htesttes2
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Is there a cleaner, more concise way to write the following conditional statement that creates a string in Javascript?
var search;
if (text === '' && user === '' && filter !== '') {
search = filter;
} else if (filter === '' && text === '' && user !== '') {
search = 'email="' + user + '"';
} else if (filter === '' && user === '' && text !== '') {
search = 'text~"' + text + '"';
} else if (text !== '' && user !== '' && filter === '') {
search = 'text~"' + text + '" ANDemail="' + user + '"';
} else if (text !== '' && filter !== '' && user === '') {
search = 'text~"' + text + '" AND ' + filter;
} else {
search = 'text~"' + text + '" AND ' + filter + '" ANDemail="' + user + '"';
}
// using computed switch
var TEXT = 1, haveText = (text !== "") << 0;
var FILTER = 2, haveFilter = (filter !== "") << 1;
var USER = 4, haveUser = (user !== "") << 2;
switch(haveText + haveFilter + haveUser)
{
case FILTER: search = filter; break;
case USER: search = 'email="' + user + '"'; break;
case TEXT: search = 'text~"' + text + '"'; break;
case TEXT+USER: search = 'text~"' + text + '" AND email="' + user + '"'; break;
case TEXT+FILTER: search = 'text~"' + text + '" AND ' + filter; break;
case TEXT+FILTER+USER: search = 'text~"' + text + '" AND ' + filter + '" AND email="' + user + '"'; break;
case FILTER+USER: search = filter + '" AND email="' + user + '"'; break;
default: search = ""; // no search criteria
}
makes two possible errors in the compound if statement version stand out:
The case of FILTER+USER was not tested for and produced a search using TEXT,
The case of no criteria was not tested in the if statement and also produced a search using TEXT.
Off the top of my head, one thing you can do to simplify this is to use the js rule that '' falsy which simplifies this. Also, short circuits with return would help.
var search;
function getSearch(user, text, filter) {
if (!text && !user && filter) return filter;
if (!filter && !text && user) return 'email="' + user + '"';
if (!filter && !user && text ) return 'text~"' + text + '"';
if (text && user && !filter) return 'text~"' + text + '" ANDemail="' + user + '"';
if (text && filter && !user) return 'text~"' + text + '" AND ' + filter;
return 'text~"' + text + '" AND ' + filter + '" ANDemail="' + user + '"';
}
search = getSearch(user, text, filter);
I think that's a little cleaner. It could be cleaned up a lot if the formatting of your string wasn't so strange, but I'm assuming it's specific and required so this maintains the exact formatting you're after in the "search" string.
You can make a function to build the search string, that way you can easily add more variables in the future. The builder is messy, it's a messy process, but it won't get much more messy, no matter how many variables you add. As a side-note, remember that in some cases like this (with a lot of conditionals) you can consider refactoring using polymorphism, that probably wouldn't work for you here, though. The fiddle for this is here: https://jsfiddle.net/ytg1rxu8/
function buildSearchString(text, user, filter) {
var returnString = '';
var strings = [];
if (text) {strings.push('text~ =' + text);}
if (user) {strings.push('email = ' + user);}
if(filter){
strings.push(filter);}
var length = strings.length;
for(var i =0; i < length; ++i){
var s = strings[i];
if(i ===length -1){
returnString += s;
}
else{
returnString += s+' AND ' ;
}
}
return returnString;
}
alert(buildSearchString('', 'there', 'guy'));
The following may fit the criterion "concise", but it may not fit "cleaner":
var text = 'someText';
var filter = '';
var user = 'aUser';
var search = text? 'text~"' + text + '"' : '';
search += search && filter? ' AND ' + filter : filter? filter : '';
search += search && user? ' AND email="' + user + '"' : user? 'email="' + user + '"' : '';
document.write('Search: ' + search); // text~"someText" AND email="aUser"
var search;
if (filter)
search = appendFilter(search, filter);
if (text)
search = appendFilter(search, 'text~"' + text + '"');
if (user)
search = appendFilter(search, 'email="' + user + '"');
function appendFilter(search, f) {
return search ? search + ' AND ' + f : f;
}
Trying to add a div around some javascript code.
Here's the code I'm trying to modify:
slider.controlNavScaffold = $('<ol class="'+ namespace + 'control-nav ' + namespace + type + '"></ol>');
if (slider.pagingCount > 1) {
for (var i = 0; i < slider.pagingCount; i++) {
slide = slider.slides.eq(i);
item = (slider.vars.controlNav === "thumbnails") ? '<img src="' + slide.attr( 'data-thumb' ) + '"/>' : '<a>' + j + '</a>';
if ( 'thumbnails' === slider.vars.controlNav && true === slider.vars.thumbCaptions ) {
var captn = slide.attr( 'data-thumbcaption' );
if ( '' != captn && undefined != captn ) item += '<span class="' + namespace + 'caption">' + captn + '</span>';
}
slider.controlNavScaffold.append('<li>' + item + '</li>');
j++;
}
}
Here's the resulted outcome when you add <div class="container"> before the <ol> and closing </div> tag after </ol> in the code above...as you can see the list closes before list items make it inside:
<div class="container"><ol class="flex-control-nav flex-control-paging"></ol><li><a>1</a></li><li><a>2</a></li><li><a>3</a></li><li><a>4</a></li></div>
Here's what I'm trying to output.
<div class="container"><ol class="flex-control-nav flex-control-paging"><li><a class="">1</a></li><li><a class="flex-active">2</a></li><li><a>3</a></li><li><a>4</a></li></ol></div>
Code that isn't working:
slider.controlNavScaffold = $('<div class="container"><ol class="'+ namespace + 'control-nav ' + namespace + type + '"></ol></div>');
if (slider.pagingCount > 1) {
for (var i = 0; i < slider.pagingCount; i++) {
slide = slider.slides.eq(i);
item = (slider.vars.controlNav === "thumbnails") ? '<img src="' + slide.attr( 'data-thumb' ) + '"/>' : '<a>' + j + '</a>';
if ( 'thumbnails' === slider.vars.controlNav && true === slider.vars.thumbCaptions ) {
var captn = slide.attr( 'data-thumbcaption' );
if ( '' != captn && undefined != captn ) item += '<span class="' + namespace + 'caption">' + captn + '</span>';
}
slider.controlNavScaffold.append('<li>' + item + '</li>');
j++;
}
}
You didn't post it, but I suspect that you changed that first line to
slider.controlNavScaffold = $('<div><ol class="'+ namespace + 'control-nav ' + namespace + type + '"></ol></div>');
That will cause the code to do exactly what you describe, because the .append() calls will append to the outer element (the <div>).
Instead, leave that first line alone, and then at the end — after the <li> elements have been added — add this:
slider.controlNavScaffold.wrap( $('<div/>', { "class": "container" }) );
After that, in order to have things work properly when you actually add the stuff to the DOM, you'll want to find the parent of the <ol> and make sure that that's what you add:
slider.controlNavWrapper = slider.controlNavScaffold.parent();
(or however you want to keep track of it).
I want to convert this shortcode generating form into one that will generate either of two different shortcodes depending on a value selected in the form. So, a radio button says, "Which shortcode do you want to build?" Then they choose it, then they go on with the other fields to fill out the attribute values. Then when it comes time to generate the code, the JS will condition its output based on the radio button question. I've tried to modify it myself, but the problem is, this script generates the attributes from the options index, so I don't know how to include an option that doesn't go into the index:
var table = form.find('table');
form.appendTo('body').hide();
form.find('#myshortcodeidstem-submit').click(function(){
var options = {
'shortcodename' : '', \\ THIS IS THE ONE TO DETERMINE THE SHORTCODE NAME
'attribute' : '', \\ THIS IS THE ATTRIBUTE THAT BOTH SHORTCODES SHARE
};
var shortcode = '[myshortcode'; \\ THIS LINE NEEDS TO BE CONDITIONAL ON OPTION 1
for( var index in options) {
var value = table.find('#myshortcodeidstem-' + index).val();
if ( value !== options[index] && value != null )
shortcode += ' ' + index + '="' + value + '"';
}
shortcode += '] Content Here [/myshortcode]'; \\ THIS LINE CONDITIONAL ON OP1
--- UPDATE ---
Barmar pointed me in the right direction, and I got it to work, but I'd like to know if there's a more economical way to do it. Here's what I have:
var table = form.find('table');
form.appendTo('body').hide();
form.find('#myshortcodeid-submit').click(function(){
var codeselector = table.find('#myshortcodeid-codeselector').val();
if (codeselector === '1'){
var options = {
'attribute' : '',
};
var shortcode = '[shortcode_one';
for( var index in options) {
var value = table.find('#myshortcodeid-' + index).val();
if ( value !== options[index] && value != null )
shortcode += ' ' + index + '="' + value + '"';
}
shortcode += '] Content Here [/shortcode_one]';
}
if (codeselector === '2'){
var options = {
'attribute' : '',
};
var shortcode = '[shortcode_two';
for( var index in options) {
var value = table.find('#myshortcodeid-' + index).val();
if ( value !== options[index] && value != null )
shortcode += ' ' + index + '="' + value + '"';
}
shortcode += '] Content Here [/shortcode_two]';
}
--- UPDATE ---
Found a more economical way, without repeating the options index. See the answer below.
Here's the most economic way I could come up with. Doesn't repeat the options index this way. Working good. Just had to create a var for the dropdown field that chooses the shortcode, then do if statements referencing that var's value.
var codeselector = table.find('#myid-codeselector').val();
if (codeselector === '1'){
var shortcode = '[shortcode_one';
}
if (codeselector === '2'){
var shortcode = '[shortcode_two';
}
var options = {
'attribute' : '',
};
for( var index in options) {
var value = table.find('#myid-' + index).val();
if ( value !== options[index] && value != null )
shortcode += ' ' + index + '="' + value + '"';
}
if (codeselector === '1'){
shortcode += '] Content Here [/shortcode_one]';
}
if (codeselector === '2'){
shortcode += '] Content Here [/shortcode_two]';
}
I have the code below, but items.push does not work when it is inside the if statement. If uncomment the line before ending } then items.push works as expected.
for (i = 0; i < len; i += 1) {
row = resultexpense.rows.item(i);
t.executeSql('SELECT * FROM expensepayments WHERE Barcode = ?',
[row.barcode],
function(t, resultpaid) {
var myrowpaid,
myrowpaidlen;
myrowpaidlen = resultpaid.rows.length;
alert(myrowpaidlen); //alerts 1
if (myrowpaidlen > 0){
myrowpaid = resultpaid.rows.item(0);
alert(row.amount); //alerts 90
alert(myrowpaid.Amount); //alerts 50
if (row.amount > myrowpaid.Amount){
alert(row.amount- myrowpaid.Amount); //alerts 40
items.push('<li>' + row.description + '</li>');
}
} else {
items.push('<li>' + row.description + '</li>');
}
});
// items.push('<li>' + row.description + '</li>');
}
I am not really sure what typeof your variable is, is it number or just a string?
if typeof row.amount == "string" or typeof myrowpaid.Amount == "string" then the if condition not going to execute.
to be sure, your variable is number type, use parseInt() function to convert into number
if (parseInt(row.amount, 10) > parseInt(myrowpaid.Amount, 10)){
alert(row.amount- myrowpaid.Amount); //alerts 40
items.push('<li>' + row.description + '</li>');
}
if you not already deceleared the items variable then, add var items = []; before use of items variable