pip-webui
Version:
HTML5 UI for LOB applications
129 lines (110 loc) • 5.36 kB
JavaScript
/**
* @file Markdown control
* @copyright Digital Living Software Corp. 2014-2016
* @todo
* - Move css styles under control
* - Improve samples in sampler app
*/
(function (angular, marked, _) {
'use strict';
var thisModule = angular.module('pipMarkdown', ['ngSanitize', 'pipUtils', 'pipTranslate']);
/* eslint-disable quote-props */
thisModule.config(function (pipTranslateProvider) {
pipTranslateProvider.translations('en', {
'MARKDOWN_ATTACHMENTS': 'Attachments:',
'checklist': 'Checklist',
'documents': 'Documents',
'pictures': 'Pictures',
'location': 'Location',
'time': 'Time'
});
pipTranslateProvider.translations('ru', {
'MARKDOWN_ATTACHMENTS': 'Вложения:',
'checklist': 'Список',
'documents': 'Документы',
'pictures': 'Изображения',
'location': 'Местонахождение',
'time': 'Время'
});
});
/* eslint-enable quote-props */
thisModule.directive('pipMarkdown',
function ($parse, pipUtils, pipTranslate) {
return {
restrict: 'EA',
scope: false,
link: function ($scope, $element, $attrs) {
var
textGetter = $parse($attrs.pipText),
listGetter = $parse($attrs.pipList),
clampGetter = $parse($attrs.pipLineCount);
function describeAttachments(array) {
var attachString = '',
attachTypes = [];
_.each(array, function (attach) {
if (attach.type && attach.type !== 'text') {
if (attachString.length === 0) {
attachString = pipTranslate.translate('MARKDOWN_ATTACHMENTS');
}
if (attachTypes.indexOf(attach.type) < 0) {
attachTypes.push(attach.type);
attachString += attachTypes.length > 1 ? ', ' : ' ';
attachString += pipTranslate.translate(attach.type);
}
}
});
return attachString;
}
function bindText(value) {
var textString, isClamped, height, options, obj;
if (_.isArray(value)) {
obj = _.find(value, function (item) {
return item.type === 'text' && item.text;
});
textString = obj ? obj.text : describeAttachments(value);
} else {
textString = value;
}
isClamped = $attrs.pipLineCount && _.isNumber(clampGetter());
isClamped = isClamped && textString && textString.length > 0;
options = {
gfm: true,
tables: true,
breaks: true,
sanitize: true,
pedantic: true,
smartLists: true,
smartypents: false
};
textString = marked(textString || '', options);
if (isClamped) {
height = 1.5 * clampGetter();
}
// Assign value as HTML
$element.html('<div' + (isClamped ? listGetter() ? 'class="pip-markdown-content ' +
'pip-markdown-list" style="max-height: ' + height + 'em">'
: ' class="pip-markdown-content" style="max-height: ' + height + 'em">' : listGetter()
? ' class="pip-markdown-list">' : '>') + textString + '</div>');
$element.find('a').attr('target', 'blank');
if (!listGetter() && isClamped) {
$element.append('<div class="pip-gradient-block"></div>');
}
}
// Fill the text
bindText(textGetter($scope));
// Also optimization to avoid watch if it is unnecessary
if (pipUtils.toBoolean($attrs.pipRebind)) {
$scope.$watch(textGetter, function (newValue) {
bindText(newValue);
});
}
$scope.$on('pipWindowResized', function () {
bindText(textGetter($scope));
});
// Add class
$element.addClass('pip-markdown');
}
};
}
);
})(window.angular, window.marked, window._);