Skip to content

Instantly share code, notes, and snippets.

@erwan
Created March 13, 2015 11:11
Show Gist options
  • Save erwan/a15f4abf498300e0290d to your computer and use it in GitHub Desktop.
Save erwan/a15f4abf498300e0290d to your computer and use it in GitHub Desktop.
define(['events', 'communication', 'animation', 'navigation', 'utils', 'releases', 'experiments', 'dropdown'], function(Events, Communication, Animation, Navigation, Utils, Releases, Experiments, Dropdown) {
var load = function(id, versionAndBase) {
var r = /^([^~]+)(~([^~]+))?$/.exec(versionAndBase),
version = r[1];
return Communication.group(
'history',
Communication.history(id, {
setup: focus(version),
container: '#history'
}).done(
function() {
setTimeout(function() {
$('.focus').removeClass('highlight');
}, 0);
}
)
);
};
var loadPublish = function(id, versionAndBase) {
var r = /^([^~]+)(~([^~]+))?$/.exec(versionAndBase),
version = r[1];
return Communication.group(
'history',
Communication.publish(id, version, {
setup: function() {
Dropdown.from('#publish');
$('.actionSelector:not(.sub-dropdown)').change();
},
container: '#history'
}).done(function() {
$('section#history').data('document', id).data('version', version);
})
);
};
var loadPublishRelease = function(id) {
return Communication.group(
'history',
Communication.publishRelease(id, {
setup: function() {
Dropdown.from('#publish');
$('.actionSelector:not(.sub-dropdown)').change();
},
container: '#history'
}).done(function() {
$('section#history').data('release', id);
})
);
};
var reset = function() {
$('section#history').removeData('document').removeData('version').removeData('release');
$('section#history .reloadable').html('');
};
var focus = function(version) {
return function() {
if(version) {
$('#history .version[data-versionId=' + version + ']:first').addClass('focus');
$('.version.focus').parents('.intermediates').removeClass('closed');
$('.version.focus').addClass($('.version.focus .status em').attr('class')).addClass('highlight');
if($('.version.focus').size()) {
$('#history .scroller').scrollTop($('.version.focus').offset().top - 200);
}
}
};
};
// -- ACTIONS
var publicationAction = function(button, f) {
var documentId = $('#history').data('document') || $('#history').data('release'),
versionId = $('#history').data('version');
$(button).text('Please wait…').attr('disabled', 'disabled').removeClass('error');
$(Events).trigger('wio:publishing', documentId);
return Utils.withMinimumDelay(f(documentId, versionId), 500).then(function(json) {
if(json && json.newFuture) Releases.updateTheFuture(json.newFuture);
$(Events).trigger('wio:published', documentId);
Navigation.navigate('../');
}).fail(function() {
$(button).text('Error – Click to retry').addClass('error').removeAttr('disabled');
});
};
// -> PUBLISH RELEASE NOW
$('section#history').on('click', 'button[data-type=publish_release_now]', function() {
publicationAction(this, function(releaseId) {
return Communication.mergeRelease(releaseId);
});
});
// -> QUEUE RELEASE
$('section#history').on('click', 'button[data-type=publish_release_datetime]', function() {
var date = $.trim($('p.publish_release_datetime input.datep').val()),
time = $.trim($('p.publish_release_datetime input.time').val()),
datetime = date ? date + ' ' + (time || '12:00 AM') : '',
parsed = datetime ? moment(datetime, 'MM-DD-YYYY hh:mm A') : moment();
if(parsed && parsed.isValid() && parsed.isAfter(moment())) {
publicationAction(this, function(id) {
return Communication.updateReleaseDate(id, parsed.valueOf());
});
} else {
$('p.publish_release_datetime').addClass('error');
}
});
// -> CANCEL RELEASE
$('section#history').on('click', 'button[data-type=cancel_release]', function() {
publicationAction(this, function(releaseId) {
return Communication.updateReleaseDate(releaseId, 0);
});
});
// -> PUBLISH NOW
$('section#history').on('click', 'button[data-type=publish_now]', function() {
publicationAction(this, function(documentId, versionId) {
return Communication.publishNow(documentId, versionId);
});
});
// -> DEPUBLISH NOW
$('section#history').on('click', 'button[data-type=depublish_now]', function() {
publicationAction(this, function(documentId, versionId) {
return Communication.depublishNow(documentId, versionId);
});
});
// -> CANCEL QUEUED PUBLICATION
$('section#history').on('click', 'button[data-type=cancel]', function() {
publicationAction(this, function(documentId, versionId) {
return Communication.unstage(documentId, versionId);
});
});
// -> QUEUE PUBLICATION
$('section#history').on('click', 'button[data-type=publish_release]', function() {
var release = $('input.actionSelector.release').val();
if(release) {
publicationAction(this, function(documentId, versionId) {
return Communication.stageForRelease(documentId, versionId, 'ADD', release);
});
} else {
$('p.publish_release').addClass('error');
}
});
// -> QUEUE PUBLICATION IN A NEW RELEASE
$('section#history').on('click', 'button[data-type=publish_create_release]', function() {
publicationAction(this, function(documentId, versionId) {
var release = $('.publish_create_release input[type=text]').val();
return Communication.stageForNewRelease(documentId, versionId, 'ADD', release);
});
});
// -> QUEUE DEPUBLICATION
$('section#history').on('click', 'button[data-type=depublish_release]', function() {
var release = $('input.actionSelector.release').val();
if(release) {
publicationAction(this, function(documentId, versionId) {
return Communication.stageForRelease(documentId, versionId, 'DEL', release);
});
} else {
$('p.depublish_release').addClass('error');
}
});
$('section#history').on('click', 'button[data-type=depublish_variation]', function() {
var variationId = $('input.actionSelector.variation').val();
if(variationId) {
publicationAction(this, function(documentId, versionId) {
return Communication.stageInExistingVariation(documentId, versionId, 'DEL', variationId);
});
} else {
$('p.depublish_variation').addClass('error');
}
});
// -> QUEUE DEPUBLICATION IN A NEW RELEASE
$('section#history').on('click', 'button[data-type=depublish_create_release]', function() {
publicationAction(this, function(documentId, versionId) {
return Communication.stageForNewRelease(documentId, versionId, 'DEL', $('input.releaseName').val());
});
});
$('section#history').on('click', 'button[data-type=depublish_create_variation]', function() {
publicationAction(this, function(documentId, versionId) {
var variation = $('.variation-name').siblings('input').val();
return Communication.stageForNewVariation(documentId, versionId, 'DEL', null, variation);
});
});
// -> SCHEDULE PUBLICATION
$('section#history').on('click', 'button[data-type=publish_datetime]', function() {
var date = $.trim($('p.publish_datetime input.datep').val()),
time = $.trim($('p.publish_datetime input.time').val()),
datetime = date ? date + ' ' + (time || '12:00 AM') : '',
parsed = datetime ? moment(datetime, 'MM-DD-YYYY hh:mm A') : moment();
if(parsed && parsed.isValid() && parsed.isAfter(moment())) {
publicationAction(this, function(documentId, versionId) {
return Communication.stageForDate(documentId, versionId, 'ADD', parsed.valueOf());
});
} else {
$('p.publish_datetime').addClass('error');
}
});
// -> SCHEDULE DEPUBLICATION
$('section#history').on('click', 'button[data-type=depublish_datetime]', function() {
var date = $.trim($('p.depublish_datetime input.datep').val()),
time = $.trim($('p.depublish_datetime input.time').val()),
div = $(this).parents('.actions'),
datetime = date ? date + ' ' + (time || '12:00 AM') : '',
parsed = datetime ? moment(datetime, 'MM-DD-YYYY hh:mm A') : moment();
if(parsed && parsed.isValid() && parsed.isAfter(moment())) {
publicationAction(this, function(documentId, versionId) {
return Communication.stageForDate(documentId, versionId, 'DEL', parsed.valueOf());
});
} else {
$('p.depublish_datetime', div).addClass('error');
}
});
// -> QUEUE PUBLICATION IN A NEW EXPERIMENT VARIATION
$('section#history').on('click', 'button[data-type=publish_create_variation]', function() {
publicationAction(this, function(documentId, versionId) {
var variation = $('.variation-name').siblings('input').val();
return Communication.stageForNewVariation(documentId, versionId, 'ADD', null, variation);
});
});
// -> QUEUE PUBLICATION IN AN EXISTING EXPERIMENT VARIATION
$('section#history').on('click', 'button[data-type=publish_variation]', function() {
var variationId = $('input.actionSelector.variation').val();
if(variationId) {
publicationAction(this, function(documentId, versionId) {
return Communication.stageInExistingVariation(documentId, versionId, 'ADD', variationId);
});
}
});
// -> UNSTAGE CHANGES FROM EXPERIMENT VARIATION
$('section#history').on('click', 'button[data-type=cancel_from_variation]', function() {
publicationAction(this, function(documentId, versionId) {
return Communication.unstage(documentId, versionId);
});
});
// -- EVENTS
$('section#history').on('click', '.intermediates.closed', function() {
$(this).removeClass('closed');
});
$('section#history').on('change', '.actionSelector', function() {
var $input = $(this),
option = $input.val(),
div = $input.parents('.actions'),
optionEl = $('option[value="' + option + '"]', '#publish');
if(!$input.is('.sub-dropdown')) {
$('p.option', div).hide();
$('p.option', div).hide().removeClass('error');
} else {
var previous = $input.data('previous-value');
if(previous) {
div.find('> p.' + previous).hide();
}
}
$('p.option.' + (option || 'nothing'), div).show();
$('p.option.' + (option || 'nothing') + ' input[data-initialvalue]', div).each(function() {
var maybeFormat = $(this).data('format');
if(maybeFormat) {
var parsed = moment($(this).data('initialvalue') || '');
if(parsed) {
$(this).val(parsed.format(maybeFormat));
}
} else {
$(this).val($(this).data('initialvalue') || '');
}
});
var $button = $('p.buttons button');
$button.text(optionEl.data('btn-label'));
if(optionEl.is('[data-btn-value]')) {
$button.attr('data-type', optionEl.attr('data-btn-value'));
} else {
$button.attr('data-type', optionEl.val());
}
$button.removeClass('error').attr('class', 'btn btn-' + optionEl.data('btn-style'));
var releaseValues = ['publish_release', 'depublish_release'];
if(releaseValues.indexOf(optionEl.val()) != -1 && $('section#history .no-release').length) {
$('p.buttons button').attr('disabled', '');
} else {
$('p.buttons button').removeAttr('disabled');
}
if($('p.buttons button').text()) {
$('p.buttons').removeClass('empty');
} else {
$('p.buttons').addClass('empty');
}
$('.datepicker.publishdatepicker').remove();
$('p.option input.datep', div).removeData('datepicker').datepicker({
format: 'mm-dd-yyyy',
clazz: 'publishdatepicker'
}).on('changeDate', function(ev) {
$(this).data('datepicker').hide();
});
Utils.dateComponent($('p.option input.datep', div));
Utils.timeComponent($('p.option input.time', div));
});
$('section#history').on('focus', 'p.option input', function() {
$(this).parents('p.option').removeClass('error');
});
return {
load: load,
loadPublish: loadPublish,
loadPublishRelease: loadPublishRelease,
reset: reset
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment