From d2f988d545530f769c25d657b576f1e9938c1729 Mon Sep 17 00:00:00 2001 From: vitalets Date: Sun, 25 Nov 2012 12:12:26 +0400 Subject: [PATCH] list input + checklist alpha --- CHANGELOG.txt | 6 +- src/editable-form/editable-form.js | 15 +- src/inputs/checklist.js | 150 +++++++++++++++++ src/inputs/date/date.js | 1 + src/inputs/dateui/dateui.js | 1 + src/inputs/list.js | 255 +++++++++++++++++++++++++++++ src/inputs/select.js | 236 ++------------------------ src/inputs/text.js | 1 + src/inputs/textarea.js | 1 + test/loader.js | 4 +- 10 files changed, 444 insertions(+), 226 deletions(-) create mode 100644 src/inputs/checklist.js create mode 100644 src/inputs/list.js diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 90c417d..da494b4 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,6 +1,10 @@ X-editable changelog ============================= - + +Version 1.0.2 wip +---------------------------- +[enh] updated docs: inputs dropdown menu, global templates section (vitalets) + Version 1.0.1 Nov 22, 2012 ---------------------------- diff --git a/src/editable-form/editable-form.js b/src/editable-form/editable-form.js index 8a975b0..d93042a 100644 --- a/src/editable-form/editable-form.js +++ b/src/editable-form/editable-form.js @@ -17,7 +17,7 @@ Editableform is linked with one of input types, e.g. 'text' or 'select'. EditableForm.prototype = { constructor: EditableForm, - initInput: function() { + initInput: function() { //called once var TypeConstructor, typeOptions; //create input of specified type @@ -247,7 +247,18 @@ Editableform is linked with one of input types, e.g. 'text' or 'select'. option: function(key, value) { this.options[key] = value; - } + if(key === 'value') { + this.setValue(value); + } + }, + + setValue: function(value, convertStr) { + if(convertStr) { + this.value = this.input.str2value(value); + } else { + this.value = value; + } + } }; /* diff --git a/src/inputs/checklist.js b/src/inputs/checklist.js new file mode 100644 index 0000000..158d509 --- /dev/null +++ b/src/inputs/checklist.js @@ -0,0 +1,150 @@ +/** +Checklist input. Internally value stored as array. + +@class checklist +@extends list +@final +@example + + +**/ +(function ($) { + + var Checklist = function (options) { + this.init('checklist', options, Checklist.defaults); + }; + + $.fn.editableform.utils.inherit(Checklist, $.fn.editableform.types.list); + + $.extend(Checklist.prototype, { + renderList: function() { + var $label, $div; + if(!$.isArray(this.sourceData)) { + return; + } + + for(var i=0; i').text(' '+this.sourceData[i].text) + .prepend($('', { + type: 'checkbox', + value: this.sourceData[i].value, + name: this.options.name + })); + + $('
').append($label).appendTo(this.$input); + } + }, + + value2str: function(value) { + return $.isArray(value) ? value.join($.trim(this.options.separator)) : ''; + }, + + //parse separated string + str2value: function(str) { + var reg, value = null; + if(typeof str === 'string' && str.length) { + reg = new RegExp('\s*'+$.trim(this.options.separator)+'\s*'); + value = str.split(reg); + } else if($.isArray(str)) { + value = str; + } + return value; + }, + + //set checked on required checkboxes + value2input: function(value) { + var $checks = this.$input.find('input[type="checkbox"]'); + $checks.removeAttr('checked'); + if($.isArray(value) && value.length) { + $checks.each(function(i, el) { + if($.inArray($(el).val(), value) !== -1) { + $(el).attr('checked', 'checked'); + } + }); + } + }, + + input2value: function() { + var checked = []; + this.$input.find('input:checked').each(function(i, el) { + checked.push($(el).val()); + }); + return checked; + }, + + //collect text of checked boxes + value2htmlFinal: function(value, element) { + var selected = [], html = ''; + if($.isArray(value) && value.length <= this.options.limit) { + for(var i=0; i').text(item.text).html()); + } + } + html = selected.join(this.options.viewseparator); + } else { + html = this.options.limitText.replace('{checked}', $.isArray(value) ? value.length : 0).replace('{count}', this.sourceData.length); + } + $(element).html(html); + } + }); + + Checklist.defaults = $.extend({}, $.fn.editableform.types.list.defaults, { + /** + @property tpl + @default
+ **/ + tpl:'
', + + /** + Separator of values in string when sending to server + + @property separator + @type string + @default ', ' + **/ + separator: ',', + /** + Separator of text when display as element content. + + @property viewseparator + @type string + @default ', ' + **/ + viewseparator: '
', + /** + Maximum number of items shown as element content. + If checked more items - limitText will be shown. + + @property limit + @type integer + @default 4 + **/ + limit: 4, + /** + Text shown when count of checked items is greater than limit parameter. + You can use {checked} and count placeholders. + + @property limitText + @type string + @default 'Checked {checked} options of {count}' + **/ + limitText: 'Checked {checked} options of {count}' + }); + + $.fn.editableform.types.checklist = Checklist; + +}(window.jQuery)); \ No newline at end of file diff --git a/src/inputs/date/date.js b/src/inputs/date/date.js index 38680e8..5081db2 100644 --- a/src/inputs/date/date.js +++ b/src/inputs/date/date.js @@ -5,6 +5,7 @@ For localization you can include js file from here: https://github.com/eternicod @class date @extends abstract +@final @example 15/05/1984