mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-25 20:44:53 -04:00
Improves event system.
This commit is contained in:
parent
aded72363a
commit
34eed09537
13 changed files with 98 additions and 130 deletions
|
@ -63,7 +63,7 @@ It profits from these great projects:
|
||||||
|
|
||||||
* removes `aai` mode!
|
* removes `aai` mode!
|
||||||
* adds smart browsing
|
* 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*
|
### v0.22.1 - *2012-10-16*
|
||||||
|
|
|
@ -72,6 +72,36 @@ modulejs.define('core/location', ['_', 'modernizr', 'core/settings', 'core/event
|
||||||
return modulejs.require('model/entry').get(absHref);
|
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) {
|
setLocation = function (newAbsHref, keepBrowserUrl) {
|
||||||
|
|
||||||
newAbsHref = encodedHref(newAbsHref);
|
newAbsHref = encodedHref(newAbsHref);
|
||||||
|
@ -89,12 +119,19 @@ modulejs.define('core/location', ['_', 'modernizr', 'core/settings', 'core/event
|
||||||
}
|
}
|
||||||
|
|
||||||
notify.set('loading...');
|
notify.set('loading...');
|
||||||
modulejs.require('core/refresh')(function () {
|
load(function () {
|
||||||
notify.set();
|
notify.set();
|
||||||
event.pub('location.changed', getItem());
|
event.pub('location.changed', getItem());
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
refresh = function () {
|
||||||
|
|
||||||
|
load(function () {
|
||||||
|
event.pub('location.refreshed', getItem());
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
setLink = function ($el, item) {
|
setLink = function ($el, item) {
|
||||||
|
|
||||||
$el.attr('href', item.absHref);
|
$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 {
|
return {
|
||||||
forceEncoding: forceEncoding,
|
forceEncoding: forceEncoding,
|
||||||
encodedHref: encodedHref,
|
|
||||||
getDomain: getDomain,
|
getDomain: getDomain,
|
||||||
getAbsHref: getAbsHref,
|
getAbsHref: getAbsHref,
|
||||||
getItem: getItem,
|
getItem: getItem,
|
||||||
setLocation: setLocation,
|
setLocation: setLocation,
|
||||||
|
refresh: refresh,
|
||||||
setLink: setLink
|
setLink: setLink
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
@ -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;
|
|
||||||
});
|
|
|
@ -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({
|
var settings = _.extend({
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
@ -8,7 +8,7 @@ modulejs.define('ext/autorefresh', ['_', '$', 'core/settings', 'core/event', 'co
|
||||||
|
|
||||||
heartbeat = function () {
|
heartbeat = function () {
|
||||||
|
|
||||||
refresh();
|
location.refresh();
|
||||||
setTimeout(heartbeat, settings.interval);
|
setTimeout(heartbeat, settings.interval);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -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"/>',
|
pageHintTemplate = '<img class="hint" src="' + resource.image('page') + '" alt="has index page"/>',
|
||||||
statusHintTemplate = '<span class="hint"/>',
|
statusHintTemplate = '<span class="hint"/>',
|
||||||
|
|
||||||
// updates the crumb for this single entry
|
|
||||||
update = function (entry, force) {
|
update = function (entry, force) {
|
||||||
|
|
||||||
if (!force && entry.$crumb && entry.$crumb.data('status') === entry.status) {
|
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;
|
return $html;
|
||||||
},
|
},
|
||||||
|
|
||||||
onContentChanged = function (entry) {
|
|
||||||
|
|
||||||
if (entry.$crumb) {
|
|
||||||
update(entry, true);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
onLocationChanged = function (item) {
|
onLocationChanged = function (item) {
|
||||||
|
|
||||||
var crumb = item.getCrumb(),
|
var crumb = item.getCrumb(),
|
||||||
|
@ -101,10 +93,6 @@ modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// event.sub('entry.created', onContentChanged);
|
|
||||||
// event.sub('entry.removed', onContentChanged);
|
|
||||||
event.sub('entry.changed', onContentChanged);
|
|
||||||
|
|
||||||
event.sub('location.changed', onLocationChanged);
|
event.sub('location.changed', onLocationChanged);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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({
|
var settings = _.extend({
|
||||||
enabled: false
|
enabled: false
|
||||||
|
@ -31,7 +31,7 @@ modulejs.define('ext/delete', ['_', '$', 'core/settings', 'core/event', 'core/re
|
||||||
if (!json || json.code) {
|
if (!json || json.code) {
|
||||||
failed();
|
failed();
|
||||||
}
|
}
|
||||||
refresh();
|
location.refresh();
|
||||||
},
|
},
|
||||||
|
|
||||||
requestDeletion = function (hrefsStr) {
|
requestDeletion = function (hrefsStr) {
|
||||||
|
|
|
@ -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({
|
var settings = _.extend({
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
@ -99,7 +99,7 @@ modulejs.define('ext/dropbox', ['_', '$', 'core/settings', 'core/location', 'cor
|
||||||
|
|
||||||
afterAll: function () {
|
afterAll: function () {
|
||||||
|
|
||||||
refresh();
|
location.refresh();
|
||||||
},
|
},
|
||||||
|
|
||||||
error: function (err, file) {
|
error: function (err, file) {
|
||||||
|
|
|
@ -143,8 +143,8 @@ modulejs.define('ext/sort', ['_', '$', 'core/settings', 'core/resource', 'core/e
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
});
|
});
|
||||||
|
|
||||||
event.sub('entry.changed', onContentChanged);
|
event.sub('location.changed', onContentChanged);
|
||||||
event.sub('entry.created', onContentChanged);
|
event.sub('location.refreshed', onContentChanged);
|
||||||
};
|
};
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
|
@ -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({
|
var settings = _.extend({
|
||||||
enabled: false
|
enabled: false
|
||||||
|
@ -37,36 +37,22 @@ modulejs.define('ext/statusbar', ['_', '$', 'core/settings', 'core/format', 'cor
|
||||||
|
|
||||||
var $statusbar = $(template),
|
var $statusbar = $(template),
|
||||||
$folderTotal = $statusbar.find('.folderTotal'),
|
$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');
|
$statusDefault = $statusbar.find('.status.default');
|
||||||
$statusDynamic = $statusbar.find('.status.dynamic');
|
$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);
|
$('#bottombar > .center').append($statusbar);
|
||||||
|
|
||||||
event.sub('entry.created', function () {
|
event.sub('statusbar', update);
|
||||||
|
event.sub('location.changed', onLocationChanged);
|
||||||
var stats = location.getItem().getStats();
|
event.sub('location.refreshed', onLocationChanged);
|
||||||
$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('entry.mouseenter', function (entry) {
|
event.sub('entry.mouseenter', function (entry) {
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ modulejs.define('ext/tree', ['_', '$', 'core/settings', 'core/resource', 'core/e
|
||||||
|
|
||||||
magicSequence = '=h5ai=',
|
magicSequence = '=h5ai=',
|
||||||
|
|
||||||
// updates the tree for this single entry
|
|
||||||
update = function (entry) {
|
update = function (entry) {
|
||||||
|
|
||||||
var $html = $(template),
|
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 () {
|
init = function () {
|
||||||
|
|
||||||
if (!settings.enabled) {
|
if (!settings.enabled) {
|
||||||
|
@ -243,14 +241,8 @@ modulejs.define('ext/tree', ['_', '$', 'core/settings', 'core/resource', 'core/e
|
||||||
shiftTree();
|
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('ready', adjustSpacing);
|
||||||
|
event.sub('location.changed', onLocationChanged);
|
||||||
|
|
||||||
$(window).on('resize', function () {
|
$(window).on('resize', function () {
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,10 @@
|
||||||
|
|
||||||
modulejs.define('main', ['_', 'core/event'], function (_, event) {
|
modulejs.define('main', ['_', 'core/event'], function (_, event) {
|
||||||
|
|
||||||
event.pub('beforeView');
|
|
||||||
|
|
||||||
modulejs.require('view/items');
|
modulejs.require('view/items');
|
||||||
modulejs.require('view/spacing');
|
modulejs.require('view/spacing');
|
||||||
modulejs.require('view/viewmode');
|
modulejs.require('view/viewmode');
|
||||||
|
|
||||||
event.pub('beforeExt');
|
|
||||||
|
|
||||||
_.each(modulejs.state(), function (state, id) {
|
_.each(modulejs.state(), function (state, id) {
|
||||||
|
|
||||||
if (/^ext\/.+/.test(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');
|
event.pub('ready');
|
||||||
});
|
});
|
||||||
|
|
|
@ -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 () {
|
init = function () {
|
||||||
|
|
||||||
var $content = $(contentTemplate),
|
var $content = $(contentTemplate),
|
||||||
|
@ -134,36 +164,8 @@ modulejs.define('view/items', ['_', '$', 'core/settings', 'core/resource', 'core
|
||||||
.on('mouseenter', '.entry a', onMouseenter)
|
.on('mouseenter', '.entry a', onMouseenter)
|
||||||
.on('mouseleave', '.entry a', onMouseleave);
|
.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.changed', onLocationChanged);
|
||||||
|
event.sub('location.refreshed', onLocationRefreshed);
|
||||||
|
|
||||||
$content.appendTo('body');
|
$content.appendTo('body');
|
||||||
};
|
};
|
||||||
|
|
|
@ -55,7 +55,7 @@ Options
|
||||||
- interval: number, update interval in milliseconds, at least 1000
|
- interval: number, update interval in milliseconds, at least 1000
|
||||||
*/
|
*/
|
||||||
"autorefresh": {
|
"autorefresh": {
|
||||||
"enabled": false,
|
"enabled": true,
|
||||||
"interval": 5000
|
"interval": 5000
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue