mirror of
https://github.com/flarum/framework.git
synced 2025-05-31 04:25:49 +08:00

- Extract shared Ember components into a “flarum-common” ember-cli addon. This can be used by both the forum + admin Ember apps, keeping things DRY - Move LESS styles into their own top-level directory and do a similar thing (extract common styles) - Add LESS/JS compilation and versioning to PHP (AssetManager) - Set up admin entry point (Theoretical) upgrade instructions: - Delete everything in [app_root]/public - Set up tooling in forum/admin Ember apps (npm install/update, bower install/update) and then build them (ember build) - php artisan vendor:publish - Upgrade flarum/flarum repo (slight change in a config file) - If you need to trigger a LESS/JS recompile, delete the .css/.js files in [app_root]/public/flarum. I set up LiveReload to do this for me when I change files in less/ or ember/ Todo: - Start writing admin app! - Remove bootstrap/font-awesome from repo and instead depend on their composer packages? Maybe? (Bower is not an option here)
148 lines
3.7 KiB
JavaScript
148 lines
3.7 KiB
JavaScript
import Ember from 'ember';
|
|
|
|
import humanTime from '../utils/human-time';
|
|
|
|
var $ = Ember.$;
|
|
|
|
export default {
|
|
name: 'human-time-updater',
|
|
initialize: function(container) {
|
|
|
|
// Livestamp.js / v1.1.2 / (c) 2012 Matt Bradley / MIT License
|
|
// @todo rewrite this to be simpler and cleaner
|
|
(function($, moment) {
|
|
var updateInterval = 1e3,
|
|
paused = false,
|
|
$livestamps = $([]),
|
|
|
|
init = function() {
|
|
livestampGlobal.resume();
|
|
},
|
|
|
|
prep = function($el, timestamp) {
|
|
var oldData = $el.data('livestampdata');
|
|
if (typeof timestamp == 'number')
|
|
timestamp *= 1e3;
|
|
|
|
$el.removeAttr('data-livestamp')
|
|
.removeData('livestamp');
|
|
|
|
timestamp = moment(timestamp);
|
|
if (timestamp.diff(moment(new Date())) < 60 * 60) {
|
|
return;
|
|
}
|
|
if (moment.isMoment(timestamp) && !isNaN(+timestamp)) {
|
|
var newData = $.extend({ }, { 'original': $el.contents() }, oldData);
|
|
newData.moment = moment(timestamp);
|
|
|
|
$el.data('livestampdata', newData).empty();
|
|
$livestamps.push($el[0]);
|
|
}
|
|
},
|
|
|
|
run = function() {
|
|
if (paused) return;
|
|
livestampGlobal.update();
|
|
setTimeout(run, updateInterval);
|
|
},
|
|
|
|
livestampGlobal = {
|
|
update: function() {
|
|
$('[data-humantime]').each(function() {
|
|
var $this = $(this);
|
|
prep($this, $this.attr('datetime'));
|
|
});
|
|
|
|
var toRemove = [];
|
|
$livestamps.each(function() {
|
|
var $this = $(this),
|
|
data = $this.data('livestampdata');
|
|
|
|
if (data === undefined)
|
|
toRemove.push(this);
|
|
else if (moment.isMoment(data.moment)) {
|
|
var from = $this.html(),
|
|
to = humanTime(data.moment);
|
|
// to = data.moment.fromNow();
|
|
|
|
if (from != to) {
|
|
var e = $.Event('change.livestamp');
|
|
$this.trigger(e, [from, to]);
|
|
if (!e.isDefaultPrevented())
|
|
$this.html(to);
|
|
}
|
|
}
|
|
});
|
|
|
|
$livestamps = $livestamps.not(toRemove);
|
|
},
|
|
|
|
pause: function() {
|
|
paused = true;
|
|
},
|
|
|
|
resume: function() {
|
|
paused = false;
|
|
run();
|
|
},
|
|
|
|
interval: function(interval) {
|
|
if (interval === undefined)
|
|
return updateInterval;
|
|
updateInterval = interval;
|
|
}
|
|
},
|
|
|
|
livestampLocal = {
|
|
add: function($el, timestamp) {
|
|
if (typeof timestamp == 'number')
|
|
timestamp *= 1e3;
|
|
timestamp = moment(timestamp);
|
|
|
|
if (moment.isMoment(timestamp) && !isNaN(+timestamp)) {
|
|
$el.each(function() {
|
|
prep($(this), timestamp);
|
|
});
|
|
livestampGlobal.update();
|
|
}
|
|
|
|
return $el;
|
|
},
|
|
|
|
destroy: function($el) {
|
|
$livestamps = $livestamps.not($el);
|
|
$el.each(function() {
|
|
var $this = $(this),
|
|
data = $this.data('livestampdata');
|
|
|
|
if (data === undefined)
|
|
return $el;
|
|
|
|
$this
|
|
.html(data.original ? data.original : '')
|
|
.removeData('livestampdata');
|
|
});
|
|
|
|
return $el;
|
|
},
|
|
|
|
isLivestamp: function($el) {
|
|
return $el.data('livestampdata') !== undefined;
|
|
}
|
|
};
|
|
|
|
$.livestamp = livestampGlobal;
|
|
$(init);
|
|
$.fn.livestamp = function(method, options) {
|
|
if (!livestampLocal[method]) {
|
|
options = method;
|
|
method = 'add';
|
|
}
|
|
|
|
return livestampLocal[method](this, options);
|
|
};
|
|
})(jQuery, moment);
|
|
|
|
}
|
|
};
|