refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-01-15 05:02:38 +03:00
parent ff5d1c4655
commit d5ef01b144
3 changed files with 194 additions and 197 deletions

View File

@ -1048,12 +1048,6 @@ var KeyboardActions = actions.Actions({
return dialog
})],
// XXX key editor:
// [ mode ]
// [ action (text with dataset) ] [ args (text field) ] no default: [_]
// ---
// <list of keys>
// new key
// XXX need a way to abort edits...
editKeyBinding: ['- Interface/Key mapping...',
widgets.makeUIDialog(function(mode, code){
@ -1071,7 +1065,7 @@ var KeyboardActions = actions.Actions({
// list the keys...
var keys = that.keyboard.keys(code)
keys = mode in keys ?
keys[mode][code]
(keys[mode][code] || [])
: []
var to_remove = []
@ -1083,7 +1077,7 @@ var KeyboardActions = actions.Actions({
], })
})
var new_button = make.Action('New key')
var new_button = make.Action('New key...')
.on('open', function(){
widgets.editItem(dialog, new_button)
})
@ -1142,7 +1136,7 @@ var KeyboardActions = actions.Actions({
var that = this
// XXX need a way to set a special '*' key...
var dialog = widgets.makeListEditor(function(keys){
var dialog = browse.makeListEditor(function(keys){
// get...
if(keys === undefined){
return that.keybindings[mode].drop || []

View File

@ -166,193 +166,6 @@ function(list, elem, callback, options){
}
//
// Options format:
// {
// new_button: <text>|<bool>,
//
// length_limit: <number>,
//
// // check input value...
// check: function(value){ ... },
//
// // if true only unique values will be stored...
// // if a function this will be used to normalize the values before
// // uniqueness check is performed...
// unique: <bool>|function(value){ ... },
//
// // if true sort values...
// // if function will be used as cmp for sorting...
// sort: <bool> || function(a, b){ ... },
//
// // this is called when a new value is added via new_button but
// // list length limit is reached...
// callback: function(selected){ ... },
//
// // see: itemButtons doc in browse.js for more info...
// itemButtons: [..]
// }
//
// XXX add sort buttons: up/down/top/bottom...
// XXX currently using this also requires the use of makeUIDialog(..),
// can this be simpler???
// XXX this is generic, move to browse...
var makeListEditor =
module.makeListEditor =
function(list, options){
options = options || {}
var new_button = options.new_button || true
new_button = new_button === true ? 'New...' : new_button
var _makeEditable = function(elem){
return $(elem).find('.text')
.makeEditable({
activate: true,
clear_on_edit: true,
blur_on_abort: false,
blur_on_commit: false,
})
.on('edit-aborted', function(){
dialog.select(null)
dialog.update()
})
.on('edit-done', function(evt, text){
var txt = $(this).text()
// invalid format...
if(options.check && !options.check(txt)){
dialog.update()
return
}
// list length limit
if(options.length_limit
&& (lst.length >= options.length_limit)){
options.callback && options.callback.call(dialog, txt)
return
}
// prevent editing non-arrays...
if(!editable || !lst){
return
}
// add new value and sort list...
lst.push(txt)
// unique...
if(options.unique == null || options.unique === true){
lst = lst.unique()
// unique normalized...
} else if(typeof(options.unique) == typeof(function(){})){
lst = lst.unique(options.unique)
}
// sort...
if(options.sort){
lst = lst
.sort(typeof(options.sort) == typeof(function(){}) ?
options.sort
: undefined)
}
_write(lst)
// update the list data...
dialog.options.data = lst.concat(new_button ? [ new_button ] : [])
// update list and select new value...
dialog.update()
.done(function(){
dialog.select('"'+txt+'"')
})
})
}
var _write = function(lst){
// write back the list...
return list instanceof Function ?
// call the writer...
list(lst)
// in-place replace list elements...
// NOTE: this is necessary as not everything we do with lst
// is in-place...
: list.splice.apply(list, [0, list.length].concat(lst))
}
var to_remove = []
var lst = list instanceof Function ?
list()
: list
var editable = lst instanceof Array
// view objects...
lst = !editable ? Object.keys(lst) : lst.slice()
var dialog = browse.makeList(null,
lst.concat(new_button ? [ new_button ] : []),
{
path: options.path,
itemButtons: options.itemButtons || [
// mark for removal...
browse.buttons.markForRemoval(to_remove)
// XXX add shift up/down/top/bottom and other buttons (optional)...
]
})
// select the new_button item...
.on('select', function(evt, elem){
if(new_button && $(elem).find('.text').text() == new_button){
_makeEditable(elem)
}
})
// restore striked-out items...
.on('update', function(){
to_remove.forEach(function(e){
dialog.filter('"'+ e +'"')
.toggleClass('strike-out')
})
})
.open(function(evt, path){
// we clicked the 'New' button -- select it...
if(new_button && (path == new_button || path == '')){
dialog.select(new_button)
} else {
options.callback && options.callback.call(dialog, path)
}
})
.on('close', function(){
// prevent editing non-arrays...
if(!editable){
return
}
// remove striked items...
to_remove.forEach(function(e){
lst.splice(lst.indexOf(e), 1)
_write(lst)
})
// sort...
if(options.sort){
lst.sort(options.sort !== true ? options.sort : undefined)
_write(lst)
}
})
// XXX
new_button && dialog.dom.addClass('tail-action')
return dialog
}
//---------------------------------------------------------------------
@ -362,7 +175,7 @@ function(actions, path, options){
path = path.split('.')
var key = path.pop()
return makeListEditor(function(lst){
return browse.makeListEditor(function(lst){
var target = actions.config
path.forEach(function(p){
target = target[p] = target[p] || {}

View File

@ -2944,5 +2944,195 @@ module.makePathList = makeBrowserMaker(PathList)
/*********************************************************************/
// Make an list/Array editor...
//
// Options format:
// {
// new_button: <text>|<bool>,
//
// length_limit: <number>,
//
// // check input value...
// check: function(value){ ... },
//
// // if true only unique values will be stored...
// // if a function this will be used to normalize the values before
// // uniqueness check is performed...
// unique: <bool>|function(value){ ... },
//
// // if true sort values...
// // if function will be used as cmp for sorting...
// sort: <bool> || function(a, b){ ... },
//
// // this is called when a new value is added via new_button but
// // list length limit is reached...
// callback: function(selected){ ... },
//
// // see: itemButtons doc in browse.js for more info...
// itemButtons: [..]
// }
//
// XXX add sort buttons: up/down/top/bottom...
// XXX this is generic, move to browse...
var makeListEditor =
module.makeListEditor =
function(list, options){
options = options || {}
var new_button = options.new_button || true
new_button = new_button === true ? 'New...' : new_button
var _makeEditable = function(elem){
return $(elem).find('.text')
.makeEditable({
activate: true,
clear_on_edit: true,
blur_on_abort: false,
blur_on_commit: false,
})
.on('edit-aborted', function(){
dialog.select(null)
dialog.update()
})
.on('edit-done', function(evt, text){
var txt = $(this).text()
// invalid format...
if(options.check && !options.check(txt)){
dialog.update()
return
}
// list length limit
if(options.length_limit
&& (lst.length >= options.length_limit)){
options.callback && options.callback.call(dialog, txt)
return
}
// prevent editing non-arrays...
if(!editable || !lst){
return
}
// add new value and sort list...
lst.push(txt)
// unique...
if(options.unique == null || options.unique === true){
lst = lst.unique()
// unique normalized...
} else if(typeof(options.unique) == typeof(function(){})){
lst = lst.unique(options.unique)
}
// sort...
if(options.sort){
lst = lst
.sort(typeof(options.sort) == typeof(function(){}) ?
options.sort
: undefined)
}
_write(lst)
// update the list data...
dialog.options.data = lst.concat(new_button ? [ new_button ] : [])
// update list and select new value...
dialog.update()
.done(function(){
dialog.select('"'+txt+'"')
})
})
}
var _write = function(lst){
// write back the list...
return list instanceof Function ?
// call the writer...
list(lst)
// in-place replace list elements...
// NOTE: this is necessary as not everything we do with lst
// is in-place...
: list.splice.apply(list, [0, list.length].concat(lst))
}
var to_remove = []
var lst = list instanceof Function ?
list()
: list
var editable = lst instanceof Array
// view objects...
lst = !editable ? Object.keys(lst) : lst.slice()
var dialog = makeList(null,
lst.concat(new_button ? [ new_button ] : []),
{
path: options.path,
itemButtons: options.itemButtons || [
// mark for removal...
Buttons.markForRemoval(to_remove)
// XXX add shift up/down/top/bottom and other buttons (optional)...
]
})
// select the new_button item...
.on('select', function(evt, elem){
if(new_button && $(elem).find('.text').text() == new_button){
_makeEditable(elem)
}
})
// restore striked-out items...
.on('update', function(){
to_remove.forEach(function(e){
dialog.filter('"'+ e +'"')
.toggleClass('strike-out')
})
})
.open(function(evt, path){
// we clicked the 'New' button -- select it...
if(new_button && (path == new_button || path == '')){
dialog.select(new_button)
} else {
options.callback && options.callback.call(dialog, path)
}
})
.on('close', function(){
// prevent editing non-arrays...
if(!editable){
return
}
// remove striked items...
to_remove.forEach(function(e){
lst.splice(lst.indexOf(e), 1)
_write(lst)
})
// sort...
if(options.sort){
lst.sort(options.sort !== true ? options.sort : undefined)
_write(lst)
}
})
// XXX
new_button && dialog.dom.addClass('tail-action')
return dialog
}
/**********************************************************************
* vim:set ts=4 sw=4 : */ return module })