1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 08:52:39 +01: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' => 'javelin-behavior-countdown-timer' =>
array( array(
'uri' => '/res/5ee9cb13/rsrc/js/application/countdown/timer.js', 'uri' => '/res/7468acb7/rsrc/js/application/countdown/timer.js',
'type' => 'js', 'type' => 'js',
'requires' => 'requires' =>
array( array(

View file

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

View file

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

View file

@ -411,6 +411,8 @@ final class PhabricatorMarkupEngine {
$rules[] = new PhabricatorRemarkupRuleManiphest(); $rules[] = new PhabricatorRemarkupRuleManiphest();
$rules[] = new PhabricatorRemarkupRulePaste(); $rules[] = new PhabricatorRemarkupRulePaste();
$rules[] = new PhabricatorRemarkupRuleCountdown();
$rules[] = new PonderRuleQuestion(); $rules[] = new PonderRuleQuestion();
if ($options['macros']) { 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) { JX.behavior('countdown-timer', function(config) {
var container = JX.$(config.container);
calculateTimeLeft(); calculateTimeLeft();
function setComponent(which, content) {
var component = JX.DOM.find(container, '*', 'phabricator-timer-' + which);
JX.DOM.setContent(component, content);
}
function calculateTimeLeft() { function calculateTimeLeft() {
var days = 0; var days = 0;
var hours = 0; var hours = 0;
@ -18,31 +24,25 @@ JX.behavior('countdown-timer', function(config) {
var current_timestamp = Math.round(new Date() / 1000); var current_timestamp = Math.round(new Date() / 1000);
var delta = config.timestamp - current_timestamp; var delta = config.timestamp - current_timestamp;
if (delta <= 0) { if (delta > 0) {
JX.DOM.setContent(JX.$('phabricator-timer-days'), days); days = Math.floor(delta/86400);
JX.DOM.setContent(JX.$('phabricator-timer-hours'), hours); delta -= days * 86400;
JX.DOM.setContent(JX.$('phabricator-timer-minutes'), minutes);
JX.DOM.setContent(JX.$('phabricator-timer-seconds'), seconds); hours = Math.floor(delta/3600);
return; delta -= hours * 3600;
minutes = Math.floor(delta / 60);
delta -= minutes * 60;
seconds = delta;
setTimeout(calculateTimeLeft, 1000);
} }
days = Math.floor(delta/86400); setComponent('days', days);
delta -= days * 86400; setComponent('hours', hours);
setComponent('minutes', minutes);
hours = Math.floor(delta/3600); setComponent('seconds', seconds);
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);
} }
}); });