Improves event system.

This commit is contained in:
Lars Jung 2012-10-25 01:37:34 +02:00
parent aded72363a
commit 34eed09537
13 changed files with 98 additions and 130 deletions

View File

@ -63,7 +63,7 @@ It profits from these great projects:
* removes `aai` mode!
* adds smart browsing
* add line wrap and line highlighting on hover to text preview
* add line wrap and line highlighting (on hover) to text preview
### v0.22.1 - *2012-10-16*

View File

@ -72,6 +72,36 @@ modulejs.define('core/location', ['_', 'modernizr', 'core/settings', 'core/event
return modulejs.require('model/entry').get(absHref);
},
load = function (callback) {
modulejs.require('core/server').request({action: 'get', entries: true, entriesHref: absHref, entriesWhat: 1}, function (json) {
var Entry = modulejs.require('model/entry'),
entry = Entry.get(absHref);
if (json) {
var found = {};
_.each(json.entries, function (jsonEntry) {
var e = Entry.get(jsonEntry.absHref, jsonEntry.time, jsonEntry.size, jsonEntry.status, jsonEntry.content);
found[e.absHref] = true;
});
_.each(entry.content, function (e) {
if (!found[e.absHref]) {
Entry.remove(e.absHref);
}
});
}
if (_.isFunction(callback)) {
callback(entry);
}
});
},
setLocation = function (newAbsHref, keepBrowserUrl) {
newAbsHref = encodedHref(newAbsHref);
@ -89,12 +119,19 @@ modulejs.define('core/location', ['_', 'modernizr', 'core/settings', 'core/event
}
notify.set('loading...');
modulejs.require('core/refresh')(function () {
load(function () {
notify.set();
event.pub('location.changed', getItem());
});
},
refresh = function () {
load(function () {
event.pub('location.refreshed', getItem());
});
},
setLink = function ($el, item) {
$el.attr('href', item.absHref);
@ -123,13 +160,19 @@ modulejs.define('core/location', ['_', 'modernizr', 'core/settings', 'core/event
}
event.sub('ready', function () {
setLocation(document.location.href, true);
});
return {
forceEncoding: forceEncoding,
encodedHref: encodedHref,
getDomain: getDomain,
getAbsHref: getAbsHref,
getItem: getItem,
setLocation: setLocation,
refresh: refresh,
setLink: setLink
};
});

View File

@ -1,37 +0,0 @@
modulejs.define('core/refresh', ['_', 'core/server', 'model/entry', 'core/location'], function (_, server, Entry, location) {
var parseJson = function (entry, json) {
var found = {};
_.each(json.entries, function (jsonEntry) {
var e = Entry.get(jsonEntry.absHref, jsonEntry.time, jsonEntry.size, jsonEntry.status, jsonEntry.content);
found[e.absHref] = true;
});
_.each(entry.content, function (e) {
if (!found[e.absHref]) {
Entry.remove(e.absHref);
}
});
},
refresh = function (callback) {
var entry = Entry.get(location.getAbsHref());
server.request({action: 'get', entries: true, entriesHref: entry.absHref, entriesWhat: 1}, function (json) {
if (json) {
parseJson(entry, json);
}
if (_.isFunction(callback)) {
callback(entry);
}
});
};
return refresh;
});

View File

@ -1,5 +1,5 @@
modulejs.define('ext/autorefresh', ['_', '$', 'core/settings', 'core/event', 'core/refresh'], function (_, $, allsettings, event, refresh) {
modulejs.define('ext/autorefresh', ['_', '$', 'core/settings', 'core/event', 'core/location'], function (_, $, allsettings, event, location) {
var settings = _.extend({
enabled: false,
@ -8,7 +8,7 @@ modulejs.define('ext/autorefresh', ['_', '$', 'core/settings', 'core/event', 'co
heartbeat = function () {
refresh();
location.refresh();
setTimeout(heartbeat, settings.interval);
},

View File

@ -14,7 +14,6 @@ modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/
pageHintTemplate = '<img class="hint" src="' + resource.image('page') + '" alt="has index page"/>',
statusHintTemplate = '<span class="hint"/>',
// updates the crumb for this single entry
update = function (entry, force) {
if (!force && entry.$crumb && entry.$crumb.data('status') === entry.status) {
@ -62,13 +61,6 @@ modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/
return $html;
},
onContentChanged = function (entry) {
if (entry.$crumb) {
update(entry, true);
}
},
onLocationChanged = function (item) {
var crumb = item.getCrumb(),
@ -101,10 +93,6 @@ modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/
return;
}
// event.sub('entry.created', onContentChanged);
// event.sub('entry.removed', onContentChanged);
event.sub('entry.changed', onContentChanged);
event.sub('location.changed', onLocationChanged);
};

View File

@ -1,5 +1,5 @@
modulejs.define('ext/delete', ['_', '$', 'core/settings', 'core/event', 'core/resource', 'core/refresh', 'core/server'], function (_, $, allsettings, event, resource, refresh, server) {
modulejs.define('ext/delete', ['_', '$', 'core/settings', 'core/event', 'core/resource', 'core/location', 'core/server'], function (_, $, allsettings, event, resource, location, server) {
var settings = _.extend({
enabled: false
@ -31,7 +31,7 @@ modulejs.define('ext/delete', ['_', '$', 'core/settings', 'core/event', 'core/re
if (!json || json.code) {
failed();
}
refresh();
location.refresh();
},
requestDeletion = function (hrefsStr) {

View File

@ -1,5 +1,5 @@
modulejs.define('ext/dropbox', ['_', '$', 'core/settings', 'core/location', 'core/refresh', 'core/server'], function (_, $, allsettings, location, refresh, server) {
modulejs.define('ext/dropbox', ['_', '$', 'core/settings', 'core/location', 'core/server'], function (_, $, allsettings, location, server) {
var settings = _.extend({
enabled: false,
@ -99,7 +99,7 @@ modulejs.define('ext/dropbox', ['_', '$', 'core/settings', 'core/location', 'cor
afterAll: function () {
refresh();
location.refresh();
},
error: function (err, file) {

View File

@ -143,8 +143,8 @@ modulejs.define('ext/sort', ['_', '$', 'core/settings', 'core/resource', 'core/e
event.preventDefault();
});
event.sub('entry.changed', onContentChanged);
event.sub('entry.created', onContentChanged);
event.sub('location.changed', onContentChanged);
event.sub('location.refreshed', onContentChanged);
};
init();

View File

@ -1,5 +1,5 @@
modulejs.define('ext/statusbar', ['_', '$', 'core/settings', 'core/format', 'core/event', 'core/location'], function (_, $, allsettings, format, event, location) {
modulejs.define('ext/statusbar', ['_', '$', 'core/settings', 'core/format', 'core/event'], function (_, $, allsettings, format, event) {
var settings = _.extend({
enabled: false
@ -37,36 +37,22 @@ modulejs.define('ext/statusbar', ['_', '$', 'core/settings', 'core/format', 'cor
var $statusbar = $(template),
$folderTotal = $statusbar.find('.folderTotal'),
$fileTotal = $statusbar.find('.fileTotal');
$fileTotal = $statusbar.find('.fileTotal'),
onLocationChanged = function (item) {
var stats = item.getStats();
$folderTotal.text(stats.folders);
$fileTotal.text(stats.files);
};
$statusDefault = $statusbar.find('.status.default');
$statusDynamic = $statusbar.find('.status.dynamic');
event.sub('location.changed', function (item) {
var stats = item.getStats();
$folderTotal.text(stats.folders);
$fileTotal.text(stats.files);
update();
});
event.sub('statusbar', update);
$('#bottombar > .center').append($statusbar);
event.sub('entry.created', function () {
var stats = location.getItem().getStats();
$folderTotal.text(stats.folders);
$fileTotal.text(stats.files);
});
event.sub('entry.removed', function () {
var stats = location.getItem().getStats();
$folderTotal.text(stats.folders);
$fileTotal.text(stats.files);
});
event.sub('statusbar', update);
event.sub('location.changed', onLocationChanged);
event.sub('location.refreshed', onLocationChanged);
event.sub('entry.mouseenter', function (entry) {

View File

@ -20,7 +20,6 @@ modulejs.define('ext/tree', ['_', '$', 'core/settings', 'core/resource', 'core/e
magicSequence = '=h5ai=',
// updates the tree for this single entry
update = function (entry) {
var $html = $(template),
@ -223,7 +222,6 @@ modulejs.define('ext/tree', ['_', '$', 'core/settings', 'core/resource', 'core/e
});
},
// creates the complete tree from entry down to the root
init = function () {
if (!settings.enabled) {
@ -243,14 +241,8 @@ modulejs.define('ext/tree', ['_', '$', 'core/settings', 'core/resource', 'core/e
shiftTree();
});
event.sub('location.changed', onLocationChanged);
// strong negative performance impact in aai mode
// event.sub('entry.changed', onContentChanged);
// event.sub('entry.created', onContentChanged);
// event.sub('entry.removed', onContentChanged);
event.sub('ready', adjustSpacing);
event.sub('location.changed', onLocationChanged);
$(window).on('resize', function () {

View File

@ -1,14 +1,10 @@
modulejs.define('main', ['_', 'core/event'], function (_, event) {
event.pub('beforeView');
modulejs.require('view/items');
modulejs.require('view/spacing');
modulejs.require('view/viewmode');
event.pub('beforeExt');
_.each(modulejs.state(), function (state, id) {
if (/^ext\/.+/.test(id)) {
@ -16,7 +12,5 @@ modulejs.define('main', ['_', 'core/event'], function (_, event) {
}
});
modulejs.require('core/location').setLocation(document.location.href, true);
event.pub('ready');
});

View File

@ -119,6 +119,36 @@ modulejs.define('view/items', ['_', '$', 'core/settings', 'core/resource', 'core
}
},
onLocationRefreshed = function (item) {
var $extended = $('#extended'),
$ul = $extended.find('ul'),
$empty = $extended.find('.empty'),
$items = $ul.find('.entry:not(.folder-parent)'),
currentItems = _.map($items.get(), function (i) { return $(i).data('entry'); }),
refreshedItems = _.values(item.content),
create = _.difference(refreshedItems, currentItems),
remove = _.difference(currentItems, refreshedItems);
_.each(create, function (item) {
update(item, true).hide().appendTo($ul).fadeIn(400);
});
_.each(remove, function (item) {
item.$extended.fadeOut(400, function () {
item.$extended.remove();
});
});
if (item.isEmpty()) {
setTimeout(function () { $empty.show(); }, 400);
} else {
$empty.hide();
}
},
init = function () {
var $content = $(contentTemplate),
@ -134,36 +164,8 @@ modulejs.define('view/items', ['_', '$', 'core/settings', 'core/resource', 'core
.on('mouseenter', '.entry a', onMouseenter)
.on('mouseleave', '.entry a', onMouseleave);
event.sub('entry.changed', function (entry) {
if (entry.isInCurrentFolder() && entry.$extended) {
update(entry, true);
}
});
event.sub('entry.created', function (entry) {
if (entry.isInCurrentFolder() && !entry.$extended) {
$emtpy.fadeOut(100, function () {
update(entry, true).hide().appendTo($ul).fadeIn(400);
});
}
});
event.sub('entry.removed', function (entry) {
if (entry.isInCurrentFolder() && entry.$extended) {
entry.$extended.fadeOut(400, function () {
entry.$extended.remove();
if (entry.parent.isEmpty()) {
$emtpy.fadeIn(100);
}
});
}
});
event.sub('location.changed', onLocationChanged);
event.sub('location.refreshed', onLocationRefreshed);
$content.appendTo('body');
};

View File

@ -55,7 +55,7 @@ Options
- interval: number, update interval in milliseconds, at least 1000
*/
"autorefresh": {
"enabled": false,
"enabled": true,
"interval": 5000
},