1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-20 09:18:48 +02:00

Make countdowns (internally) embeddable

Summary:
You can now embed countdowns in Remarkup! Not sure what it's
useful for, but there you have it.

Also I may have made a hash of the markup code; I don't really know what
I'm doing.

Test Plan: Make a new countdown, put `{C###}` in a Differential comment.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T1053

Differential Revision: https://secure.phabricator.com/D3290
This commit is contained in:
Alan Huang 2012-08-14 19:19:23 -07:00
parent d45855e33e
commit f736ca047a
6 changed files with 126 additions and 31 deletions

View file

@ -969,7 +969,7 @@ celerity_register_resource_map(array(
),
'javelin-behavior-countdown-timer' =>
array(
'uri' => '/res/5ee9cb13/rsrc/js/application/countdown/timer.js',
'uri' => '/res/7468acb7/rsrc/js/application/countdown/timer.js',
'type' => 'js',
'requires' =>
array(

View file

@ -928,6 +928,7 @@ phutil_register_library_map(array(
'PhabricatorQuery' => 'infrastructure/query/PhabricatorQuery.php',
'PhabricatorRedirectController' => 'applications/base/controller/PhabricatorRedirectController.php',
'PhabricatorRefreshCSRFController' => 'applications/auth/controller/PhabricatorRefreshCSRFController.php',
'PhabricatorRemarkupRuleCountdown' => 'infrastructure/markup/rule/PhabricatorRemarkupRuleCountdown.php',
'PhabricatorRemarkupRuleDifferential' => 'infrastructure/markup/rule/PhabricatorRemarkupRuleDifferential.php',
'PhabricatorRemarkupRuleDifferentialHandle' => 'infrastructure/markup/rule/handle/PhabricatorRemarkupRuleDifferentialHandle.php',
'PhabricatorRemarkupRuleDiffusion' => 'infrastructure/markup/rule/PhabricatorRemarkupRuleDiffusion.php',
@ -2015,6 +2016,7 @@ phutil_register_library_map(array(
'PhabricatorProjectUpdateController' => 'PhabricatorProjectController',
'PhabricatorRedirectController' => 'PhabricatorController',
'PhabricatorRefreshCSRFController' => 'PhabricatorAuthController',
'PhabricatorRemarkupRuleCountdown' => 'PhutilRemarkupRule',
'PhabricatorRemarkupRuleDifferential' => 'PhabricatorRemarkupRuleObjectName',
'PhabricatorRemarkupRuleDifferentialHandle' => 'PhabricatorRemarkupRuleObjectHandle',
'PhabricatorRemarkupRuleDiffusion' => 'PhutilRemarkupRule',

View file

@ -47,8 +47,9 @@ final class PhabricatorCountdownViewController
),
$chrome_visible ? 'Disable Chrome' : 'Enable Chrome');
$container = celerity_generate_unique_node_id();
$content =
'<div class="phabricator-timer">
'<div class="phabricator-timer" id="'.$container.'">
<h1 class="phabricator-timer-header">'.
phutil_escape_html($timer->getTitle()).' &middot; '.
phabricator_datetime($timer->getDatePoint(), $user).
@ -61,18 +62,24 @@ final class PhabricatorCountdownViewController
<th>Minutes</th>
<th>Seconds</th>
</tr>
<tr>
<td id="phabricator-timer-days"></td>
<td id="phabricator-timer-hours"></td>
<td id="phabricator-timer-minutes"></td>
<td id="phabricator-timer-seconds"></td>
<tr>'.
javelin_render_tag('td',
array('sigil' => 'phabricator-timer-days'), '').
javelin_render_tag('td',
array('sigil' => 'phabricator-timer-hours'), '').
javelin_render_tag('td',
array('sigil' => 'phabricator-timer-minutes'), '').
javelin_render_tag('td',
array('sigil' => 'phabricator-timer-seconds'), '').
'</tr>
</table>
</div>'.
$chrome_link.
'</div>';
Javelin::initBehavior('countdown-timer', array(
'timestamp' => $timer->getDatepoint()
'timestamp' => $timer->getDatepoint(),
'container' => $container,
));
$panel = $content;

View file

@ -411,6 +411,8 @@ final class PhabricatorMarkupEngine {
$rules[] = new PhabricatorRemarkupRuleManiphest();
$rules[] = new PhabricatorRemarkupRulePaste();
$rules[] = new PhabricatorRemarkupRuleCountdown();
$rules[] = new PonderRuleQuestion();
if ($options['macros']) {

View file

@ -0,0 +1,84 @@
<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @group markup
*/
final class PhabricatorRemarkupRuleCountdown extends PhutilRemarkupRule {
const KEY_RULE_COUNTDOWN = 'rule.countdown';
public function apply($text) {
return preg_replace_callback(
"@\B{C(\d+)}\B@",
array($this, 'markupCountdown'),
$text);
}
private function markupCountdown($matches) {
$countdown = id(new PhabricatorTimer())->load($matches[1]);
if (!$countdown) {
return $matches[0];
}
$id = celerity_generate_unique_node_id();
$engine = $this->getEngine();
$token = $engine->storeText('');
$metadata_key = self::KEY_RULE_COUNTDOWN;
$metadata = $engine->getTextMetadata($metadata_key, array());
$metadata[$id] = array($countdown->getDatepoint(), $token);
$engine->setTextMetadata($metadata_key, $metadata);
return $token;
}
public function didMarkupText() {
$engine = $this->getEngine();
$metadata_key = self::KEY_RULE_COUNTDOWN;
$metadata = $engine->getTextMetadata($metadata_key, array());
require_celerity_resource('javelin-behavior-countdown-timer');
foreach ($metadata as $id => $info) {
list($time, $token) = $info;
$count = phutil_render_tag(
'span',
array(
'id' => $id,
),
javelin_render_tag('span',
array('sigil' => 'phabricator-timer-days'), '').'d'.
javelin_render_tag('span',
array('sigil' => 'phabricator-timer-hours'), '').'h'.
javelin_render_tag('span',
array('sigil' => 'phabricator-timer-minutes'), '').'m'.
javelin_render_tag('span',
array('sigil' => 'phabricator-timer-seconds'), '').'s');
Javelin::initBehavior('countdown-timer', array(
'timestamp' => $time,
'container' => $id,
));
$engine->overwriteStoredText($token, $count);
}
$engine->setTextMetadata($metadata_key, array());
}
}

View file

@ -7,8 +7,14 @@
JX.behavior('countdown-timer', function(config) {
var container = JX.$(config.container);
calculateTimeLeft();
function setComponent(which, content) {
var component = JX.DOM.find(container, '*', 'phabricator-timer-' + which);
JX.DOM.setContent(component, content);
}
function calculateTimeLeft() {
var days = 0;
var hours = 0;
@ -18,31 +24,25 @@ JX.behavior('countdown-timer', function(config) {
var current_timestamp = Math.round(new Date() / 1000);
var delta = config.timestamp - current_timestamp;
if (delta <= 0) {
JX.DOM.setContent(JX.$('phabricator-timer-days'), days);
JX.DOM.setContent(JX.$('phabricator-timer-hours'), hours);
JX.DOM.setContent(JX.$('phabricator-timer-minutes'), minutes);
JX.DOM.setContent(JX.$('phabricator-timer-seconds'), seconds);
return;
if (delta > 0) {
days = Math.floor(delta/86400);
delta -= days * 86400;
hours = Math.floor(delta/3600);
delta -= hours * 3600;
minutes = Math.floor(delta / 60);
delta -= minutes * 60;
seconds = delta;
setTimeout(calculateTimeLeft, 1000);
}
days = Math.floor(delta/86400);
delta -= days * 86400;
hours = Math.floor(delta/3600);
delta -= hours * 3600;
minutes = Math.floor(delta / 60);
delta -= minutes * 60;
seconds = delta;
JX.DOM.setContent(JX.$('phabricator-timer-days'), days);
JX.DOM.setContent(JX.$('phabricator-timer-hours'), hours);
JX.DOM.setContent(JX.$('phabricator-timer-minutes'), minutes);
JX.DOM.setContent(JX.$('phabricator-timer-seconds'), seconds);
setTimeout(calculateTimeLeft, 1000);
setComponent('days', days);
setComponent('hours', hours);
setComponent('minutes', minutes);
setComponent('seconds', seconds);
}
});