mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-25 20:44:53 -04:00
Adds new events. Adds autoupdate extension.
This commit is contained in:
parent
4245f2019e
commit
da438f2f0b
10 changed files with 195 additions and 12 deletions
|
@ -51,6 +51,18 @@ var H5AI_CONFIG = {
|
||||||
* Extensions in alphabetical order.
|
* Extensions in alphabetical order.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Watch current folder content.
|
||||||
|
* Folders possibly visible in the tree view that are not the
|
||||||
|
* current folder might not be updated.
|
||||||
|
*
|
||||||
|
* Interval will be a least 1000 milliseconds.
|
||||||
|
*/
|
||||||
|
"autoupdate": {
|
||||||
|
"enabled": true,
|
||||||
|
"interval": 5000
|
||||||
|
},
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Show a clickable breadcrumb.
|
* Show a clickable breadcrumb.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -13,7 +13,7 @@ modulejs.define('core/event', ['amplify'], function (amplify) {
|
||||||
|
|
||||||
pub = function (topic, data) {
|
pub = function (topic, data) {
|
||||||
|
|
||||||
// console.log('EVENT PUB', topic, data);
|
console.log('EVENT PUB', topic, data);
|
||||||
amplify.publish(topic, data);
|
amplify.publish(topic, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
64
src/_h5ai/js/inc/ext/autoupdate.js
Normal file
64
src/_h5ai/js/inc/ext/autoupdate.js
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
|
||||||
|
modulejs.define('ext/autoupdate', ['_', '$', 'core/settings', 'core/event', 'core/resource', 'model/entry'], function (_, $, allsettings, event, resource, Entry) {
|
||||||
|
|
||||||
|
var defaults = {
|
||||||
|
enabled: false,
|
||||||
|
interval: 5000
|
||||||
|
},
|
||||||
|
|
||||||
|
settings = _.extend({}, defaults, allsettings.autoupdate),
|
||||||
|
|
||||||
|
parseJson = function (entry, json) {
|
||||||
|
|
||||||
|
var found = {};
|
||||||
|
|
||||||
|
_.each(json.entries, function (jsonEntry) {
|
||||||
|
|
||||||
|
found[jsonEntry.absHref] = true;
|
||||||
|
Entry.get(jsonEntry.absHref, jsonEntry.time, jsonEntry.size, jsonEntry.status, jsonEntry.content);
|
||||||
|
});
|
||||||
|
|
||||||
|
_.each(entry.content, function (e) {
|
||||||
|
if (!found[e.absHref]) {
|
||||||
|
Entry.remove(e.absHref);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
heartbeat = function () {
|
||||||
|
|
||||||
|
var entry = Entry.get();
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: resource.api(),
|
||||||
|
data: {
|
||||||
|
action: 'getentries',
|
||||||
|
href: entry.absHref,
|
||||||
|
content: 1
|
||||||
|
},
|
||||||
|
dataType: 'json',
|
||||||
|
success: function (json) {
|
||||||
|
|
||||||
|
parseJson(entry, json);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(heartbeat, settings.interval);
|
||||||
|
},
|
||||||
|
|
||||||
|
init = function () {
|
||||||
|
|
||||||
|
if (!settings.enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
settings.interval = Math.max(1000, settings.interval);
|
||||||
|
|
||||||
|
event.sub('ready', function () {
|
||||||
|
|
||||||
|
setTimeout(heartbeat, settings.interval);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
init();
|
||||||
|
});
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/entry'], function (_, $, allsettings, resource, entry) {
|
modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/event', 'core/entry'], function (_, $, allsettings, resource, event, entry) {
|
||||||
|
|
||||||
var defaults = {
|
var defaults = {
|
||||||
enabled: false
|
enabled: false
|
||||||
|
@ -17,9 +17,9 @@ modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/
|
||||||
statusHintTemplate = '<span class="hint"></span>',
|
statusHintTemplate = '<span class="hint"></span>',
|
||||||
|
|
||||||
// updates the crumb for this single entry
|
// updates the crumb for this single entry
|
||||||
update = function (entry) {
|
update = function (entry, force) {
|
||||||
|
|
||||||
if (entry.$crumb && entry.$crumb.data('status') === entry.status) {
|
if (!force && entry.$crumb && entry.$crumb.data('status') === entry.status) {
|
||||||
return entry.$crumb;
|
return entry.$crumb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,6 +59,13 @@ modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/
|
||||||
return $html;
|
return $html;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
onContentChanged = function (entry) {
|
||||||
|
|
||||||
|
if (entry.$crumb) {
|
||||||
|
update(entry, true);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// creates the complete crumb from entry down to the root
|
// creates the complete crumb from entry down to the root
|
||||||
init = function (entry) {
|
init = function (entry) {
|
||||||
|
|
||||||
|
@ -78,6 +85,10 @@ modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/
|
||||||
update(e);
|
update(e);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
event.sub('entry.created', onContentChanged);
|
||||||
|
event.sub('entry.removed', onContentChanged);
|
||||||
|
event.sub('entry.changed', onContentChanged);
|
||||||
};
|
};
|
||||||
|
|
||||||
init(entry);
|
init(entry);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
modulejs.define('ext/sort', ['_', '$', 'core/settings', 'core/resource', 'core/store'], function (_, $, allsettings, resource, store) {
|
modulejs.define('ext/sort', ['_', '$', 'core/settings', 'core/resource', 'core/event', 'core/store'], function (_, $, allsettings, resource, event, store) {
|
||||||
|
|
||||||
var defaults = {
|
var defaults = {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
@ -70,6 +70,11 @@ modulejs.define('ext/sort', ['_', '$', 'core/settings', 'core/resource', 'core/s
|
||||||
$('#extended .entry').detach().sort(order.fn).appendTo('#extended > ul');
|
$('#extended .entry').detach().sort(order.fn).appendTo('#extended > ul');
|
||||||
},
|
},
|
||||||
|
|
||||||
|
onContentChanged = function (entry) {
|
||||||
|
|
||||||
|
sortBy(store.get(storekey) || settings.order);
|
||||||
|
},
|
||||||
|
|
||||||
init = function () {
|
init = function () {
|
||||||
|
|
||||||
if (!settings.enabled) {
|
if (!settings.enabled) {
|
||||||
|
@ -139,6 +144,9 @@ modulejs.define('ext/sort', ['_', '$', 'core/settings', 'core/resource', 'core/s
|
||||||
sortBy('s' + ($size.hasClass('ascending') ? 'd' : 'a'));
|
sortBy('s' + ($size.hasClass('ascending') ? 'd' : 'a'));
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
event.sub('entry.changed', onContentChanged);
|
||||||
|
event.sub('entry.created', onContentChanged);
|
||||||
};
|
};
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
|
@ -53,7 +53,19 @@ modulejs.define('ext/statusbar', ['_', '$', 'core/settings', 'core/format', 'cor
|
||||||
event.sub('statusbar', update);
|
event.sub('statusbar', update);
|
||||||
$('#bottombar > .center').append($statusbar);
|
$('#bottombar > .center').append($statusbar);
|
||||||
|
|
||||||
|
event.sub('entry.created', function () {
|
||||||
|
|
||||||
|
var stats = entry.getStats();
|
||||||
|
$folderTotal.text(stats.folders);
|
||||||
|
$fileTotal.text(stats.files);
|
||||||
|
});
|
||||||
|
|
||||||
|
event.sub('entry.removed', function () {
|
||||||
|
|
||||||
|
var stats = entry.getStats();
|
||||||
|
$folderTotal.text(stats.folders);
|
||||||
|
$fileTotal.text(stats.files);
|
||||||
|
});
|
||||||
|
|
||||||
event.sub('entry.mouseenter', function (entry) {
|
event.sub('entry.mouseenter', function (entry) {
|
||||||
|
|
||||||
|
|
|
@ -186,6 +186,15 @@ modulejs.define('ext/tree', ['_', '$', 'core/settings', 'core/resource', 'core/e
|
||||||
$tree.scrollpanel('update');
|
$tree.scrollpanel('update');
|
||||||
},
|
},
|
||||||
|
|
||||||
|
onContentChanged = function (entry) {
|
||||||
|
|
||||||
|
while (entry.parent) {
|
||||||
|
entry = entry.parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
update(entry);
|
||||||
|
},
|
||||||
|
|
||||||
// creates the complete tree from entry down to the root
|
// creates the complete tree from entry down to the root
|
||||||
init = function (entry, parser) {
|
init = function (entry, parser) {
|
||||||
|
|
||||||
|
@ -217,6 +226,9 @@ modulejs.define('ext/tree', ['_', '$', 'core/settings', 'core/resource', 'core/e
|
||||||
});
|
});
|
||||||
|
|
||||||
event.sub('ready', adjustSpacing);
|
event.sub('ready', adjustSpacing);
|
||||||
|
event.sub('entry.changed', onContentChanged);
|
||||||
|
event.sub('entry.created', onContentChanged);
|
||||||
|
event.sub('entry.removed', onContentChanged);
|
||||||
|
|
||||||
$(window).on('resize', function () {
|
$(window).on('resize', function () {
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
// @include "view/spacing.js"
|
// @include "view/spacing.js"
|
||||||
// @include "view/viewmode.js"
|
// @include "view/viewmode.js"
|
||||||
|
|
||||||
|
// @include "ext/autoupdate.js"
|
||||||
// @include "ext/crumb.js"
|
// @include "ext/crumb.js"
|
||||||
// @include "ext/custom.js"
|
// @include "ext/custom.js"
|
||||||
// @include "ext/download.js"
|
// @include "ext/download.js"
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, types, ajax) {
|
modulejs.define('model/entry', ['_', 'core/types', 'core/ajax', 'core/event'], function (_, types, ajax, event) {
|
||||||
|
|
||||||
var doc = document,
|
var doc = document,
|
||||||
domain = doc.domain,
|
domain = doc.domain,
|
||||||
|
@ -111,24 +111,62 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
|
||||||
|
|
||||||
absHref = forceEncoding(absHref || location);
|
absHref = forceEncoding(absHref || location);
|
||||||
|
|
||||||
|
var created = !cache[absHref],
|
||||||
|
changed = false;
|
||||||
|
|
||||||
var self = cache[absHref] || new Entry(absHref);
|
var self = cache[absHref] || new Entry(absHref);
|
||||||
|
|
||||||
if (_.isNumber(time)) {
|
if (_.isNumber(time)) {
|
||||||
|
if (self.time !== time) {
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
self.time = time;
|
self.time = time;
|
||||||
}
|
}
|
||||||
if (_.isNumber(size)) {
|
if (_.isNumber(size)) {
|
||||||
|
if (self.size !== size) {
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
self.size = size;
|
self.size = size;
|
||||||
}
|
}
|
||||||
if (status) {
|
if (status) {
|
||||||
|
if (self.status !== status) {
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
self.status = status;
|
self.status = status;
|
||||||
}
|
}
|
||||||
if (isContentFetched) {
|
if (isContentFetched) {
|
||||||
self.isContentFetched = true;
|
self.isContentFetched = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (created) {
|
||||||
|
event.pub('entry.created', self);
|
||||||
|
} else if (changed) {
|
||||||
|
event.pub('entry.changed', self);
|
||||||
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
removeEntry = function (absHref) {
|
||||||
|
|
||||||
|
absHref = forceEncoding(absHref || location);
|
||||||
|
|
||||||
|
var self = cache[absHref];
|
||||||
|
|
||||||
|
if (self) {
|
||||||
|
delete cache[absHref];
|
||||||
|
if (self.parent) {
|
||||||
|
delete self.parent.content[self.absHref];
|
||||||
|
}
|
||||||
|
_.each(self.content, function (entry) {
|
||||||
|
|
||||||
|
removeEntry(entry.absHref);
|
||||||
|
});
|
||||||
|
|
||||||
|
event.pub('entry.removed', self);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
fetchStatus = function (absHref, callback) {
|
fetchStatus = function (absHref, callback) {
|
||||||
|
|
||||||
var self = getEntry(absHref);
|
var self = getEntry(absHref);
|
||||||
|
@ -202,6 +240,11 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
|
||||||
return this.absHref === location;
|
return this.absHref === location;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
isInCurrentFolder: function () {
|
||||||
|
|
||||||
|
return !!this.parent && this.parent.isCurrentFolder();
|
||||||
|
},
|
||||||
|
|
||||||
isDomain: function () {
|
isDomain: function () {
|
||||||
|
|
||||||
return this.absHref === '/';
|
return this.absHref === '/';
|
||||||
|
@ -276,9 +319,8 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return window.ENTRY = {
|
||||||
|
get: getEntry,
|
||||||
return {
|
remove: removeEntry
|
||||||
get: getEntry
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
@ -29,9 +29,9 @@ modulejs.define('view/extended', ['_', '$', 'core/settings', 'core/resource', 'c
|
||||||
emptyTemplate = '<div class="empty l10n-empty">empty</div>',
|
emptyTemplate = '<div class="empty l10n-empty">empty</div>',
|
||||||
|
|
||||||
// updates this single entry
|
// updates this single entry
|
||||||
update = function (entry) {
|
update = function (entry, force) {
|
||||||
|
|
||||||
if (entry.$extended && entry.status && entry.$extended.data('status') === entry.status) {
|
if (!force && entry.$extended && entry.status && entry.$extended.data('status') === entry.status) {
|
||||||
return entry.$extended;
|
return entry.$extended;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,6 +125,27 @@ modulejs.define('view/extended', ['_', '$', 'core/settings', 'core/resource', 'c
|
||||||
$extended
|
$extended
|
||||||
.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) {
|
||||||
|
$ul.append(update(entry, true));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
event.sub('entry.removed', function (entry) {
|
||||||
|
|
||||||
|
if (entry.isInCurrentFolder() && entry.$extended) {
|
||||||
|
entry.$extended.remove();
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
init(entry);
|
init(entry);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue