WIP: editor in progress

This commit is contained in:
jean-emmanuel 2015-10-31 15:15:39 +01:00
parent 65e9bbfc07
commit 33dd05427f
6 changed files with 361 additions and 75 deletions

View file

@ -60,52 +60,54 @@ init = function(session,callback) {
var contentPanels = $('.content')
function scrollBindings(){
if (webFrame) {
$('.tab').on('mousewheel.zoom',function(e) {
// console.log(e)
if (e.ctrlKey) {
e.preventDefault()
var d = -e.originalEvent.deltaY/Math.abs(e.originalEvent.deltaY)/10,
s = d+webFrame.getZoomFactor()
webFrame.setZoomFactor(s)
if (webFrame) {
$('.tab').on('mousewheel.zoom',function(e) {
// console.log(e)
if (e.ctrlKey) {
e.preventDefault()
var d = -e.originalEvent.deltaY/Math.abs(e.originalEvent.deltaY)/10,
s = d+webFrame.getZoomFactor()
webFrame.setZoomFactor(s)
}
})
$(document).on('keydown.resetzoom', function(e){
if (e.keyCode==96||e.keyCode==48) webFrame.setZoomFactor(1)
})
}
$('.tab').on('mousewheel.scroll',function(e) {
if (!e.ctrlKey) {
if ($(this).height()>$(this).get(0).scrollHeight) {
var scroll = e.originalEvent.deltaY || e.originalEvent.deltaX
$(this).scrollLeft($(this).scrollLeft()+scroll)
e.preventDefault()
} else if (e.pageY>window.innerHeight-scrollbarHeight) {
$(this).scrollLeft($(this).scrollLeft()+e.originalEvent.deltaY)
e.preventDefault()
}
}
})
$(document).on('keydown.resetzoom', function(e){
if (e.keyCode==96||e.keyCode==48) webFrame.setZoomFactor(1)
$('.panel').on('mousewheel.scroll',function(e) {
if (!e.ctrlKey) {
var h = $(this).parent().innerHeight()-scrollbarHeight-$(this).parents('.tab').length*5
if ($(this).get(0).scrollHeight+scrollbarHeight == $(this).parent().height()) {
var scroll = e.originalEvent.deltaY || e.originalEvent.deltaX
$(this).scrollLeft($(this).scrollLeft()+scroll)
e.preventDefault()
}
}
})
}
$('.tab').on('mousewheel.scroll',function(e) {
if (!e.ctrlKey) {
if ($(this).height()>$(this).get(0).scrollHeight) {
var scroll = e.originalEvent.deltaY || e.originalEvent.deltaX
$(this).scrollLeft($(this).scrollLeft()+scroll)
e.preventDefault()
} else if (e.pageY>window.innerHeight-scrollbarHeight) {
$(this).scrollLeft($(this).scrollLeft()+e.originalEvent.deltaY)
e.preventDefault()
}
}
})
$('.panel').on('mousewheel.scroll',function(e) {
if (!e.ctrlKey) {
var h = $(this).parent().innerHeight()-scrollbarHeight-$(this).parents('.tab').length*5
if ($(this).get(0).scrollHeight+scrollbarHeight == $(this).parent().height()) {
var scroll = e.originalEvent.deltaY || e.originalEvent.deltaX
$(this).scrollLeft($(this).scrollLeft()+scroll)
e.preventDefault()
}
}
})
scrollBindings()
// sidepanel
@ -141,6 +143,10 @@ init = function(session,callback) {
click:toggleFullscreen,
icon:'tv'
},
{
html:'<div class="editor btn">\
</div>',
},
{
html:'<div class="inspector btn">\
Inspector\
@ -155,9 +161,9 @@ init = function(session,callback) {
$('#open-toggle, #sidepanel, #container').toggleClass('sidepanel-open')
})
$('.widget .label').click(function(){
$('.widget').click(function(e){
if (!$('#sidepanel').hasClass('sidepanel-open')){return}
var data = String(JSON.stringify($(this).data('papers'),null,2)).split('\n').join('<br/>&nbsp;&nbsp;')
var data = String(JSON.stringify($(e.target).parents('.widget').first().data('widgetData'),null,2)).split('\n').join('<br/>&nbsp;&nbsp;')
$('.inspector .result').html(data)
})
@ -192,6 +198,96 @@ init = function(session,callback) {
function editor(){
$('.widget').click(function(e){
e.preventDefault()
var widget = $(e.target).parents('.widget').first()
var parent = widget.parent()
var index = widget.index()
var data = widget.data('widgetData')
var form = $('<form></form>')
for (i in data) {
if (i!='widgets') {
var d = JSON.stringify(data[i])
var input = $(`
<input value='${d}' title="${i}"/>
`)
input.appendTo(form)
input.on('change',function(){
data[$(this).attr('title')]=$(this).val().match(/[false|true]/)==null?eval($(this).val()):JSON.parse($(this).val())
parsewidgets([data],widget.parent())
widget.remove()
var newWidget = $('[widgetId="'+data.id+'"]')
if (index == parent.children().length-1) {
newWidget.detach().appendTo(parent)
} else {
newWidget.detach().insertBefore(parent.children().eq(index))
}
editor()
newWidget.children().first().click()
})
}
}
if (data.widgets) {
var list = $('<ul></ul>')
for (i in data.widgets) {
var item = $(`<li class="sortables" data-id="${data.widgets[i].id}">${data.widgets[i].id}</li>`).appendTo(list)
}
list.sortable({forcePlaceholderSize: true, items: '.sortables'}).on('sortupdate',function(e,ui){
var prevIndex = $('[widgetId="'+$(ui.item).text()+'"]').index()
var newIndex = $(ui.item).index()
data.widgets.splice(newIndex, 0, data.widgets.splice(prevIndex, 1)[0])
parsewidgets([data],widget.parent())
widget.remove()
var newWidget = $('[widgetId="'+data.id+'"]')
if (index == parent.children().length-1) {
newWidget.detach().appendTo(parent)
} else {
newWidget.detach().insertBefore(parent.children().eq(index))
}
editor()
newWidget.children().first().click()
})
var add = $(`<li>+</li>`).appendTo(list).click(function(){
data.widgets.push({})
parsewidgets([data],widget.parent())
widget.remove()
var newWidget = $('[widgetId="'+data.id+'"]')
if (index == parent.children().length-1) {
newWidget.detach().appendTo(parent)
} else {
newWidget.detach().insertBefore(parent.children().eq(index))
}
editor()
newWidget.children().first().click()
})
list.appendTo(form)
}
$('.editor').html(form)
scrollBindings()
})
}
editor()
$('#open-toggle').click()
if (callback) callback()
}

View file

@ -9,9 +9,12 @@ getIterator = function(type){
return iterator[type]
}
session = []
parsetabs = function(tabs,parent,sub){
if (!sub) {
session = tabs
var nav = $(document.createElement('div')).addClass('main navigation'),
navtabs = $(document.createElement('ul')).addClass('tablist'),
content = $(document.createElement('div')).addClass('content')
@ -37,6 +40,7 @@ parsetabs = function(tabs,parent,sub){
navtabs.append(`<li><a data-tab="#${id}"><span>${label}</span></a></li>`)
var tabContent = $('<div></div>').addClass('tab').attr('id',id)
tabContent.data('tabData',tabData)
if (tabData.stretch) tabContent.addClass('stretch')
@ -58,21 +62,31 @@ parsewidgets = function(widgets,parent) {
for (i in widgets) {
var widgetData = widgets[i]
var type = widgetData.type || 'fader'
widgetData.id = widgetData.id?widgetData.id.replace(' ','_'):type+'_'+getIterator(type)
widgetData.type = widgetData.type || 'fader'
var id = widgetData.id,
label = widgetData.label || id,
path = widgetData.path || '/' + id,
width = clip(parseInt(widgetData.width),[100,2000]),
style= widgetData.width?`width:${width}rem;min-width:${width}rem`:''
for (i in widgetOptions[widgetData.type]) {
if (widgetData[i]===undefined) widgetData[i] = widgetOptions[widgetData.type][i]
}
widgetData.path = path
for (i in widgetData) {
if (widgetOptions[widgetData.type][i]===undefined && i!='type') {delete widgetData[i]}
}
widgetData.id = widgetData.id=='auto'?widgetData.type+'_'+getIterator(widgetData.type):widgetData.id.replace(' ','_')
widgetData.label = widgetData.label=='auto'?widgetData.id:widgetData.label
widgetData.path = widgetData.path=='auto'?'/' + widgetData.id:widgetData.path
widgetData.target = widgetData.target?(Array.isArray(widgetData.target)?widgetData.target:[widgetData.target]):false
var width = parseInt(widgetData.width)==widgetData.width?parseInt(widgetData.width)+'rem' : widgetData.width,
style = widgetData.width!='auto'?`width:${width};min-width:${width}`:''
var widgetContainer = $(`
<div class="widget ${type}-container" widgetType="${type}" widgetId="${id}" path="${path}" style="${style}">
<div class="label"><span>${label}</span></div>
<div class="widget ${widgetData.type}-container" widgetType="${widgetData.type}" widgetId="${widgetData.id}" path="${widgetData.path}" style="${style}">
<div class="label"><span>${widgetData.label}</span></div>
</div>
`)
@ -80,21 +94,22 @@ parsewidgets = function(widgets,parent) {
if (widgetData.label===false) widgetContainer.addClass('nolabel')
// create widget
var widgetInner = createWidget[type](widgetData,widgetContainer)
widgetInner.type = type
widgetContainer.find('.label').data('papers',widgetData)
var widgetInner = createWidget[widgetData.type](widgetData,widgetContainer)
widgetInner.type = widgetData.type
widgetContainer.data('widgetData',widgetData)
widgetContainer.append(widgetInner)
// store widget reference for cross widget sync
if (__widgets__[id]==undefined) {
__widgets__[id] = []
if (__widgets__[widgetData.id]==undefined) {
__widgets__[widgetData.id] = []
}
__widgets__[id].push(widgetInner)
__widgets__[widgetData.id].push(widgetInner)
// store path vs widget id for faster cross-app sync
__widgetsIds__[widgetData.path ] = id
__widgetsIds__[widgetData.path ] = widgetData.id
parent.append(widgetContainer)
}

View file

@ -1,5 +1,89 @@
createWidget= {}
createWidget = {}
widgetOptions = {
strip: {
id:'auto',
label:'auto',
horizontal:false,
width:'auto',
widgets:[]
},
panel: {
id:'auto',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
widgets:[]
},
led: {
id:'auto',
label:'auto',
color:'green',
width:'auto',
range:{min:0,max:1},
path:'auto'
},
toggle: {
id:'auto',
label:'auto',
width:'auto',
on:0,
off:1,
path:'auto',
target:false
},
switch: {
id:'auto',
label:'auto',
width:'auto',
values:[],
path:'auto',
target:false
},
xy: {
id:'auto',
label:'auto',
width:'auto',
split:false,
range:{x:{min:0,max:1},y:{min:0,max:1}},
absolute:false,
path:'auto',
target:false
},
rgb: {
id:'auto',
label:'auto',
width:'auto',
split:false,
// range:{r:{min:0,max:255},g:{min:0,max:255},b:{min:255}},
absolute:false,
path:'auto',
target:false
},
fader: {
id:'auto',
label:'auto',
width:'auto',
horizontal:false,
range:{min:0,max:1},
absolute:false,
path:'auto',
target:false
},
knob: {
id:'auto',
label:'auto',
width:'auto',
horizontal:false,
range:{min:0,max:1},
absolute:false,
pan:false,
path:'auto',
target:false
}
}
createWidget.strip = function(widgetData,container) {
var widget = $(`
@ -20,20 +104,26 @@ createWidget.panel = function(widgetData,container) {
var widget = $(`
<div class="panel">
</div>
`),
left = parseInt(widgetData.x) || 'auto',
top = parseInt(widgetData.y) || 'auto',
width = parseInt(widgetData.width) || 'auto',
height = parseInt(widgetData.height) || 'auto'
`),
left = parseInt(widgetData.left)==widgetData.left?parseInt(widgetData.left)+'rem' : widgetData.left,
top = parseInt(widgetData.top)==widgetData.top?parseInt(widgetData.top)+'rem' : widgetData.top,
width = parseInt(widgetData.width)==widgetData.width?parseInt(widgetData.width)+'rem' : widgetData.width,
height = parseInt(widgetData.height)==widgetData.height?parseInt(widgetData.height)+'rem' : widgetData.height
container.css({
left:parseInt(widgetData.left)+'rem',
top:parseInt(widgetData.top)+'rem',
width:parseInt(widgetData.width)+'rem',
height:parseInt(widgetData.height)+'rem',
'min-width':''
left:'auto',
top:'auto',
width:'auto',
height:'auto'
})
container.css({
left:left,
top:top,
width:width,
height:height
})
parsewidgets(widgetData.widgets,widget)
widget.getValue = function(){return}
@ -63,7 +153,7 @@ createWidget.led = function(widgetData) {
createWidget.toggle = createWidget.button = function(widgetData) {
createWidget.toggle = function(widgetData) {
widgetData.on = widgetData.on || 1
widgetData.off = widgetData.off || 0
@ -175,7 +265,7 @@ createWidget.xy = function(widgetData) {
handle = widget.find('.handle'),
pad = widget.find('.xy'),
value = {x:widget.find('.x'),y:widget.find('.y')},
range = widgetData.range || {x:{min:0,max:1},y:{min:0,max:1}},
range = widgetData.range,
absolute = widgetData.absolute,
split = widgetData.split?
typeof widgetData.split == 'object'?
@ -183,9 +273,6 @@ createWidget.xy = function(widgetData) {
:false
widgetData.range = range
pad.width = pad.innerWidth()
pad.height = pad.innerHeight()
handle.height = 0
@ -300,6 +387,7 @@ createWidget.xy = function(widgetData) {
createWidget.rgb = function(widgetData) {
var widget = $(`
<div class="xy-wrapper rgb-wrapper">
<div class="xy rgb">

View file

@ -14,6 +14,7 @@ window.$ = window.jQuery = require('./jquery/jquery.min.js')
// third-party js libraries
require('./jquery/jquery.sortable.js')
require('./jquery/jquery.drag.js')
require('./jquery/jquery.resize.js')

View file

@ -8,6 +8,7 @@ window.$ = window.jQuery = require('./jquery/jquery.min.js')
// third-party js libraries
require('./jquery/jquery.sortable.js')
require('./jquery/jquery.drag.js')
require('./jquery/jquery.resize.js')

View file

@ -0,0 +1,85 @@
/*
* HTML5 Sortable jQuery Plugin
* http://farhadi.ir/projects/html5sortable
*
* Copyright 2012, Ali Farhadi
* Released under the MIT license.
*/
(function($) {
var dragging, placeholders = $();
$.fn.sortable = function(options) {
var method = String(options);
options = $.extend({
connectWith: false
}, options);
return this.each(function() {
if (/^(enable|disable|destroy)$/.test(method)) {
var items = $(this).children($(this).data('items')).attr('draggable', method == 'enable');
if (method == 'destroy') {
items.add(this).removeData('connectWith items')
.off('dragstart.h5s dragend.h5s selectstart.h5s dragover.h5s dragenter.h5s drop.h5s');
}
return;
}
var isHandle, index, items = $(this).children(options.items);
var placeholder = $('<' + (/^(ul|ol)$/i.test(this.tagName) ? 'li' : 'div') + ' class="sortable-placeholder">');
items.find(options.handle).mousedown(function() {
isHandle = true;
}).mouseup(function() {
isHandle = false;
});
$(this).data('items', options.items)
placeholders = placeholders.add(placeholder);
if (options.connectWith) {
$(options.connectWith).add(this).data('connectWith', options.connectWith);
}
items.attr('draggable', 'true').on('dragstart.h5s', function(e) {
if (options.handle && !isHandle) {
return false;
}
isHandle = false;
var dt = e.originalEvent.dataTransfer;
dt.effectAllowed = 'move';
dt.setData('Text', 'dummy');
index = (dragging = $(this)).addClass('sortable-dragging').index();
}).on('dragend.h5s', function() {
if (!dragging) {
return;
}
dragging.removeClass('sortable-dragging').show();
placeholders.detach();
if (index != dragging.index()) {
dragging.parent().trigger('sortupdate', {item: dragging});
}
dragging = null;
}).not('a[href], img').on('selectstart.h5s', function() {
this.dragDrop && this.dragDrop();
return false;
}).end().add([this, placeholder]).on('dragover.h5s dragenter.h5s drop.h5s', function(e) {
if (!items.is(dragging) && options.connectWith !== $(dragging).parent().data('connectWith')) {
return true;
}
if (e.type == 'drop') {
e.stopPropagation();
placeholders.filter(':visible').after(dragging);
dragging.trigger('dragend.h5s');
return false;
}
e.preventDefault();
e.originalEvent.dataTransfer.dropEffect = 'move';
if (items.is(this)) {
if (options.forcePlaceholderSize) {
placeholder.height(dragging.outerHeight());
}
dragging.hide();
$(this)[placeholder.index() < $(this).index() ? 'after' : 'before'](placeholder);
placeholders.not(placeholder).detach();
} else if (!placeholders.is(this) && !$(this).children(options.items).length) {
placeholders.detach();
$(this).append(placeholder);
}
return false;
});
});
};
})(jQuery);