Permalink
Browse files

Good enough for barebones

  • Loading branch information...
1 parent c791115 commit 0f13f96039095b3d6d888cb1a39fe7cea34cccff Jeff Johnson committed Mar 14, 2013
Showing with 171 additions and 8 deletions.
  1. +69 −0 jquery.caret.1.02.js
  2. +37 −7 jquery.moneyfield.coffee
  3. +10 −1 jquery.moneyfield.html
  4. +55 −0 jquery.moneyfield.js
View
69 jquery.caret.1.02.js
@@ -0,0 +1,69 @@
+/*
+ *
+ * Copyright (c) 2010 C. F., Wong (<a href="http://cloudgen.w0ng.hk">Cloudgen Examplet Store</a>)
+ * Licensed under the MIT License:
+ * http://www.opensource.org/licenses/mit-license.php
+ *
+ */
+ (function($,len,createRange,duplicate){
+ $.fn.caret=function(options,opt2){
+ var start,end,t=this[0],browser=$.browser.msie;
+ if(typeof options==="object" && typeof options.start==="number" && typeof options.end==="number") {
+ start=options.start;
+ end=options.end;
+ } else if(typeof options==="number" && typeof opt2==="number"){
+ start=options;
+ end=opt2;
+ } else if(typeof options==="string"){
+ if((start=t.value.indexOf(options))>-1) end=start+options[len];
+ else start=null;
+ } else if(Object.prototype.toString.call(options)==="[object RegExp]"){
+ var re=options.exec(t.value);
+ if(re != null) {
+ start=re.index;
+ end=start+re[0][len];
+ }
+ }
+ if(typeof start!="undefined"){
+ if(browser){
+ var selRange = this[0].createTextRange();
+ selRange.collapse(true);
+ selRange.moveStart('character', start);
+ selRange.moveEnd('character', end-start);
+ selRange.select();
+ } else {
+ this[0].selectionStart=start;
+ this[0].selectionEnd=end;
+ }
+ this[0].focus();
+ return this
+ } else {
+ if(browser){
+ var selection=document.selection;
+ if (this[0].tagName.toLowerCase() != "textarea") {
+ var val = this.val(),
+ range = selection[createRange]()[duplicate]();
+ range.moveEnd("character", val[len]);
+ var s = (range.text == "" ? val[len]:val.lastIndexOf(range.text));
+ range = selection[createRange]()[duplicate]();
+ range.moveStart("character", -val[len]);
+ var e = range.text[len];
+ } else {
+ var range = selection[createRange](),
+ stored_range = range[duplicate]();
+ stored_range.moveToElementText(this[0]);
+ stored_range.setEndPoint('EndToEnd', range);
+ var s = stored_range.text[len] - range.text[len],
+ e = s + range.text[len]
+ }
+ } else {
+ var s=t.selectionStart,
+ e=t.selectionEnd;
+ }
+ var te=t.value.substring(s,e);
+ return {start:s,end:e,text:te,replace:function(st){
+ return t.value.substring(0,s)+st+t.value.substring(e,t.value[len])
+ }}
+ }
+ }
+})(jQuery,"length","createRange","duplicate");
View
44 jquery.moneyfield.coffee
@@ -1,17 +1,47 @@
$ = jQuery
value_field_selector = "[data-money-field]"
+defaults =
+ prefix: '$'
+
$.fn.extend \
- moneyfield: ->
- $(this).keyup update_value_field
+ moneyfield: (params) ->
+ opt = $.extend(defaults, params)
+
+ this.each ->
+ # Prevent multiple bindings
+ if $(this).data('money-field')
+ return this;
+
+ $(this).data('money-field', opt)
+
+ # Bind to update on keys
+ $(this).keyup update_value_field
+
+ # Set initial value
+ $val = get_value_field.call(this)
+ $(this).val(convert_actual_to_friendly.call($val, opt.prefix))
+ this
get_value_field = ->
$(this).parent().find(value_field_selector)
-convert_input_to_actual = ->
- $(this).val()
+convert_input_to_actual = (prefix)->
+ $(this).val().replace(/\./g,"").replace(prefix, "")
+
+convert_actual_to_friendly = (prefix)->
+ dollars = $(this).val()/100
+
+ "#{prefix}#{dollars.toFixed(2)}"
+
+
+update_value_field = ->
+ cursor_pos = $(this).caret().start
+ prefix = $(this).data('money-field').prefix
+
+ $val = get_value_field.call(this)
+ $val.val(convert_input_to_actual.call(this, prefix))
+ $(this).val(convert_actual_to_friendly.call($val, prefix))
-update_value_field = ->
- $val = get_value_field.apply(this)
- $val.val(convert_input_to_actual.apply(this))
+ $(this).caret(cursor_pos,cursor_pos)
View
11 jquery.moneyfield.html
@@ -4,6 +4,7 @@
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> </script>
<!-- Now this is us right here -->
+ <script src="jquery.caret.1.02.js"> </script>
<script src="jquery.moneyfield.js"> </script>
</head>
@@ -12,14 +13,22 @@
<h1>jquery.moneyfield</h1>
<p>jquery-money-field helps you get paid.</p>
-
+ <h1>Blank Default Value</h1>
<div class="example">
<input id="example1" type="text">
<input type="hidden" name="example" data-money-field>
</div>
+
+ <h1>Existing Value, Custom Prefix</h1>
+ <div class="example">
+ <input id="example2" type="text">
+ <input type="hidden" name="example" data-money-field value="1234">
+ </div>
+
<script>
$(function() {
$('#example1').moneyfield();
+ $('#example2').moneyfield({prefix: 'Cash: '})
});
</script>
View
55 jquery.moneyfield.js
@@ -0,0 +1,55 @@
+// Generated by CoffeeScript 1.3.3
+(function() {
+ var $, convert_actual_to_friendly, convert_input_to_actual, defaults, get_value_field, update_value_field, value_field_selector;
+
+ $ = jQuery;
+
+ value_field_selector = "[data-money-field]";
+
+ defaults = {
+ prefix: '$'
+ };
+
+ $.fn.extend({
+ moneyfield: function(params) {
+ var opt;
+ opt = $.extend(defaults, params);
+ return this.each(function() {
+ var $val;
+ if ($(this).data('money-field')) {
+ return this;
+ }
+ $(this).data('money-field', opt);
+ $(this).keyup(update_value_field);
+ $val = get_value_field.call(this);
+ $(this).val(convert_actual_to_friendly.call($val, opt.prefix));
+ return this;
+ });
+ }
+ });
+
+ get_value_field = function() {
+ return $(this).parent().find(value_field_selector);
+ };
+
+ convert_input_to_actual = function(prefix) {
+ return $(this).val().replace(/\./g, "").replace(prefix, "");
+ };
+
+ convert_actual_to_friendly = function(prefix) {
+ var dollars;
+ dollars = $(this).val() / 100;
+ return "" + prefix + (dollars.toFixed(2));
+ };
+
+ update_value_field = function() {
+ var $val, cursor_pos, prefix;
+ cursor_pos = $(this).caret().start;
+ prefix = $(this).data('money-field').prefix;
+ $val = get_value_field.call(this);
+ $val.val(convert_input_to_actual.call(this, prefix));
+ $(this).val(convert_actual_to_friendly.call($val, prefix));
+ return $(this).caret(cursor_pos, cursor_pos);
+ };
+
+}).call(this);

0 comments on commit 0f13f96

Please sign in to comment.