Commit 70d780d3 authored by Phil Hughes's avatar Phil Hughes

Merge branch '38869-templates' into 'master'

Export template selector as ES6 modules

See merge request gitlab-org/gitlab-ce!15524
parents 5d9585d1 20180395
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-use-before-define, prefer-arrow-callback, no-else-return, consistent-return, prefer-template, quotes, one-var, one-var-declaration-per-line, no-unused-vars, no-return-assign, comma-dangle, quote-props, no-unused-expressions, no-sequences, object-shorthand, max-len */ /* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-use-before-define, prefer-arrow-callback, no-else-return, consistent-return, prefer-template, quotes, one-var, one-var-declaration-per-line, no-unused-vars, no-return-assign, comma-dangle, quote-props, no-unused-expressions, no-sequences, object-shorthand, max-len */
import 'vendor/jquery.waitforimages'; import 'vendor/jquery.waitforimages';
(function() { // Width where images must fits in, for 2-up this gets divided by 2
gl.ImageFile = (function() { const availWidth = 900;
var prepareFrames; const viewModes = ['two-up', 'swipe'];
// Width where images must fits in, for 2-up this gets divided by 2 export default class ImageFile {
ImageFile.availWidth = 900; constructor(file) {
ImageFile.viewModes = ['two-up', 'swipe'];
function ImageFile(file) {
this.file = file; this.file = file;
this.requestImageInfo($('.two-up.view .frame.deleted img', this.file), (function(_this) { this.requestImageInfo($('.two-up.view .frame.deleted img', this.file), (function(_this) {
return function(deletedWidth, deletedHeight) { return function(deletedWidth, deletedHeight) {
...@@ -29,9 +25,8 @@ import 'vendor/jquery.waitforimages'; ...@@ -29,9 +25,8 @@ import 'vendor/jquery.waitforimages';
})(this)); })(this));
} }
ImageFile.prototype.initViewModes = function() { initViewModes() {
var viewMode; const viewMode = viewModes[0];
viewMode = ImageFile.viewModes[0];
$('.view-modes', this.file).removeClass('hide'); $('.view-modes', this.file).removeClass('hide');
$('.view-modes-menu', this.file).on('click', 'li', (function(_this) { $('.view-modes-menu', this.file).on('click', 'li', (function(_this) {
return function(event) { return function(event) {
...@@ -41,9 +36,9 @@ import 'vendor/jquery.waitforimages'; ...@@ -41,9 +36,9 @@ import 'vendor/jquery.waitforimages';
}; };
})(this)); })(this));
return this.activateViewMode(viewMode); return this.activateViewMode(viewMode);
}; }
ImageFile.prototype.activateViewMode = function(viewMode) { activateViewMode(viewMode) {
$('.view-modes-menu li', this.file).removeClass('active').filter("." + viewMode).addClass('active'); $('.view-modes-menu li', this.file).removeClass('active').filter("." + viewMode).addClass('active');
return $(".view:visible:not(." + viewMode + ")", this.file).fadeOut(200, (function(_this) { return $(".view:visible:not(." + viewMode + ")", this.file).fadeOut(200, (function(_this) {
return function() { return function() {
...@@ -51,13 +46,13 @@ import 'vendor/jquery.waitforimages'; ...@@ -51,13 +46,13 @@ import 'vendor/jquery.waitforimages';
return _this.initView(viewMode); return _this.initView(viewMode);
}; };
})(this)); })(this));
}; }
ImageFile.prototype.initView = function(viewMode) { initView(viewMode) {
return this.views[viewMode].call(this); return this.views[viewMode].call(this);
}; }
// eslint-disable-next-line class-methods-use-this
ImageFile.prototype.initDraggable = function($el, padding, callback) { initDraggable($el, padding, callback) {
var dragging = false; var dragging = false;
var $body = $('body'); var $body = $('body');
var $offsetEl = $el.parent(); var $offsetEl = $el.parent();
...@@ -79,9 +74,9 @@ import 'vendor/jquery.waitforimages'; ...@@ -79,9 +74,9 @@ import 'vendor/jquery.waitforimages';
callback(e, left); callback(e, left);
}); });
}; }
prepareFrames = function(view) { prepareFrames(view) {
var maxHeight, maxWidth; var maxHeight, maxWidth;
maxWidth = 0; maxWidth = 0;
maxHeight = 0; maxHeight = 0;
...@@ -98,17 +93,17 @@ import 'vendor/jquery.waitforimages'; ...@@ -98,17 +93,17 @@ import 'vendor/jquery.waitforimages';
height: maxHeight height: maxHeight
}); });
return [maxWidth, maxHeight]; return [maxWidth, maxHeight];
}; }
ImageFile.prototype.views = { views = {
'two-up': function() { 'two-up': function() {
return $('.two-up.view .wrap', this.file).each((function(_this) { return $('.two-up.view .wrap', this.file).each((function(_this) {
return function(index, wrap) { return function(index, wrap) {
$('img', wrap).each(function() { $('img', wrap).each(function() {
var currentWidth; var currentWidth;
currentWidth = $(this).width(); currentWidth = $(this).width();
if (currentWidth > ImageFile.availWidth / 2) { if (currentWidth > availWidth / 2) {
return $(this).width(ImageFile.availWidth / 2); return $(this).width(availWidth / 2);
} }
}); });
return _this.requestImageInfo($('img', wrap), function(width, height) { return _this.requestImageInfo($('img', wrap), function(width, height) {
...@@ -126,7 +121,7 @@ import 'vendor/jquery.waitforimages'; ...@@ -126,7 +121,7 @@ import 'vendor/jquery.waitforimages';
return $('.swipe.view', this.file).each((function(_this) { return $('.swipe.view', this.file).each((function(_this) {
return function(index, view) { return function(index, view) {
var $swipeWrap, $swipeBar, $swipeFrame, wrapPadding, ref; var $swipeWrap, $swipeBar, $swipeFrame, wrapPadding, ref;
ref = prepareFrames(view), maxWidth = ref[0], maxHeight = ref[1]; ref = this.prepareFrames(view), maxWidth = ref[0], maxHeight = ref[1];
$swipeFrame = $('.swipe-frame', view); $swipeFrame = $('.swipe-frame', view);
$swipeWrap = $('.swipe-wrap', view); $swipeWrap = $('.swipe-wrap', view);
$swipeBar = $('.swipe-bar', view); $swipeBar = $('.swipe-bar', view);
...@@ -163,7 +158,7 @@ import 'vendor/jquery.waitforimages'; ...@@ -163,7 +158,7 @@ import 'vendor/jquery.waitforimages';
return $('.onion-skin.view', this.file).each((function(_this) { return $('.onion-skin.view', this.file).each((function(_this) {
return function(index, view) { return function(index, view) {
var $frame, $track, $dragger, $frameAdded, framePadding, ref, dragging = false; var $frame, $track, $dragger, $frameAdded, framePadding, ref, dragging = false;
ref = prepareFrames(view), maxWidth = ref[0], maxHeight = ref[1]; ref = this.prepareFrames(view), maxWidth = ref[0], maxHeight = ref[1];
$frame = $('.onion-skin-frame', view); $frame = $('.onion-skin-frame', view);
$frameAdded = $('.frame.added', view); $frameAdded = $('.frame.added', view);
$track = $('.drag-track', view); $track = $('.drag-track', view);
...@@ -194,11 +189,10 @@ import 'vendor/jquery.waitforimages'; ...@@ -194,11 +189,10 @@ import 'vendor/jquery.waitforimages';
}; };
})(this)); })(this));
} }
}; }
ImageFile.prototype.requestImageInfo = function(img, callback) { requestImageInfo(img, callback) {
var domImg; const domImg = img.get(0);
domImg = img.get(0);
if (domImg) { if (domImg) {
if (domImg.complete) { if (domImg.complete) {
return callback.call(this, domImg.naturalWidth, domImg.naturalHeight); return callback.call(this, domImg.naturalWidth, domImg.naturalHeight);
...@@ -210,8 +204,5 @@ import 'vendor/jquery.waitforimages'; ...@@ -210,8 +204,5 @@ import 'vendor/jquery.waitforimages';
})(this)); })(this));
} }
} }
}; }
}
return ImageFile;
})();
}).call(window);
...@@ -30,7 +30,7 @@ import projectImport from './project_import'; ...@@ -30,7 +30,7 @@ import projectImport from './project_import';
import Labels from './labels'; import Labels from './labels';
import LabelManager from './label_manager'; import LabelManager from './label_manager';
/* global Sidebar */ /* global Sidebar */
import IssuableTemplateSelectors from './templates/issuable_template_selectors';
import Flash from './flash'; import Flash from './flash';
import CommitsList from './commits'; import CommitsList from './commits';
import Issue from './issue'; import Issue from './issue';
...@@ -264,7 +264,7 @@ import ProjectVariables from './project_variables'; ...@@ -264,7 +264,7 @@ import ProjectVariables from './project_variables';
new IssuableForm($('.issue-form')); new IssuableForm($('.issue-form'));
new LabelsSelect(); new LabelsSelect();
new MilestoneSelect(); new MilestoneSelect();
new gl.IssuableTemplateSelectors(); new IssuableTemplateSelectors();
break; break;
case 'projects:merge_requests:creations:new': case 'projects:merge_requests:creations:new':
const mrNewCompareNode = document.querySelector('.js-merge-request-new-compare'); const mrNewCompareNode = document.querySelector('.js-merge-request-new-compare');
...@@ -288,7 +288,7 @@ import ProjectVariables from './project_variables'; ...@@ -288,7 +288,7 @@ import ProjectVariables from './project_variables';
new IssuableForm($('.merge-request-form')); new IssuableForm($('.merge-request-form'));
new LabelsSelect(); new LabelsSelect();
new MilestoneSelect(); new MilestoneSelect();
new gl.IssuableTemplateSelectors(); new IssuableTemplateSelectors();
new AutoWidthDropdownSelect($('.js-target-branch-select')).init(); new AutoWidthDropdownSelect($('.js-target-branch-select')).init();
break; break;
case 'projects:tags:new': case 'projects:tags:new':
......
import ImageBadge from '../image_badge'; import ImageBadge from '../image_badge';
import ImageDiff from '../image_diff'; import ImageDiff from '../image_diff';
import ReplacedImageDiff from '../replaced_image_diff'; import ReplacedImageDiff from '../replaced_image_diff';
import '../../commit/image_file'; import ImageFile from '../../commit/image_file';
export function resizeCoordinatesToImageElement(imageEl, meta) { export function resizeCoordinatesToImageElement(imageEl, meta) {
const { x, y, width, height } = meta; const { x, y, width, height } = meta;
...@@ -81,7 +81,7 @@ export function initImageDiff(fileEl, canCreateNote, renderCommentBadge) { ...@@ -81,7 +81,7 @@ export function initImageDiff(fileEl, canCreateNote, renderCommentBadge) {
// ImageFile needs to be invoked before initImageDiff so that badges // ImageFile needs to be invoked before initImageDiff so that badges
// can mount to the correct location // can mount to the correct location
new gl.ImageFile(fileEl); // eslint-disable-line no-new new ImageFile(fileEl); // eslint-disable-line no-new
if (fileEl.querySelector('.diff-file .js-single-image')) { if (fileEl.querySelector('.diff-file .js-single-image')) {
diff = new ImageDiff(fileEl, options); diff = new ImageDiff(fileEl, options);
......
<script> <script>
import IssuableTemplateSelectors from '../../../templates/issuable_template_selectors';
export default { export default {
props: { props: {
formState: { formState: {
...@@ -32,7 +34,7 @@ ...@@ -32,7 +34,7 @@
}; };
editor.getValue = () => this.formState.description; editor.getValue = () => this.formState.description;
this.issuableTemplate = new gl.IssuableTemplateSelectors({ this.issuableTemplate = new IssuableTemplateSelectors({
$dropdowns: $(this.$refs.toggle), $dropdowns: $(this.$refs.toggle),
editor, editor,
}); });
......
/* eslint-disable comma-dangle, max-len, no-useless-return, no-param-reassign, max-len */ /* eslint-disable no-useless-return, max-len */
import Api from '../api';
import Api from '../api';
import TemplateSelector from '../blob/template_selector'; import TemplateSelector from '../blob/template_selector';
((global) => { export default class IssuableTemplateSelector extends TemplateSelector {
class IssuableTemplateSelector extends TemplateSelector {
constructor(...args) { constructor(...args) {
super(...args); super(...args);
this.projectPath = this.dropdown.data('project-path'); this.projectPath = this.dropdown.data('project-path');
...@@ -13,7 +12,7 @@ import TemplateSelector from '../blob/template_selector'; ...@@ -13,7 +12,7 @@ import TemplateSelector from '../blob/template_selector';
this.titleInput = $(`#${this.issuableType}_title`); this.titleInput = $(`#${this.issuableType}_title`);
const initialQuery = { const initialQuery = {
name: this.dropdown.data('selected') name: this.dropdown.data('selected'),
}; };
if (initialQuery.name) this.requestFile(initialQuery); if (initialQuery.name) this.requestFile(initialQuery);
...@@ -54,7 +53,4 @@ import TemplateSelector from '../blob/template_selector'; ...@@ -54,7 +53,4 @@ import TemplateSelector from '../blob/template_selector';
} }
return; return;
} }
} }
global.IssuableTemplateSelector = IssuableTemplateSelector;
})(window.gl || (window.gl = {}));
/* eslint-disable no-new, comma-dangle, class-methods-use-this, no-param-reassign */ /* eslint-disable no-new, class-methods-use-this */
import IssuableTemplateSelector from './issuable_template_selector';
((global) => { export default class IssuableTemplateSelectors {
class IssuableTemplateSelectors {
constructor({ $dropdowns, editor } = {}) { constructor({ $dropdowns, editor } = {}) {
this.$dropdowns = $dropdowns || $('.js-issuable-selector'); this.$dropdowns = $dropdowns || $('.js-issuable-selector');
this.editor = editor || this.initEditor(); this.editor = editor || this.initEditor();
this.$dropdowns.each((i, dropdown) => { this.$dropdowns.each((i, dropdown) => {
const $dropdown = $(dropdown); const $dropdown = $(dropdown);
new gl.IssuableTemplateSelector({ new IssuableTemplateSelector({
pattern: /(\.md)/, pattern: /(\.md)/,
data: $dropdown.data('data'), data: $dropdown.data('data'),
wrapper: $dropdown.closest('.js-issuable-selector-wrap'), wrapper: $dropdown.closest('.js-issuable-selector-wrap'),
dropdown: $dropdown, dropdown: $dropdown,
editor: this.editor editor: this.editor,
}); });
}); });
} }
...@@ -25,7 +25,4 @@ ...@@ -25,7 +25,4 @@
editor.getValue = editor.val; editor.getValue = editor.val;
return editor; return editor;
} }
} }
global.IssuableTemplateSelectors = IssuableTemplateSelectors;
})(window.gl || (window.gl = {}));
---
title: Remove template selector from global namespace
merge_request:
author:
type: performance
...@@ -157,27 +157,19 @@ describe('utilsHelper', () => { ...@@ -157,27 +157,19 @@ describe('utilsHelper', () => {
beforeEach(() => { beforeEach(() => {
window.gl = window.gl || (window.gl = {}); window.gl = window.gl || (window.gl = {});
glCache = window.gl; glCache = window.gl;
window.gl.ImageFile = () => {};
fileEl = document.createElement('div'); fileEl = document.createElement('div');
fileEl.innerHTML = ` fileEl.innerHTML = `
<div class="diff-file"></div> <div class="diff-file"></div>
`; `;
spyOn(ImageDiff.prototype, 'init').and.callFake(() => {});
spyOn(ReplacedImageDiff.prototype, 'init').and.callFake(() => {}); spyOn(ReplacedImageDiff.prototype, 'init').and.callFake(() => {});
spyOn(ImageDiff.prototype, 'init').and.callFake(() => {});
}); });
afterEach(() => { afterEach(() => {
window.gl = glCache; window.gl = glCache;
}); });
it('should initialize gl.ImageFile', () => {
spyOn(window.gl, 'ImageFile');
utilsHelper.initImageDiff(fileEl, false, false);
expect(gl.ImageFile).toHaveBeenCalled();
});
it('should initialize ImageDiff if js-single-image', () => { it('should initialize ImageDiff if js-single-image', () => {
const diffFileEl = fileEl.querySelector('.diff-file'); const diffFileEl = fileEl.querySelector('.diff-file');
diffFileEl.innerHTML = ` diffFileEl.innerHTML = `
......
...@@ -34,7 +34,6 @@ describe('Inline edit form component', () => { ...@@ -34,7 +34,6 @@ describe('Inline edit form component', () => {
}); });
it('renders template selector when templates exists', (done) => { it('renders template selector when templates exists', (done) => {
spyOn(gl, 'IssuableTemplateSelectors');
vm.issuableTemplates = ['test']; vm.issuableTemplates = ['test'];
Vue.nextTick(() => { Vue.nextTick(() => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment