Wednesday, November 30, 2011

Windows Notepad++ delete line shortcut

Command to delete a line in Notepad++ (WIndows):
CTRL + L

Wednesday, November 23, 2011

jQuery - Check / uncheck all checkboxes on a page

Master checkbox element:
<input type="checkbox" name="checkAllAuto" id="checkAllAuto" value="1"/>

Assigning extra JS event to the control checkbox above:
$('#checkAllAuto').click(
   function()
   {
      $("INPUT[type='checkbox']").attr('checked', $('#checkAllAuto').is(':checked'));
   }
)

Thursday, November 10, 2011

Ubuntu svn client

Eclipse - do not create project file (change project file default location)

For some reasons you don't want Eclipse to create the .project file at the same location with the files of the project (maybe you don't have write permissions to the root folder of the project).

The goal is to change .project file location for Eclipse.

Solution: Linked Resources.
You can create a virtual folder (variable) and then include it in the project, created at another location. Changing anything in the virtual folder will change it at the original location.

1) Create a new project; location for the project: local hdd, anywhere...
2) Go to: Window->Preferences->General->Workspace->Linked Resources and create a new resource that points to the folder you need
3) In the project you created at 1) right click and select "New folder" -> Advanced and include the variable (virtual folder)
4) Done! Fixed!

Sunday, November 6, 2011

JavaScript - Extend/duplicate/copy object with jQuery


var Object1 = jQuery.extend(true, {}, Object);
var Object2 = jQuery.extend(true, {}, Object);
......

Friday, November 4, 2011

JavaScript - Remove element from array (optionally preserve keys)

JavaScript - Remove element from array (unset array element)

1) Method 1: splice
array.splice(position, number_of_keys);
Example:
array.splice(1, 1);

OBS: The function doesn't preserve key association.

2) Method 2:
To preserve key association, you must do something like this:

var my_array = [1, 4, 5, 6];
var remove_key = 1;
var array_new = [];
for(var i in my_array) {
    if(i != remove_key) {
        array_new[i] = my_array[i];
    }
}


JavaScript - Generate random number (range)

JavaScript : random number range

function randomRange(minVal, maxVal, floatVal) {
    var randVal = minVal+(Math.random()*(maxVal-minVal));
    return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

floatVal - optional, without it, the function will return an integer; it represents the number of decimal points; precision

Custom JavaScript list - custom data elements

Custom JavaScript list Add / Remove elements.

Example: add / remove <li> element to <ul> list:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
<style type="text/css">
.list1 {}
.list1 li {list-style:none;}
</style>
</head>
<body>


<script type="text/javascript">

var ListManager = {
  
    next_position : 0,
    elems : [],
  
    add : function(_data) {
        this.elems[this.next_position] = _data;
        this.next_position++;
        this.addHTMLelement(this.next_position - 1);
        /* this.displayConsole(); */
    },
    remove : function(_pos) {
        /* this.elems.splice(_pos, 1); */
        var elems_new = [];
        for(var i in this.elems) {
            if(i != _pos) {
                elems_new[i] = this.elems[i];
            }
        }
        this.elems = elems_new;
        /* this.displayConsole(); */
    },
    displayConsole : function() {
        console.log('----------------');
        for(var i in this.elems) {
            console.log(i + ' : ' + this.elems[i].text + ', ' + this.elems[i].value);
        }
    },
  
    addHTMLelement : function(_pos) {
        var html = '';
        html += '<li id="list-element-'+_pos+'">';
        html += '<span style="cursor:pointer;color:red;" onclick="ListManager.remove(\''+_pos+'\');ListManager.removeHTMLelement(\''+_pos+'\');">x</span>';
        html += ' - ';
        html += this.elems[_pos].text;
        html += '</li>';
        $('#list1').append(html);
    },
  
    removeHTMLelement : function(_pos) {
        $('#list-element-'+_pos).css({display:'none'});
    }
  
}

function addElementToList(_value, _text) {
    ListManager.add({
        value : _value,
        text  : _text
    });
}

function randomRange(minVal, maxVal, floatVal) {
    var randVal = minVal+(Math.random()*(maxVal-minVal));
    return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

</script>


<input type="text" name="text1" id="text1" value=""/>
<input type="button" name="button1" value="add" onclick="addElementToList(randomRange(5, 15), $('#text1').val())"/>
<input type="button" name="button2" value="clear" onclick="$('#text1').val('');"/>

<ul id="list1" class="list1"></ul>


</body>
</html>


jQuery AJAX cross domain request

Cross domain AJAX request

$(document).ready(function() {
  $.getJSON('http://twitter.com/users/usejquery.json?callback=?', function(json) {
    //get information about the user usejquery from twitter api
    $('#twitter_followers').text(json.followers_count);
    //get the follower_count from the json object and put it in a span
  });
});

http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide