Permalink
Browse files

initial commit

  • Loading branch information...
1 parent 43d4ef6 commit 0d3f4a746cdeb11117c1a598c2a33e442056425d @janez89 committed Aug 15, 2013
Showing with 468 additions and 0 deletions.
  1. +5 −0 index.js
  2. +148 −0 lib/form.js
  3. +66 −0 lib/html.js
  4. +183 −0 lib/index.js
  5. +66 −0 test/default.test.js
View
@@ -0,0 +1,5 @@
+/*
+ * Created By Janez
+ */
+
+module.exports = require(__dirname + "/lib");
View
@@ -0,0 +1,148 @@
+
+module.exports = function(fn) {
+ var qs = require('querystring'),
+ form = {};
+
+ form.begin = function (target, opts) {
+ return '<form'+ fn.toAttr(fn.mergeObject({ action: target || '', method: 'POST'}, opts)) +'>';
+ };
+
+ form.end = function () {
+ return '</form>';
+ };
+
+ form.label = function (label, opts) {
+ return '<label'+ fn.toAttr(label)+'>'+ label +'</label>';
+ };
+
+ form.inputField = function (opts) {
+ opts = opts || {};
+ opts.type = opts.type || 'text';
+ opts.disabled = opts.disabled ? 'disabled' : null;
+ //if (typeof opts.value !== 'undefined')
+ // opts.value = fn.e(opts.value);
+ return '<input'+ fn.toAttr(opts) +' />';
+ };
+
+ form.hiddenField = function (name, val, opts) {
+ return form.inputField(fn.mergeObject({ type: 'hidden', name: name, value: val }, opts) );
+ };
+
+ form.textField = function (name, val, opts) {
+ return form.inputField(fn.mergeObject({ type: 'text', name: name, value: val }, opts) );
+ };
+
+ form.passwordField = function (name, val, opts) {
+ return form.inputField(fn.mergeObject({ type: 'password', name: name, value: val }, opts) );
+ };
+
+ form.textArea = function (name, val, opts) {
+ opts = opts || {};
+ opts.name = name;
+ return '<textarea'+ fn.toAttr(opts) +'>'+ val +'</textarea>';
+ };
+
+ form.dropDownList = function (name, val, elements, opts) {
+ opts = opts || {},
+ label = null,
+ value = null;
+ // empty box
+ var empty = opts.empty ?
+ '\t<option'+ fn.toAttr({ value: '', selected: !val && typeof val !== 'number' ? 'selected': null }) +'>'+ opts.empty +'</option>\n' : '';
+ delete opts.empty;
+ // detect key, value
+ if (opts.label || opts.value) {
+ label = opts.label || 'label';
+ value = opts.value || 'value';
+ delete opts.label;
+ delete opts.value;
+ }
+
+ var output = '<select'+ fn.toAttr(fn.mergeObject({name: name}, opts)) +'>\n'+ empty;
+
+ if (value) {
+ for (var i in elements) {
+ var el = elements[i];
+ if (el.hasOwnProperty(value) && el.hasOwnProperty(label)) {
+ output += '\t<option'+ fn.toAttr({ value: el[value], selected: el[value] == val ? 'selected' : null }) +'>'+ el[label] +'</option>\n';
+ }
+ }
+ } else if (Array.isArray(elements)) {
+ for (var i in elements) {
+ output += '\t<option'+ fn.toAttr({ value: i, selected: i == val ? 'selected' : null }) +'>'+ elements[i] +'</option>\n';
+ }
+ } else {
+ for (var i in elements) {
+ if (elements.hasOwnProperty(i)) {
+ output += '\t<option'+ fn.toAttr({ value: i, selected: i == val ? 'selected' : null }) +'>'+ elements[i] +'</option>\n';
+ }
+ }
+ }
+
+ return output + '</select>';
+ };
+
+ form.dateSelector = function (name, value, opts) {
+ throw new Error('Not implemented method!');
+ };
+
+ form.timeSelector = function (name, value, opts) {
+ throw new Error('Not implemented method!');
+ };
+
+ form.dateTimeSelector = function (name, value, opts) {
+ throw new Error('Not implemented method!');
+ };
+
+ form.radioButtonList = function (name, value, list, opts) {
+ throw new Error('Not implemented method!');
+ };
+
+ form.checkBox = function (name, checked, opts) {
+ opts = opts || {};
+ opts.type = 'checkbox';
+ opts.name = name;
+ opts.checked = checked ? 'checked': null;
+
+ return form.inputField(opts);
+ };
+
+ form.radioButton = function (name, selected, opts) {
+ opts = opts || {};
+ opts.type = 'radio';
+ opts.name = name;
+ opts.checked = checked ? 'checked': null;
+
+ return form.inputField(opts);
+ };
+
+ form.fileField = function (name, opts) {
+ opts = opts || {};
+ opts.type = 'file';
+ opts.name = name;
+
+ return form.inputField(opts);
+ };
+
+ form.button = function (label, opts) {
+ return '<button'+ fn.toAttr(fn.mergeObject({ value: label}, opts))+' />';
+ };
+
+ form.resetButton = function (label, opts) {
+ opts = opts || {};
+ if (label)
+ opts.value = label;
+ opts.type = 'reset';
+ return form.inputField(opts);
+ };
+
+ form.submitButton = function (label, opts) {
+ opts = opts || {};
+ if (label)
+ opts.value = label;
+ opts.type = 'submit';
+ return form.inputField(opts);
+ };
+
+ return form;
+};
View
@@ -0,0 +1,66 @@
+
+module.exports = function(fn) {
+ var qs = require('querystring'),
+ html = {};
+
+ html.el = function (name, opts) {
+ return html.beginEl(name, opts) + html.endEl(name);
+ };
+
+ html.beginEl = function (name, opts) {
+ opts = opts || {};
+
+ var innerHTML = opts && typeof opts.val !== 'undefined' ? fn.e(opts.val) : ''; // escaped value
+ delete opts.val;
+ if (opts && typeof opts.html !== 'undefined') {
+ innerHTML = opts.html;
+ delete opts.html;
+ }
+ return '<'+ name + fn.toAttr(opts) +'>'+ innerHTML;
+ };
+
+ html.endEl = function (name) {
+ return '</'+ name +'>';
+ };
+
+ // meta charset
+ html.charset = function (charset) {
+ charset = charset || 'utf-8';
+ return '<meta charset="'+ charset +'">';
+ };
+
+ html.css = function (href, opts) {
+ return '<link'+ fn.toAttr(fn.mergeObject({ href: href, rel: 'stylesheet', type: 'text/css', media: 'all'}, opts)) +' />';
+ };
+
+ html.script = function (src, opts) {
+ return '<script'+ fn.toAttr(fn.mergeObject({ src: src, type: 'text/javascript'}, opts)) +'></script>';
+ };
+
+ html.a = function(link, text, opts) {
+ return '<a'+ fn.toAttr(fn.mergeObject({ href: link }, opts)) +'>'+ text +'</a>';
+ };
+
+ html.img = function (src, alt, opts) {
+ return '<img'+ fn.toAttr(fn.mergeObject({ src: src, alt: alt }, opts)) +' />';
+ };
+
+ html.imgText = function(text, opt) {
+ opt = opt || {};
+ var fgColor = opt.fgColor || 'EFEFEF';
+ var bgColor = opt.bgColor || 'AAAAAA';
+ var w = opt.w || 291;
+ var h = opt.h || 170;
+ text = text || 'nincs kép';
+ // http://www.placehold.it/291x170/EFEFEF/AAAAAA&amp;text=nincs+kép
+ return '<img src="http://www.placehold.it/'+w+'x'+h+'/'+fgColor+'/'+bgColor+'&amp;text='+qs.escape(text)+'" />';
+ };
+
+ html.refresh = function (url, sec, opts) {
+ if (!sec)
+ sec = 5;
+ return '<meta'+ fn.toAttr(fn.mergeObject({ http: { equiv: 'refresh' }, content: sec +',URL='+ url }, opts)) +' />';
+ };
+
+ return html;
+};
Oops, something went wrong.

0 comments on commit 0d3f4a7

Please sign in to comment.