Skip to content

Instantly share code, notes, and snippets.

@caneruguz
Created September 4, 2014 14:23
Show Gist options
  • Save caneruguz/0e7d1c9ea3edd5878f25 to your computer and use it in GitHub Desktop.
Save caneruguz/0e7d1c9ea3edd5878f25 to your computer and use it in GitHub Desktop.
Mithril jquery ui redrawing issue
[
{
"id" : 0,
"title" : "Dashboard",
"color" : "navy",
"minimize" : false,
"exposeWidth" : 300,
"exposeHeight" : 300,
"css" : "dashboard ht-module-inverted",
"columns" :[
{
"width": 400,
"new" : false,
"widgets" : [
{
"id" : 1,
"title" : "Welcome to Dashboard",
"type" : "dashboard",
"data" : "",
"closable" : true,
"expandable" : true,
"height" : 300,
"display" : true,
"hideHeader" : true,
"content" : "",
"css" : "ht-inverted"
}
]
},
{
"width" : 260,
"new" : true,
"widgets" : []
}
]
},
{
"id" : 1,
"title" : "Reproducibility Project: Psychology",
"color" : "blue",
"minimize" : false,
"exposeWidth" : 300,
"exposeHeight" : 300,
"css" : "",
"columns" :[
{
"width": 620,
"new" : false,
"widgets" : [
{
"id" : 2,
"title" : "Wiki",
"type" : "wiki",
"data" : "../components/wiki/wiki.json",
"closable" : true,
"expandable" : true,
"height" : 300,
"display" : true,
"hideHeader" : true,
"content" : "",
"css" : ""
},
{
"id" : 12,
"title" : "Wiki",
"type" : "wiki",
"data" : "../components/wiki/wiki.json",
"closable" : true,
"expandable" : true,
"height" : 300,
"display" : true,
"hideHeader" : true,
"content" : "",
"css" : ""
}
]
},
{
"width" : 400,
"new" : false,
"widgets" : [
{
"id" : 3,
"title" : "Comments",
"type" : "comments",
"data" : "",
"closable" : true,
"expandable" : true,
"height" : 300,
"display" : true,
"hideHeader" : false,
"content" : "",
"css" : ""
},
{
"id" : 13,
"title" : "Comments",
"type" : "comments",
"data" : "",
"closable" : true,
"expandable" : true,
"height" : 300,
"display" : true,
"hideHeader" : false,
"content" : "",
"css" : ""
}
]
},
{
"width" : 260,
"new" : false,
"widgets" : [
{
"id" : 4,
"title" : "Activity Log",
"type" : "logs",
"data" : "",
"closable" : true,
"expandable" : true,
"height" : 300,
"display" : true,
"hideHeader" : false,
"content" : "",
"css" : ""
},
{
"id" : 5,
"title" : "Comments",
"type" : "comments",
"data" : "",
"closable" : true,
"expandable" : true,
"height" : 300,
"display" : true,
"hideHeader" : true,
"content" : "",
"css" : ""
}
]
}
]
}
]
$(".ht-column" ).sortable({
connectWith: ".ht-column", // So that we can move widgets between other columns.
handle: ".ht-widget-header", // Grab from the header div only.
cursor : "move",
opacity : 0.7,
helper : 'clone',
appendTo : '#ht-content',
forceHelperSize : true,
placeholder: "ht-widget-placeholder",
start : function (event, ui){ // The only outcome of this is to get the widget that is being moved i.e. from
ui.placeholder.width("98%");
ui.helper.css({
width: 200,
height: 200,
overflow : 'hidden'
});
self.temp.fromObj = {}; // empty temp objects so we don't use any of these values accidentally
self.temp.toObj = {};
var from = {
module : ui.item.parent().parent().parent().attr('data-index'),
column : ui.item.parent().attr('data-index'),
widget : ui.item.index()
};
self.temp.fromObj = from; // assign the from object
},
stop : function(event, ui){ // get the widget placement that we want the original widget to drop to
var to = {
module : ui.item.parent().parent().parent().attr('data-index'), // ui returns the same widget but the indexes and placement has changed.
column : ui.item.parent().attr('data-index'),
widget : ui.item.index()
};
self.temp.toObj = to; // Assign the to object, this is not strictly necessary since we use it right away below
$('.ht-column').sortable( "cancel" ); // Stop sortable from actually sorting, leave this to mithril because we changed the observable model
self.moveWidget(self.temp.fromObj, self.temp.toObj); // Move the widget
},
cursorAt: {left:100, top:25}
});
};
// When widgets are moved we need to update the model itself with the changes.
this.moveWidget = function(from, to){
// modules > column > widget modules[0].column[1].widget[1]
// get widget from the from location
var widget = self.modules()[from.module].columns[from.column].widgets[from.widget];
/// if columns are different do as usual - same column number within different widgets also okay
if(from.module !== to.module || from.column !== to.column){
// add it to the to location
self.modules()[to.module].columns[to.column].widgets.splice(to.widget,0,widget);
// remove original widget
self.modules()[from.module].columns[from.column].widgets.splice(from.widget, 1);
// remove extra col if from.column empty
if(self.modules()[from.module].columns[from.column].widgets < 1){
self.modules()[from.module].columns.splice(from.column, 1);
}
} else {
// manage the index numbers properly if they are within the same column
// if from < to first delete then add
if(from.widget < to.widget){
self.modules()[from.module].columns[from.column].widgets.splice(from.widget, 1);
self.modules()[to.module].columns[to.column].widgets.splice(to.widget,0,widget);
} else {
// else first add than delete
self.modules()[to.module].columns[to.column].widgets.splice(to.widget,0,widget);
self.modules()[from.module].columns[from.column].widgets.splice(from.widget+1, 1);
}
}
self.reformatWidth(); // We need to redo sizes. Maybe we should push this to resize Widgets.
self.resizeWidgets(); // After moving we will need to readjust the heights of the widgets
m.redraw(true);
};
// A section of the mithril view that draw the widgets:
module.columns.map(function(column, column_index, column_array){
if(column.widgets.length > 0 || column.new){
// If the view is not narrow in height show full.
return m(".ht-column", {'data-index' : column_index, 'style' : "width:"+column.width+"px"}, [
( function(){
if (column.widgets.length > 0) {
return column.widgets.map(function(widget, widget_index, widget_array){
if(widget.display){
return m(".ht-widget", { config : ctrl.widgetInit, 'data-index' : widget_index, "style" : "height : "+widget.height+"px", "class" : "ui-widget ui-widget-content ui-helper-clearfix " +widget.css}, [
(function(){
if(!widget.hideHeader){
return m(".ht-widget-header", [
widget.title,
m(".ht-widget-actions", [
m("i.fa.fa-expand.ht-widget-expand", { onclick : function(){ ctrl.expandWidget(module_index, column_index, widget_index );} } ),
(function(){
if(widget.closable){
return m("i.fa.fa-times.ht-widget-remove", { onclick : function(){ widget_array.splice(widget_index, 1); }});
}
})()
])
]);
}
})(),
m(".ht-widget-body", [m("div.widget-body-inner",{ id : "widget"+widget.id, config : ctrl.reformat },
(function(){ console.log(widget.id, " was drawn."); return app[widget.type].view(ctrl.controllers[widget.id]);})()
) ])
]);
}
});
}
})()
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment