I was trying to do this in IntelliJ using Structural Search but this
function doesn't seem to work on non-recogised files (ie, java/ xml/
etc).. I just wanted to replace the contents of a plain text file with
a larger block of text.
Ended up doing this manually but if I wanted to fiddle, perhaps this
approach using sed would have been the right way to go:
http://stackoverflow.com/questions/6524896/unix-easiest-way-of-recursively-replacing-a-block-of-multi-line-text
Friday, 3 May 2013
Thursday, 2 May 2013
JQuery: disable options on a 2nd select box, when a specific selection is chosen in 1st select box
Backing up this JSFiddle I was playing around with at:
http://jsfiddle.net/hatft/3/
Scenario:
* User has selectboxes to enter "Opening hours", starting time and
closing times using two select boxes.
* When user selects "Closed" in first selectbox, the 2nd select box
must not allow the user to choose any times, and must have "-" as the
value.
* When user changes from "Closed" to some other value, the 2nd select
box must work as normal, allowing user to choose anything
Strictly speaking, we would probably want to do more validation so that:
- closing time is mandatory when start time is selected
- closing time is not earlier than starting time
HTML:
<h3 class="margin-bottom10">Your opening hours</h3>
<table>
<tbody>
<tr class="opening-hours-mon-fri display_none">
<td>Monday</td>
<td>
<select id="postad-monFrom" name="openhrs_mon_from">
<option>Please select</option>
<option value="closed">Closed</option>
<option value="0:00am">0:00am</option>
<option value="0:30am">0:30am</option>
<option value="1:00am">1:00am</option>
</select>
<!--<label for="postad-monTo">to</label>-->to
<select id="postad-monTo"
name="attributeMap[appliance_phone_repair.openhrs_mon_to_s]">
<option></option>
<option value="-">-</option>
<option value="0:00am">0:00am</option>
<option value="0:30am">0:30am</option>
<option value="1:00am">1:00am</option>
</select>
</td>
</tr>
<tr class="opening-hours-mon-fri">
<td>
<label for="postad-tuesFrom">Tuesday</label>
</td>
<td>
<select id="postad-tuesFrom"
name="attributeMap[appliance_phone_repair.openhrs_tues_from_s]">
<option>Select time</option>
<option value="closed">Closed</option>
<option value="0:00am">0:00am</option>
<option value="0:30am">0:30am</option>
<option value="1:00am">1:00am</option>
</select>
<label for="postad-tuesTo">to</label>
<select id="postad-tuesTo"
name="attributeMap[appliance_phone_repair.openhrs_tues_to_s]">
<option></option>
<option value="-">-</option>
<option value="0:00am">0:00am</option>
<option value="0:30am">0:30am</option>
<option value="1:00am">1:00am</option>
</select>
</td>
</tr>
</tbody>
</table>
JAVASCRIPT (uses jQuery 1.6.4)
// TEST SELECTORS BY CHANGING BKGROUND COLOR
//$('[class^="opening-hours-"]').css("background-color", "red");
$('[class^="opening-hours-"]').find('select[id$="From"]').css("background-color",
"yellow");
/*
TIL:
1. You need to use .nextAll() instead of .next()
next() ONLY WORKS IF THE SELECT BOX IS THE NEXT ELEMENT.
IF THERE IS ANYTHING IN BETWEEN, LIKE A <label>, then this won't work.
the earlier version of this only works because "to" was not inside a label
2. When "Closed" is chosen, set the "to" selectbox to "-" and don't
allow any other choices
- set disabled attribute on the options that were not selected
- when value of "From" selectbox is NOT "Closed", then remove disabled
attribute on the "To" options
*/
$('[class^="opening-hours-"]').find('select[id$="From"]').change(function () {
if ($(this).val() == 'closed') {
/*
DOES NOT WORK BECAUSE .next() only gets the
immediately following element.
*/
var $toSelect = $(this).nextAll('select[id$="To"]');
$toSelect.css("background-color", "red");
$toSelect.val('-');
$toSelect.find(':not(:selected)').attr('disabled','disabled');
} else {
$(this).nextAll('select[id$="To"]').find(':not(:selected)').removeAttr('disabled');
$(this).nextAll('select[id$="To"]').css("background-color", "white");
}
});
http://jsfiddle.net/hatft/3/
Scenario:
* User has selectboxes to enter "Opening hours", starting time and
closing times using two select boxes.
* When user selects "Closed" in first selectbox, the 2nd select box
must not allow the user to choose any times, and must have "-" as the
value.
* When user changes from "Closed" to some other value, the 2nd select
box must work as normal, allowing user to choose anything
Strictly speaking, we would probably want to do more validation so that:
- closing time is mandatory when start time is selected
- closing time is not earlier than starting time
HTML:
<h3 class="margin-bottom10">Your opening hours</h3>
<table>
<tbody>
<tr class="opening-hours-mon-fri display_none">
<td>Monday</td>
<td>
<select id="postad-monFrom" name="openhrs_mon_from">
<option>Please select</option>
<option value="closed">Closed</option>
<option value="0:00am">0:00am</option>
<option value="0:30am">0:30am</option>
<option value="1:00am">1:00am</option>
</select>
<!--<label for="postad-monTo">to</label>-->to
<select id="postad-monTo"
name="attributeMap[appliance_phone_repair.openhrs_mon_to_s]">
<option></option>
<option value="-">-</option>
<option value="0:00am">0:00am</option>
<option value="0:30am">0:30am</option>
<option value="1:00am">1:00am</option>
</select>
</td>
</tr>
<tr class="opening-hours-mon-fri">
<td>
<label for="postad-tuesFrom">Tuesday</label>
</td>
<td>
<select id="postad-tuesFrom"
name="attributeMap[appliance_phone_repair.openhrs_tues_from_s]">
<option>Select time</option>
<option value="closed">Closed</option>
<option value="0:00am">0:00am</option>
<option value="0:30am">0:30am</option>
<option value="1:00am">1:00am</option>
</select>
<label for="postad-tuesTo">to</label>
<select id="postad-tuesTo"
name="attributeMap[appliance_phone_repair.openhrs_tues_to_s]">
<option></option>
<option value="-">-</option>
<option value="0:00am">0:00am</option>
<option value="0:30am">0:30am</option>
<option value="1:00am">1:00am</option>
</select>
</td>
</tr>
</tbody>
</table>
JAVASCRIPT (uses jQuery 1.6.4)
// TEST SELECTORS BY CHANGING BKGROUND COLOR
//$('[class^="opening-hours-"]').css("background-color", "red");
$('[class^="opening-hours-"]').find('select[id$="From"]').css("background-color",
"yellow");
/*
TIL:
1. You need to use .nextAll() instead of .next()
next() ONLY WORKS IF THE SELECT BOX IS THE NEXT ELEMENT.
IF THERE IS ANYTHING IN BETWEEN, LIKE A <label>, then this won't work.
the earlier version of this only works because "to" was not inside a label
2. When "Closed" is chosen, set the "to" selectbox to "-" and don't
allow any other choices
- set disabled attribute on the options that were not selected
- when value of "From" selectbox is NOT "Closed", then remove disabled
attribute on the "To" options
*/
$('[class^="opening-hours-"]').find('select[id$="From"]').change(function () {
if ($(this).val() == 'closed') {
/*
DOES NOT WORK BECAUSE .next() only gets the
immediately following element.
*/
var $toSelect = $(this).nextAll('select[id$="To"]');
$toSelect.css("background-color", "red");
$toSelect.val('-');
$toSelect.find(':not(:selected)').attr('disabled','disabled');
} else {
$(this).nextAll('select[id$="To"]').find(':not(:selected)').removeAttr('disabled');
$(this).nextAll('select[id$="To"]').css("background-color", "white");
}
});
OSX: Opening a terminal from finder
I've used the shortcut in Windows XP to open a cmd window from
explorer. However, I haven't used it in OSX before. Finally got around
to getting it working. Apparently it's now built in to Lion, but my
imac is still on Snow Leopard.
Google brought me here:
http://stackoverflow.com/a/3487098/204255
and then this:
http://jameslow.com/2010/04/22/openterminalhere-and-anthere/
I downloaded the zip file, opened it, then dragged the file to the top
portion of finder, along with the other icons.
explorer. However, I haven't used it in OSX before. Finally got around
to getting it working. Apparently it's now built in to Lion, but my
imac is still on Snow Leopard.
Google brought me here:
http://stackoverflow.com/a/3487098/204255
and then this:
http://jameslow.com/2010/04/22/openterminalhere-and-anthere/
I downloaded the zip file, opened it, then dragged the file to the top
portion of finder, along with the other icons.
Wednesday, 1 May 2013
HTML5 custom data attributes are case sensitive, and potentially confusing. Just stick to lower-case.
I have a form where the user can enter the hours that a business is
open, and with a pair of select boxes for each day of the week (ie,
"From" and "To" select box with times of the day)
Now I wanted to set this up so that the first time the user sees the
form, it already has default values of 9-5pm for Monday to Friday, and
10-4pm on Saturday and Sunday.
The model class we have has a variable called editMode() that
indicates whether the form is displaying an existing record or a new
record. I planned to use this to toggle the javascript code so it only
sets the values for "opening hours" if the record is not coming from
the database.
I was trying to access some data attributes I had set up as:
<div id="editModeStatus" data-isEditMode="${model.editMode}" ></div>
with jQuery code:
if (!$('#editModeStatus').data('isEditMode')) {
alert('editMode?' + $('#editModeStatus').data('iseditmode'));
And in the alert message, I would get:
editMode? undefined
and changing that code to:
alert('editMode?' + $('#editModeStatus').data('iseditmode'));
gave me
editMode? false
--when it should have been true, because that's the value that was
displaying in the HTML:
<div id="editModeStatus" data-iseditmode="false" ></div>
Googling on "data attribute case sensitive" brought me a couple of
posts discussing the case-sensitivity of html5 data attributes, and
how DATA ATTRIBUTES CAN ONLY BE LOWER-CASE UNLESS THERE IS A HYPHEN
PRECEDING THE UPPER-CASE CHARACTER
Not something I'm likely to remember immediately, so to simplify
things, I'd rather just use lower case in both the HTML and
Javascript.
REF:
http://blog.benpowell.co.uk/2012/03/warning-html5-data-attribute-is-case.html
open, and with a pair of select boxes for each day of the week (ie,
"From" and "To" select box with times of the day)
Now I wanted to set this up so that the first time the user sees the
form, it already has default values of 9-5pm for Monday to Friday, and
10-4pm on Saturday and Sunday.
The model class we have has a variable called editMode() that
indicates whether the form is displaying an existing record or a new
record. I planned to use this to toggle the javascript code so it only
sets the values for "opening hours" if the record is not coming from
the database.
I was trying to access some data attributes I had set up as:
<div id="editModeStatus" data-isEditMode="${model.editMode}" ></div>
with jQuery code:
if (!$('#editModeStatus').data('isEditMode')) {
alert('editMode?' + $('#editModeStatus').data('iseditmode'));
And in the alert message, I would get:
editMode? undefined
and changing that code to:
alert('editMode?' + $('#editModeStatus').data('iseditmode'));
gave me
editMode? false
--when it should have been true, because that's the value that was
displaying in the HTML:
<div id="editModeStatus" data-iseditmode="false" ></div>
Googling on "data attribute case sensitive" brought me a couple of
posts discussing the case-sensitivity of html5 data attributes, and
how DATA ATTRIBUTES CAN ONLY BE LOWER-CASE UNLESS THERE IS A HYPHEN
PRECEDING THE UPPER-CASE CHARACTER
Not something I'm likely to remember immediately, so to simplify
things, I'd rather just use lower case in both the HTML and
Javascript.
REF:
http://blog.benpowell.co.uk/2012/03/warning-html5-data-attribute-is-case.html
Tuesday, 23 April 2013
Java, Spring: creating error messages in Spring - reject() vs rejectValue()
When to use errors.reject() or errors.rejectValue()??
"The difference between rejectValue and reject is that rejectValue
always has to be about a field, hence it is a FieldError. The reject
method generates global errors."
Be careful with your calls because .reject() and rejectValue() both
have two string parameters. If your call is meant to be for
.rejectValue() but call .reject() instead, it will not break but also
won't work!
REF:
http://forum.springsource.org/archive/index.php/t-34649.html
From
org/springframework/spring-context/3.0.5.RELEASE/spring-context-3.0.5.RELEASE-sources.jar!/org/springframework/validation/Errors.java
Errors.java
/**
* Register a field error for the specified field of the current object
* (respecting the current nested path, if any), using the given error
* description.
* <p>The field name may be <code>null</code> or empty String to indicate
* the current object itself rather than a field of it. This may result
* in a corresponding field error within the nested object graph or a
* global error if the current object is the top object.
* @param field the field name (may be <code>null</code> or empty String)
* @param errorCode error code, interpretable as a message key
* @see #getNestedPath()
*/
void rejectValue(String field, String errorCode);
/**
* Register a global error for the entire target object,
* using the given error description.
* @param errorCode error code, interpretable as a message key
*/
void reject(String errorCode);
/**
* Register a global error for the entire target object,
* using the given error description.
* @param errorCode error code, interpretable as a message key
* @param defaultMessage fallback default message
*/
void reject(String errorCode, String defaultMessage);
"The difference between rejectValue and reject is that rejectValue
always has to be about a field, hence it is a FieldError. The reject
method generates global errors."
Be careful with your calls because .reject() and rejectValue() both
have two string parameters. If your call is meant to be for
.rejectValue() but call .reject() instead, it will not break but also
won't work!
REF:
http://forum.springsource.org/archive/index.php/t-34649.html
From
org/springframework/spring-context/3.0.5.RELEASE/spring-context-3.0.5.RELEASE-sources.jar!/org/springframework/validation/Errors.java
Errors.java
/**
* Register a field error for the specified field of the current object
* (respecting the current nested path, if any), using the given error
* description.
* <p>The field name may be <code>null</code> or empty String to indicate
* the current object itself rather than a field of it. This may result
* in a corresponding field error within the nested object graph or a
* global error if the current object is the top object.
* @param field the field name (may be <code>null</code> or empty String)
* @param errorCode error code, interpretable as a message key
* @see #getNestedPath()
*/
void rejectValue(String field, String errorCode);
/**
* Register a global error for the entire target object,
* using the given error description.
* @param errorCode error code, interpretable as a message key
*/
void reject(String errorCode);
/**
* Register a global error for the entire target object,
* using the given error description.
* @param errorCode error code, interpretable as a message key
* @param defaultMessage fallback default message
*/
void reject(String errorCode, String defaultMessage);
Friday, 19 April 2013
Getting size of a map in JSTL (it's different in JSP2.1 and JSP 2.2)
Surprised to discover that this doesn't cause problems in JSP
<c:out value="${priceMap.lenght}"/>
tried fixing it to
${priceMap.size}
then realised it gets converted to getSize() and so changed to
${priceMap.size()}
and then get a JSP exception:
org.apache.jasper.JasperException: /WEB-INF/jsp/postform.jsp(31,22)
The function getSize must be used with a prefix when a default
namespace is not specified
I was wondering why, then realised that this works in JSP 2.2
(released in 2009) but not in JSP 2.1 (released 2006)
In JSP 2.1 you must use:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<c:out value="${fn:length(priceMap)}" />
<c:out value="${priceMap.lenght}"/>
tried fixing it to
${priceMap.size}
then realised it gets converted to getSize() and so changed to
${priceMap.size()}
and then get a JSP exception:
org.apache.jasper.JasperException: /WEB-INF/jsp/postform.jsp(31,22)
The function getSize must be used with a prefix when a default
namespace is not specified
I was wondering why, then realised that this works in JSP 2.2
(released in 2009) but not in JSP 2.1 (released 2006)
In JSP 2.1 you must use:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<c:out value="${fn:length(priceMap)}" />
Tuesday, 16 April 2013
IntelliJ IDEA: More keyboard shortcuts that don't work in Xubuntu 12.04
Just discovered a few more IntelliJ IDEA keyboard shortcuts that don't
work in Xubuntu 12.04 because of conflicts with OS default keyboard
mappings.
To fix these, go into:
Settings -> Settings Manager -> Window Manager -> Keyboard (tab)
Then remove the mappings for these key combinations:
Alt-F8 - Evaluate Expression
Ctrl-F1 to Ctrl-F12 - these are mainly for moving to different
Workspace. See the image for the replacement mapping I set - I
replaced them with Ctrl-Shift-Windows-Alt-F1-F4. I've left mappings
for Workspace 5-12 as empty because I dont' think I'll need more than
4 workspaces.
Alt-Insert - Generate
Alt-Delete - Safe Delete
work in Xubuntu 12.04 because of conflicts with OS default keyboard
mappings.
To fix these, go into:
Settings -> Settings Manager -> Window Manager -> Keyboard (tab)
Then remove the mappings for these key combinations:
Alt-F8 - Evaluate Expression
Ctrl-F1 to Ctrl-F12 - these are mainly for moving to different
Workspace. See the image for the replacement mapping I set - I
replaced them with Ctrl-Shift-Windows-Alt-F1-F4. I've left mappings
for Workspace 5-12 as empty because I dont' think I'll need more than
4 workspaces.
Alt-Insert - Generate
Alt-Delete - Safe Delete
IntelliJ IDEA: On Xubuntu 12.04, Ctrl-F12 (File structure popup) doesn't work. Here's how to fix it.
THE PROBLEM:
I use Ctrl-F12 a *lot* in IntelliJ IDEA, so it was annoying when I
started work on a new desktop with Xubuntu and found that it wasn't
working anymore. The IDEA settings were all there, but it wasn't
getting picked up.
Contacted support and found out the problem is due to conflict with
Xubuntu default keyboard mappings.
Xubuntu maps Ctrl+<function key> to Switch Workspaces.
THE SOLUTION:
Just go into Settings -> Settings Manager -> Window Manager -> Keyboard (tab)
Go down to Action = Workspace 12 and remove the keyboard mapping.
In the unlikely event you *do* need to move between 12 workspaces,
just map that to another combo, or change the IntelliJ mapping to
something similar like Windows + F12.
I use Ctrl-F12 a *lot* in IntelliJ IDEA, so it was annoying when I
started work on a new desktop with Xubuntu and found that it wasn't
working anymore. The IDEA settings were all there, but it wasn't
getting picked up.
Contacted support and found out the problem is due to conflict with
Xubuntu default keyboard mappings.
Xubuntu maps Ctrl+<function key> to Switch Workspaces.
THE SOLUTION:
Just go into Settings -> Settings Manager -> Window Manager -> Keyboard (tab)
Go down to Action = Workspace 12 and remove the keyboard mapping.
In the unlikely event you *do* need to move between 12 workspaces,
just map that to another combo, or change the IntelliJ mapping to
something similar like Windows + F12.
Monday, 15 April 2013
Depending on the Java Matcher method you use, your regex may not get what you want
We had a system that you could configure with regexes to parse
incoming data. One of them was a pattern to look at a user's email,
and was configured like:
@domain\.com
And it wasn't matching the values we had, even though the values were
of the form:
somebody@domain.com
someone.else@domain.com
Then I found that the code we used to find a match was:
pattern.matcher(value).matches();
Now the javadoc for matches() says:
"Attempts to match the entire region against the pattern."
So if you look at the ENTIRE string, then
somebody@domain.com
would never match
If you use Matcher.find(), then it just looks at any substring that
matches the pattern, and so this would succeed.
The fix was to change the regex to
.*@domain\.com
YES, it also means that invalid username values would be matched, but
we have other filters that would check if the entire string was of
valid email format. All we care about is the domain, for this bit of
functionality.
REF: http://stackoverflow.com/questions/4450045/difference-between-matches-and-find-in-java-regex
incoming data. One of them was a pattern to look at a user's email,
and was configured like:
@domain\.com
And it wasn't matching the values we had, even though the values were
of the form:
somebody@domain.com
someone.else@domain.com
Then I found that the code we used to find a match was:
pattern.matcher(value).matches();
Now the javadoc for matches() says:
"Attempts to match the entire region against the pattern."
So if you look at the ENTIRE string, then
somebody@domain.com
would never match
If you use Matcher.find(), then it just looks at any substring that
matches the pattern, and so this would succeed.
The fix was to change the regex to
.*@domain\.com
YES, it also means that invalid username values would be matched, but
we have other filters that would check if the entire string was of
valid email format. All we care about is the domain, for this bit of
functionality.
REF: http://stackoverflow.com/questions/4450045/difference-between-matches-and-find-in-java-regex
Thursday, 11 April 2013
Unix: how to display line numbers in less command
run "less"
then type ":"
then type "-N"
enter RETURN
then type ":"
then type "-N"
enter RETURN
Unix: Count the number of delimiters for each line in a huge file
Found this here:
http://ubuntuforums.org/showthread.php?t=301479
*THIS* is the one to use:
awk 'BEGIN { FS = ";" } ; { print NF-1 }' <million_lines.csv>
note that this outputs to console, so pipe to file for easier use
(add this at the end: > output_file)
example:
input file is "addresses.txt" and i want to
awk 'BEGIN { FS = "\t" } ; { print NF-1 }' addresses.txt > tab_count.txt
The other approach using echo |tr|wc is really really slow.
Note to self: learn awk and sed!
http://ubuntuforums.org/showthread.php?t=301479
*THIS* is the one to use:
awk 'BEGIN { FS = ";" } ; { print NF-1 }' <million_lines.csv>
note that this outputs to console, so pipe to file for easier use
(add this at the end: > output_file)
example:
input file is "addresses.txt" and i want to
awk 'BEGIN { FS = "\t" } ; { print NF-1 }' addresses.txt > tab_count.txt
The other approach using echo |tr|wc is really really slow.
Note to self: learn awk and sed!
Java: In a static method, how do we load a file from a jar in classpath
Task: inside a static method, load a file contained in a jar that is
in our classpath.
First approach:
static void loadFromClasspath(String filename)
InputStream in = MyClass.class.getClassLoader().getResourceAsStream(filename);
// test
System.out.println("file found?" + in.available());
in.close()
main()
MyClass.loadFromClasspath("/this/is/a/package/file.txt");
-- was getting NPE on call to in.available
Second approach:
changed one line to
InputStream in = MyClass.class.getResourceAsStream(filename);
WORKS!
Warning:
Trying
MyClass.loadFromClasspath("file.txt");
does *not* work
in our classpath.
First approach:
static void loadFromClasspath(String filename)
InputStream in = MyClass.class.getClassLoader().getResourceAsStream(filename);
// test
System.out.println("file found?" + in.available());
in.close()
main()
MyClass.loadFromClasspath("/this/is/a/package/file.txt");
-- was getting NPE on call to in.available
Second approach:
changed one line to
InputStream in = MyClass.class.getResourceAsStream(filename);
WORKS!
Warning:
Trying
MyClass.loadFromClasspath("file.txt");
does *not* work
Saturday, 23 March 2013
So it's "class war" when it offends the rich, but not when it's against poor people?
http://www.theaustralian.com.au/opinion/columnists/for-labors-sake-julia-gillard-must-drop-the-words-that-divide/story-e6frg75f-1226603796481
So precioussss. Precious miners. They digs for the precious. Please
Mistress Julia, don't hurt them.
Belittling big business is "class war". What about belittling asylum
seekers and those on the dole?
Shanahan accuses Gillard of "gender war", while ignoring non-stop
sexist abuse and hate speech from Liberal allies such as Alan Jones.
Good on ya, disingenuous twat.
So precioussss. Precious miners. They digs for the precious. Please
Mistress Julia, don't hurt them.
Belittling big business is "class war". What about belittling asylum
seekers and those on the dole?
Shanahan accuses Gillard of "gender war", while ignoring non-stop
sexist abuse and hate speech from Liberal allies such as Alan Jones.
Good on ya, disingenuous twat.
Wednesday, 9 January 2013
jQuery code to implement "email this page" functionality
Some pages on our site have a "Print this page" link which opens a popup window where the user can email the content to someone else using a form.
Our implementation uses jQuery to:
1. copy the content from the window that opened the popup, using window.opener.document
2. filter the contents of the copied content
3. put it in the textarea
<script type='text/javascript'>
$(function () {
// get the content based on id of the div -- ie, id="main-content-wrapper"
// call .clone() so that subsequent .remove() calls do not affect the original window.opener.document
var content = $('#main-content-wrapper', window.opener.document).clone();
// remove the elements that we don't want displayed in our form
$('ul.page-tools', content).remove();
$('ul.breadcrumbs', content).remove();
$('p.return', content).remove();
// paste the content into a textarea input field with id="emailContent"
// calling .children() because the content we have has lots of intervening divs
// in your case this may not be needed, so just use content.html()
$("#emailContent").html(content.children().html());
})
</script>
Btw, our implementation uses CKEditor javascript library so the emailContent textarea will display the content as HTML.
Thursday, 29 November 2012
JSTL - map contents can be referenced using (.) dot operator instead of brackets.
I was reviewing some code and it had these map references but they
were of the format:
mapName.THIS_KEY
mapName.ANOTHER_KEY
Then I googled and saw that:
"Finally, the dot operator and the bracket operator are somewhat
interchangeable. For example, ${user["firstName"]} could also be used
to retrieve the firstName property of the user object, just as
${commands.dir} could be used to fetch the value associated with the
"dir" key in the commands map."
REF: http://www.ibm.com/developerworks/java/library/j-jstl0211/index.html
I can see why, because it allows multi-dimensional maps to be
referenced like this:
mapName.ANOTHER_KEY.SOMEKEY_INSIDE_THAT_INSIDE_MAP
and won't go Boom! when the ANOTHER_KEY lookup returns nothing.
I still find it kinda icky. I think the dot expression should really
be used sparingly.
If you're doing lookups only one level deep, then it is so much easier
to understand when you do map lookups the same way you do it in normal
Java code.
were of the format:
mapName.THIS_KEY
mapName.ANOTHER_KEY
Then I googled and saw that:
"Finally, the dot operator and the bracket operator are somewhat
interchangeable. For example, ${user["firstName"]} could also be used
to retrieve the firstName property of the user object, just as
${commands.dir} could be used to fetch the value associated with the
"dir" key in the commands map."
REF: http://www.ibm.com/developerworks/java/library/j-jstl0211/index.html
I can see why, because it allows multi-dimensional maps to be
referenced like this:
mapName.ANOTHER_KEY.SOMEKEY_INSIDE_THAT_INSIDE_MAP
and won't go Boom! when the ANOTHER_KEY lookup returns nothing.
I still find it kinda icky. I think the dot expression should really
be used sparingly.
If you're doing lookups only one level deep, then it is so much easier
to understand when you do map lookups the same way you do it in normal
Java code.
Thursday, 22 November 2012
"Print this page" functionality using jQuery - opening a print-friendly page
Certain pages on my current project have either a 'Print', or both
'Print' and 'Email' icons.
The implementation in the old system works like this:
- user clicks on Print icon, triggering a javascript request
- request includes the portlet Id and content Id as a parameter,
opening a new page
- the new page reads in the parameters and reloads the same contents
as the first page
- user can preview page and then print
I've reimplemented it using only jQuery, to make it as simple and
reusable as possible, and works like this:
- printable elements of the page either have a class or are enclosed
in a div with the class "printable"
- user clicks on Print icon, triggering a Javascript function which:
- loads a new page with print-friendly template, but with no content of its own
- on the new page, upon loading, Javascript runs to extract the
content with class "printable" from the parent window
- this content is then inserted into new page
- user can preview page and then print
The basic code is:
(add this to print page)
<script type='text/javascript'>
function printClick() {
window.open('print_template.html');
}
function getPrintContent (printSelector) {
// .clone() is required otherwise the content seems to be moved from
opening page to new page
var selections = $(printSelector).clone();
return selections;
}
$(function() {
// modify this to whatever we need to select the "Print" link.
$("li.print a").click(printClick);
});
</script>
(and on print page -- extract content from opener)
<script type='text/javascript'>
$(function() {
$('#main-content-wrapper').html( window.opener.getPrintContent('.printemail') );
});
</script>
I am inclined to replace the use of "printable" class and just enclose
all the print elements in one div and just get the contents from that
div. This is mainly due to having all our content managed in a CMS,
and if the producer does not include the "printable" class, then the
element won't get included on the print page.
'Print' and 'Email' icons.
The implementation in the old system works like this:
- user clicks on Print icon, triggering a javascript request
- request includes the portlet Id and content Id as a parameter,
opening a new page
- the new page reads in the parameters and reloads the same contents
as the first page
- user can preview page and then print
I've reimplemented it using only jQuery, to make it as simple and
reusable as possible, and works like this:
- printable elements of the page either have a class or are enclosed
in a div with the class "printable"
- user clicks on Print icon, triggering a Javascript function which:
- loads a new page with print-friendly template, but with no content of its own
- on the new page, upon loading, Javascript runs to extract the
content with class "printable" from the parent window
- this content is then inserted into new page
- user can preview page and then print
The basic code is:
(add this to print page)
<script type='text/javascript'>
function printClick() {
window.open('print_template.html');
}
function getPrintContent (printSelector) {
// .clone() is required otherwise the content seems to be moved from
opening page to new page
var selections = $(printSelector).clone();
return selections;
}
$(function() {
// modify this to whatever we need to select the "Print" link.
$("li.print a").click(printClick);
});
</script>
(and on print page -- extract content from opener)
<script type='text/javascript'>
$(function() {
$('#main-content-wrapper').html( window.opener.getPrintContent('.printemail') );
});
</script>
I am inclined to replace the use of "printable" class and just enclose
all the print elements in one div and just get the contents from that
div. This is mainly due to having all our content managed in a CMS,
and if the producer does not include the "printable" class, then the
element won't get included on the print page.
Friday, 9 November 2012
Could you please introduce yourself with more bullshit per syllable?
Anyone who starts off their comment in this manner: "As a UK based
international entrepreneur trying to bring change and transformation
with new internet architectures, technology and business models for
digital eco-systems..." is a total wanker. #rulesoftheinternet
And when you talk about looking for your "Bit Torrent login details" -
you are also a moron.
http://techcrunch.com/2012/11/07/reserving-judgements-is-a-matter-of-infinite-hope
international entrepreneur trying to bring change and transformation
with new internet architectures, technology and business models for
digital eco-systems..." is a total wanker. #rulesoftheinternet
And when you talk about looking for your "Bit Torrent login details" -
you are also a moron.
http://techcrunch.com/2012/11/07/reserving-judgements-is-a-matter-of-infinite-hope
Wednesday, 31 October 2012
Tabs and Me
Principal Skinner: Answers! Answers! -- "Separate Vocations", Simpsons Season 3
That's how having dozens of tabs open at the same time makes me feel. Kinda.
Yah. Sucks to be me.
Saturday, 13 October 2012
Git-Svn - a noob's journey, Week 1-2
I've wanted to try out Git for quite a while. Maybe it's just me, the
type of person who wants to know every frigging thing about something
before actually going ahead and doing it, but I've been trying to get
my head around the whole DVCS thing, and still not completely happy
with what I knew. So I kept on reading, mostly blog posts because the
official sources were not that readable, frankly. Problem with blog
posts is that obviously they don't cover everything, and seemed to
raise a lot of corner cases that I didn't feel I understood enough.
That and we used Subversion for our source control at work. So I
didn't see the point. Then I found out about GIt-Svn, which seems to
me the best of both worlds, at least in theory. These are just some
observations and maybe tips for people who want to give git-svn a try.
* OMG. I LOVE the stash functionality. The ability to move your
current working changes into temporary storage, without the
"commitment" of making branches is much welcome.
* git svn rebase means more frequent updates to your working copy,
minimising the chance your changes stray too far from what everyone is
doing.
* I think in the long term, I just need to think about branches in a
new way, since 1. branches can be local, so no one sees the
embarrassing things you're doing; and 2. working in branches means you
can get the latest and greatest code from svn synced to master without
having it cause you any worry about conflicts, etc.
* I had the mistaken idea that if you were using git-svn, that you
couldn't merge from branches. This wasn't the complete picture. Turns
out it's merging between SVN branches in your git repository that you
shouldn't do (see:
http://stackoverflow.com/questions/2693771/git-svn-merge-2-svn-branches)
If you just make a local branch, don't create it remotely in SVN, do
your changes there instead of on "master" (equivalent to svn trunk),
then merge changes back to trunk - that's completely okay.
* Somehow, I lost my changes, even after stashing them. If you make a
stash, don't pop them (ie, apply them and remove from the saved
stashes). I was doing an svn rebase, so stashed the working changes
first. Then the rebase got a conflict, which I then tried to merge. My
merge didn't seem to work, and I saw a post of a similar problem that
said to just do "git rebase --skip". Then my stash wasn't around
anymore! This took me a couple of tries to sort out. Also learned
about "git fsck", which probably would have helped me instead of me
having to manually recreate my changes.
* Don't forget that any commits you make are only local. No one will
see them until you do "git svn dcommit". I knew this would happen to
me eventually. I broke the build doing this, after sending out an
email saying "Yeah, I've fixed the build." then going home. I can also
very easily imagine this happening at a great scale if we ever adopt
it at our workplace.
Please keep in mind that the information here may or may not be
entirely correct. Corrections, comments and advise from more
experienced users are welcome.
TODO: update with list of helpful URLs for git-svn
type of person who wants to know every frigging thing about something
before actually going ahead and doing it, but I've been trying to get
my head around the whole DVCS thing, and still not completely happy
with what I knew. So I kept on reading, mostly blog posts because the
official sources were not that readable, frankly. Problem with blog
posts is that obviously they don't cover everything, and seemed to
raise a lot of corner cases that I didn't feel I understood enough.
That and we used Subversion for our source control at work. So I
didn't see the point. Then I found out about GIt-Svn, which seems to
me the best of both worlds, at least in theory. These are just some
observations and maybe tips for people who want to give git-svn a try.
* OMG. I LOVE the stash functionality. The ability to move your
current working changes into temporary storage, without the
"commitment" of making branches is much welcome.
* git svn rebase means more frequent updates to your working copy,
minimising the chance your changes stray too far from what everyone is
doing.
* I think in the long term, I just need to think about branches in a
new way, since 1. branches can be local, so no one sees the
embarrassing things you're doing; and 2. working in branches means you
can get the latest and greatest code from svn synced to master without
having it cause you any worry about conflicts, etc.
* I had the mistaken idea that if you were using git-svn, that you
couldn't merge from branches. This wasn't the complete picture. Turns
out it's merging between SVN branches in your git repository that you
shouldn't do (see:
http://stackoverflow.com/questions/2693771/git-svn-merge-2-svn-branches)
If you just make a local branch, don't create it remotely in SVN, do
your changes there instead of on "master" (equivalent to svn trunk),
then merge changes back to trunk - that's completely okay.
* Somehow, I lost my changes, even after stashing them. If you make a
stash, don't pop them (ie, apply them and remove from the saved
stashes). I was doing an svn rebase, so stashed the working changes
first. Then the rebase got a conflict, which I then tried to merge. My
merge didn't seem to work, and I saw a post of a similar problem that
said to just do "git rebase --skip". Then my stash wasn't around
anymore! This took me a couple of tries to sort out. Also learned
about "git fsck", which probably would have helped me instead of me
having to manually recreate my changes.
* Don't forget that any commits you make are only local. No one will
see them until you do "git svn dcommit". I knew this would happen to
me eventually. I broke the build doing this, after sending out an
email saying "Yeah, I've fixed the build." then going home. I can also
very easily imagine this happening at a great scale if we ever adopt
it at our workplace.
Please keep in mind that the information here may or may not be
entirely correct. Corrections, comments and advise from more
experienced users are welcome.
TODO: update with list of helpful URLs for git-svn
Friday, 12 October 2012
Say my name, say my name
Oh boy, this post about "Taking on an English name" hits the spot. Having been given a slightly non-standard name by my parents, I've had my name mangled quite a number of ways. But I understand why it gets mangled. It's different, it's unusual! I geddit. And a lot of people are *not* used to reading phonetically, unaware of other names because they don't friggin read enough or know about the world, or just plain can't spell even if the letters are in front of them.
But here's the thing: instead of giving up, and getting a new name, why not try TEACHING people how to say your name? Educate them, and teach them to make new sounds with phonemes. Teach them how to make sounds they never thought could be done with letter combinations!
In the end, for me, it's all about respect. Respecting other people's cultures to learn their name. Pay attention to how they say it, because "correct" is always defined by the owner of the name. (even those normal-but-misspelt names like Jazzmyn)
To some it might be trivial, but to me if you can't be arsed getting someone's name right, what more when it comes to other things that actually require a bit more effort?
Your name matters. The more you teach people, the easier it becomes both for that person and other people who have "strange" names. Do it. Do it for little Zbignew and Raghavendra.
But here's the thing: instead of giving up, and getting a new name, why not try TEACHING people how to say your name? Educate them, and teach them to make new sounds with phonemes. Teach them how to make sounds they never thought could be done with letter combinations!
In the end, for me, it's all about respect. Respecting other people's cultures to learn their name. Pay attention to how they say it, because "correct" is always defined by the owner of the name. (even those normal-but-misspelt names like Jazzmyn)
To some it might be trivial, but to me if you can't be arsed getting someone's name right, what more when it comes to other things that actually require a bit more effort?
Your name matters. The more you teach people, the easier it becomes both for that person and other people who have "strange" names. Do it. Do it for little Zbignew and Raghavendra.
Subscribe to:
Posts (Atom)