1 // !code vendor/crayon/lib/crayon.js
  2 // !code vendor/crayon/lib/tag.js
  3 /**
  4 * @class
  5 * @namespace Holds functionality for escaping charactors.
  6 * @requires crayon/lib/core.js
  7 * @requires crayon/lib/escape.js
  8 */
  9 Crayon.Form = {
 10    /**
 11     * Returns select tags for date selection tailored for accessing a specified attribute.
 12     * @param {Object} doc JSON document to bind.
 13     * @param {String} path JSON member path seperated by "-".
 14     * @param [Object] options tag attribute options.
 15     */
 16    date_select : function(doc, path, options, html_options){
 17       var val = Crayon.Form._getValueFromPath(doc, path);
 18       if( val && (!Crayon.isDate(val))) {
 19          val = new Date(val.toString());
 20          if( val == "Invalid Date"){
 21             val = null;
 22          }
 23       }
 24       var html_opt = Crayon.extend({
 25          name: path
 26       }, options);
 27       return select_date(val, options, html_opt);
 28    },
 29 
 30    /**
 31     * Returns an input tag of the "checkbox" type tailored for accessing a specified attribute.
 32     * @param {Object} doc JSON document to bind.
 33     * @param {String} path JSON member path seperated by "-".
 34     * @param {String} tag_value radio button value.
 35     * @param [Object] options tag attribute options.
 36     */
 37    check_box : function(doc, path, options, checked_value, unchecked_value){
 38       var val = Crayon.Form._getValueFromPath(doc, path);
 39       checked_value   = checked_value == undefined ? true : checked_value;
 40       unchecked_value = unchecked_value == undefined ? false : checked_value;
 41       // unchecked value field (hidden)
 42       var html_opt = Crayon.extend({
 43          name: path
 44       }, options);
 45       html_opt["type"] = "hidden";
 46       html_opt["value"] = toJSON(unchecked_value);
 47       var unchecked_tag = tag("input", html_opt);
 48       // checked value field (checkbox)
 49       html_opt = Crayon.extend({
 50          id : path,
 51          name: path
 52       }, options);
 53       html_opt["type"]  = "checkbox";
 54       html_opt["value"] = toJSON(checked_value);
 55       if( val == checked_value ){
 56          html_opt["checked"] = "checked";
 57       }
 58       var checked_tag = tag("input", html_opt);
 59       return checked_tag + "\n" + unchecked_tag;
 60    },
 61 
 62    /**
 63     * Returns an input tag of the "radio" type tailored for accessing a specified attribute.
 64     * @param {Object} doc JSON document to bind.
 65     * @param {String} path JSON member path seperated by "-".
 66     * @param {String} tag_value radio button value.
 67     * @param [Object] options tag attribute options.
 68     */
 69    radio_button : function(doc, path, tag_value, options){
 70       var val = Crayon.Form._getValueFromPath(doc, path);
 71       // html options
 72       var html_opt = Crayon.extend({
 73          id : path,
 74          name: path
 75       }, options);
 76       html_opt["type"]  = "radio";
 77       html_opt["value"] = toJSON(tag_value);
 78       if( val == tag_value ){
 79          html_opt["checked"] = "checked";
 80       }
 81       return tag("input", html_opt);
 82    },
 83 
 84    /**
 85     * Returns an input tag of the "hidden" type tailored for accessing a specified attribute.
 86     * @param {Object} doc JSON document to bind.
 87     * @param {String} path JSON member path seperated by "-".
 88     * @param [Object] options tag attribute options.
 89     */
 90    hidden_field : function(doc, path, options){
 91       return Crayon.Form._toInputFieldTag("hidden", doc, path, options);
 92    },
 93 
 94    /**
 95     * Returns an input tag of the "password" type tailored for accessing a specified attribute.
 96     * @param {Object} doc JSON document to bind.
 97     * @param {String} path JSON member path seperated by "-".
 98     * @param [Object] options tag attribute options.
 99     */
100    password_field : function(doc, path, options){
101       return Crayon.Form._toInputFieldTag("password", doc, path, options);
102    },
103 
104    /**
105     * Returns an input tag of the "text" type tailored for accessing a specified attribute.
106     * @param {Object} doc JSON document to bind.
107     * @param {String} path JSON member path seperated by "-".
108     * @param [Object] options tag attribute options.
109     */
110    text_field : function(doc, path, options){
111       return Crayon.Form._toInputFieldTag("text", doc, path, options);
112    },
113 
114    /**
115     * Returns an textarea tag tailored for accessing a specified attribute.
116     * @param {Object} doc JSON document to bind.
117     * @param {String} path JSON member path seperated by "-".
118     * @param [Object] options tag attribute options.
119     */
120    text_area : function(doc, path, options){
121       var val = Crayon.Form._getValueFromPath(doc, path);
122       // html options
123       var html_opt = Crayon.extend({
124          id : path,
125          name: path
126       }, options);
127       return content_tag("textarea", val || "", html_opt);
128    },
129 
130    /**
131     * Returns a set of select tags for date selection.
132     * @param {String} date the default selected value.
133     * @param [Object] option kev-value pairs for the date list.
134     * @param [Object] html_option kev-value pairs passed to each select tag.
135     */
136    select_date : function(date, option, html_option){
137       var opt      = Crayon.extend({
138          "start_year" : 1900,
139          "end_year"   : 2100,
140          "date_separator" : "/",
141          "include_blank" : false
142       }, option);
143       var html_opt = Crayon.extend({
144          "name" : "date"
145       }, html_option);
146       var html = "";
147       var y,m,d;
148       if( date ){
149          y = date.getFullYear();
150          m = date.getMonth()+1;
151          d = date.getDate();
152       }
153 
154       // year
155       html += content_tag("select", function(){
156          var str = "";
157          return Crayon.Form._generateOptions(opt.start_year, opt.end_year, y, opt.include_blank);
158       }, Crayon.extend({}, html_opt, {
159          name: html_opt["name"] + "-year"
160       }));
161       html += "\n" + opt.date_separator + "\n";
162       // month
163       html += content_tag("select",  function(){
164          var str = "";
165          return Crayon.Form._generateOptions(1, 12, m, opt.include_blank);
166       }, Crayon.extend({}, html_opt, {
167          name: html_opt["name"] + "-month"
168       }));
169       html += "\n" + opt.date_separator + "\n";
170       // day
171       html += content_tag("select",  function(){
172          var str = "";
173          return Crayon.Form._generateOptions(1, 31, d, opt.include_blank);
174       }, Crayon.extend({}, html_opt, {
175          name: html_opt["name"] + "-day"
176       }));
177       return html;
178    },
179 
180    _toInputFieldTag : function(type, doc, path, options){
181       var val = Crayon.Form._getValueFromPath(doc, path);
182       // html options
183       var html_opt = Crayon.extend({
184          id : path,
185          name: path
186       }, options);
187       html_opt["type"]  = type;
188       html_opt["value"] = val || "";
189       return tag("input",html_opt);
190    },
191 
192    _getValueFromPath: function(doc, path){
193       var val;
194       // value mapping
195       if( doc ){
196          var paths = path.split("-"); // compatible path separator for CouchApp's docForm function
197          var obj = doc;
198          for(var i in paths){
199             obj = obj[paths[i]];
200             if( obj == undefined || obj == null ){
201                break;
202             }
203          }
204          val = obj;
205       }
206       return val;
207    },
208 
209    _generateOptions : function(s,t, selected, include_blank){
210       var candidates = "";
211       if(include_blank){
212          if( selected ){
213             candidates += '<option value=""></option>';
214          }else{
215             candidates += '<option value="" selected="selected"></option>';
216          }
217       }
218       for(var i=s; i<=t; i++){
219          if( selected == i ){
220             candidates += '<option value="' + i + '" selected="selected">' + i + '</option>';
221          }else{
222             candidates += '<option value="' + i + '">' + i + '</option>';
223          }
224       }
225       return candidates;
226    }
227 };
228 
229 if( !this.do_not_import_global ){
230   Crayon.extend(this, Crayon.Form);
231 }else{
232   Crayon.extend(Crayon.Form);
233 }
234