tweaking and minor refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-01-21 05:21:41 +03:00
parent 2c628341a6
commit d13b39609a
2 changed files with 44 additions and 38 deletions

View File

@ -399,22 +399,23 @@ if(typeof(jQuery) != typeof(undefined)){
// } // }
// //
// This listens to these events triggerable by user: // This listens to these events triggerable by user:
// 'commit' - will commit changes and fire 'edit-done' with // 'edit-commit' - will commit changes, this is passed the
// field text. // new text just edited.
// 'abort' - will reset field and trigger 'edit-aborted' // 'edit-abort' - will reset field, this is passed the
// with original (before edit started) field text // original text before the edit.
// //
// //
// NOTE: removing tabindex will reset focus, so this will attempt to // NOTE: removing tabindex will reset focus, so this will attempt to
// focus the first [tabindex] element up the tree... // focus the first [tabindex] element up the tree...
// //
// XXX add option to select the element on start or just focus it...
// XXX multiline fields need to retain original function of arrows
// until we are at last line, then pass it through...
// XXX should we just use form elements??? // XXX should we just use form elements???
// ...it's a trade-off, here we add editing functionality and fight // ...it's a trade-off, here we add editing functionality and fight
// a bit the original function, in an input we'll need to fight part // a bit the original function, in an input we'll need to fight part
// of the editing functionality and add our own navigation... // of the editing functionality and add our own navigation...
// XXX move this to a more generic spot... // XXX move this to a more generic spot...
// XXX should this reset field to it's original state after
// commit/abort???
jQuery.fn.makeEditable = function(options){ jQuery.fn.makeEditable = function(options){
var that = this var that = this
@ -465,14 +466,14 @@ if(typeof(jQuery) != typeof(undefined)){
// abort... // abort...
if((options.abort_keys || ['Esc']).indexOf(n) >= 0){ if((options.abort_keys || ['Esc']).indexOf(n) >= 0){
that.trigger('abort') that.trigger('edit-abort', original)
// done -- single line... // done -- single line...
} else if(n == 'Enter' } else if(n == 'Enter'
&& !options.multiline){ && !options.multiline){
event.preventDefault() event.preventDefault()
that.trigger('commit') that.trigger('edit-commit', that.text())
// done -- multiline... // done -- multiline...
} else if(n == 'Enter' } else if(n == 'Enter'
@ -480,7 +481,7 @@ if(typeof(jQuery) != typeof(undefined)){
&& options.multiline){ && options.multiline){
event.preventDefault() event.preventDefault()
that.trigger('commit') that.trigger('edit-commit', that.text())
// continue handling... // continue handling...
} else { } else {
@ -498,9 +499,7 @@ if(typeof(jQuery) != typeof(undefined)){
.selectText() .selectText()
}) })
// user triggerable events... // user triggerable events...
.on('abort', events.abort = function(){ .on('edit-abort', events['edit-abort'] = function(){
that.trigger('edit-aborted', original)
options.clear_selection_on_abort !== false options.clear_selection_on_abort !== false
&& window.getSelection().removeAllRanges() && window.getSelection().removeAllRanges()
@ -517,9 +516,7 @@ if(typeof(jQuery) != typeof(undefined)){
that.makeEditable(false) that.makeEditable(false)
}) })
.on('commit', events.commit = function(){ .on('edit-commit', events['edit-commit'] = function(){
that.trigger('edit-done', that.text())
options.clear_selection_on_commit !== false options.clear_selection_on_commit !== false
&& window.getSelection().removeAllRanges() && window.getSelection().removeAllRanges()

View File

@ -128,7 +128,7 @@ function(text, options){
// //
// options format: // options format:
// { // {
// select_text: <number> | <string> | 'first' | 'last', // select_text: <number> | 'first' | 'last' | <selector>,
// //
// ... // ...
// } // }
@ -139,22 +139,21 @@ function(text, options){
var elem = (options.action ? this.Action : this).call(this, text, options) var elem = (options.action ? this.Action : this).call(this, text, options)
.on('select', function(){ .on('select', function(){
var text = elem.find('.text') var text = elem.find('.text')
// get the specific .text element...
if(options.select_text == 'first' || options.select_text == 'last'){ // select index...
text[options.selected_index]() typeof(options.select_text) == typeof(123) ?
text.eq(options.select_text)
.selectText() .selectText()
// first/last
} else if(typeof(options.select_text) == typeof('str')){ : (options.select_text == 'first' || options.select_text == 'last') ?
elem.find(options.selected_index) text[options.select_text]()
.selectText() .selectText()
// selector...
} else if(typeof(options.select_text) == typeof(123)){ : typeof(options.select_text) == typeof('str') ?
text.eq(options.selected_index) elem.find(options.select_text)
.selectText() .selectText()
// all...
} else { : text.selectText()
text.selectText()
}
}) })
return elem return elem
} }
@ -176,7 +175,8 @@ function(text, options){
// // NOTE: by default this will select all the elements, if there // // NOTE: by default this will select all the elements, if there
// // are more than one, this may result in an odd element // // are more than one, this may result in an odd element
// // state... // // state...
// editable_index: <number>, // // NOTE: the selector is used to filter text elements...
// edit_text: <number> | 'first' | 'last' | <selector>,
// //
// // item event to start the edit on... // // item event to start the edit on...
// start_on: 'select', // start_on: 'select',
@ -198,7 +198,7 @@ function(text, options){
// ... // ...
// } // }
// //
// XXX add option to select the element on start... // XXX add option to select the element on start or just focus it...
Items.Editable = Items.Editable =
function(text, options){ function(text, options){
options = options || {} options = options || {}
@ -211,8 +211,17 @@ function(text, options){
editable = elem.find('.text') editable = elem.find('.text')
// get the specific .text element... // get the specific .text element...
editable = options.editable_index != null ? editable =
editable.eq(options.editable_index) // index...
typeof(options.edit_text) == typeof(123) ?
editable.eq(options.edit_text)
// first/last...
: (options.edit_text == 'first' || options.edit_text == 'last') ?
editable[options.edit_text]()
// selecter...
: typeof(options.edit_text) == typeof('str') ?
editable.filter(options.edit_text)
// all...
: editable : editable
// edit the element... // edit the element...
@ -227,19 +236,19 @@ function(text, options){
// deselect on abort -- if we started with a select... // deselect on abort -- if we started with a select...
start_on == 'select' && editable start_on == 'select' && editable
.on('edit-aborted', function(){ .on('edit-abort', function(){
dialog.select(null) dialog.select(null)
}) })
// edit event handlers... // edit event handlers...
options.editaborted options.editaborted
&& editable.on('edit-aborted', options.editaborted) && editable.on('edit-abort', options.editaborted)
options.editdone options.editdone
&& editable.on('edit-done', options.editdone) && editable.on('edit-commit', options.editdone)
}) })
.on('deselect', function(){ .on('deselect', function(){
editable.trigger( editable.trigger(
options.abort_on_deselect !== false ? 'abort' : 'commit') options.abort_on_deselect !== false ? 'edit-abort' : 'edit-commit')
}) })
return elem return elem
@ -400,7 +409,7 @@ function(list, options){
clear_on_edit: true, clear_on_edit: true,
}) })
// update list on edit done... // update list on edit done...
.on('edit-done', function(evt, text){ .on('edit-commit', function(evt, text){
var txt = $(this).text() var txt = $(this).text()
txt = options.normalize ? txt = options.normalize ?