<commit_before>/*
 * Copyright (c) 2016
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */

(function() {
	if (!OC.Share) {
		OC.Share = {};
	}

	var TEMPLATE =
		'<form id="emailPrivateLink" class="emailPrivateLinkForm">' +
		'  <span class="emailPrivateLinkForm--send-indicator success-message-global absolute-center hidden">{{sending}}</span>' +
		'  <label class="public-link-modal--label" for="emailPrivateLinkField-{{cid}}">{{mailLabel}}</label>' +
		'  <input class="emailPrivateLinkForm--emailField full-width" id="emailPrivateLinkField-{{cid}}" />' +
		'  <div class="emailPrivateLinkForm--elements hidden">' +
		'    {{#if userHasEmail}}' +
		'    <label class="public-link-modal--bccSelf">' +
		'      <input class="emailPrivateLinkForm--emailBccSelf" type="checkbox"> {{bccSelf}}' +
		'    </label>' +
		'    {{/if}}' +
		'    <label class="public-link-modal--label" for="emailBodyPrivateLinkField-{{cid}}">{{mailMessageLabel}}</label>' +
		'    <textarea class="public-link-modal--input emailPrivateLinkForm--emailBodyField" id="emailBodyPrivateLinkField-{{cid}}" rows="3" placeholder="{{mailBodyPlaceholder}}"></textarea>' +
		'  </div>' +
		'</form>';

	/**
	 * @class OCA.Share.ShareDialogMailView
	 * @member {OC.Share.ShareItemModel} model
	 * @member {jQuery} $el
	 * @memberof OCA.Sharing
	 * @classdesc
	 *
	 * Represents the GUI of the share dialogue
	 *
	 */
	var ShareDialogMailView = OC.Backbone.View.extend({
		/** @type {string} **/
		id: 'shareDialogMailView',

		events: {
			"keydown .emailPrivateLinkForm--emailBodyField" : "expandMailBody"
		},

		/** @type {array} **/
		_addresses: [],

		/** @type {Function} **/
		_template: undefined,

		initialize: function(options) {
			_.bindAll(this, 'render', '_afterRender');
			var _this = this;
			this.render = _.wrap(this.render, function(render) {
				render();
				_this._afterRender();
				return _this;
			});

			if (!_.isUndefined(options.itemModel)) {
				this.itemModel = options.itemModel;
			} else {
				throw 'missing OC.Share.ShareItemModel';
			}
		},

		toggleMailElements: function() {
			var $emailElements = this.$el.find('.emailPrivateLinkForm--elements');

			if (this._addresses.length > 0 && $emailElements.is(":hidden")) {
				$emailElements.slideDown();
			} else if (this._addresses.length === 0 && $emailElements.is(":visible")) {
				$emailElements.slideUp();
			}
		},

		expandMailBody: function(event) {
			var $emailBody = this.$el.find('.emailPrivateLinkForm--emailBodyField');
			$emailBody.css('minHeight', $emailBody[0].scrollHeight - 12);

			if (event.keyCode == 13) {
				event.stopPropagation();
			}
		},

		/**
		 * Send the link share information by email
		 *
		 * @param {string} recipientEmail recipient email address
		 */
		_sendEmailPrivateLink: function(mail) {
			var deferred   = $.Deferred();
			var itemType   = this.itemModel.get('itemType');
			var itemSource = this.itemModel.get('itemSource');

			var params = {
				action      : 'email',
				toAddress   : this._addresses.join(','),
				emailBody   : mail.body,
				bccSelf     : mail.bccSelf,
				link        : this.model.getLink(),
				itemType    : itemType,
				itemSource  : itemSource,
				file        : this.itemModel.getFileInfo().get('name'),
				expiration  : this.model.get('expireDate') || ''
			};

			$.post(
				OC.generateUrl('core/ajax/share.php'), params,
				function(result) {
					if (!result || result.status !== 'success') {
						deferred.reject({
							message: result.data.message
						});
					} else {
						deferred.resolve();
					}
			}).fail(function(error) {
				return deferred.reject();
			});

			return deferred.promise();
		},

		validateEmail: function(email) {
			if (email.length === 0)
				return true

			return email.match(/([\w\.\-_]+)?\w+@[\w-_]+(\.\w+){1,}$/);
		},

		sendEmails: function() {
			var $formItems         = this.$el.find('.emailPrivateLinkForm input, .emailPrivateLinkForm textarea');
			var $formSendIndicator = this.$el.find('.emailPrivateLinkForm--send-indicator');
			var  mail = {
				 to      : this._addresses.join(','),
				 bccSelf : this.$el.find('.emailPrivateLinkForm--emailBccSelf').is(':checked'),
				 body    : this.$el.find('.emailPrivateLinkForm--emailBodyField').val()
			};

			if (mail.to !== '') {
				$formItems.prop('disabled', true);
				$formSendIndicator.removeClass('hidden');
				return this._sendEmailPrivateLink(mail).done(function() {
					setTimeout(function() {
						$formItems.prop('disabled', false);
						$formSendIndicator.addClass('hidden');
					}, 2000);
				}).fail(function(error) {
					OC.dialogs.info(error.message, t('core', 'An error occured'));
					$formSendIndicator.addClass('hidden');
					$formItems.prop('disabled', false);
				});
			}
			return $.Deferred().resolve();
		},

		render: function() {
			// make sure this is empty
			this._addresses = [];

			this.$el.html(this.template({
				cid                 : this.cid,
				userHasEmail        : !!OC.getCurrentUser().email,
				mailPlaceholder     : t('core', 'Email link to person'),
				bccSelf             : t('core', 'Send copy to self'),
				mailLabel           : t('core', 'Send link via email'),
				mailBodyPlaceholder : t('core', 'Add personal message'),
				sending             : t('core', 'Sending') + ' ...'
			}));

			this.delegateEvents();
			return this;

		},

		_afterRender: function () {
			var _this = this;

			this.$el.find('.emailPrivateLinkForm--emailField').select2({
				containerCssClass: 'emailPrivateLinkForm--dropDown',
				tags: true,
				tokenSeparators:[","],
				query: function(query) {
					// directly from search
					query.callback({
						results: [{
							"id"       : query.term,
							"text"     : query.term,
							"disabled" : !_this.validateEmail(query.term)
						}]
					});
				}
			}).on("change", function(e) {
				if (e.added)
					_this._addAddress(e.added.id);

				if (e.removed)
					_this._removeAddress(e.removed.id);

				_this.toggleMailElements();
			});
		},

		_addAddress: function( email ) {
			this._addresses.push( email.toLowerCase() )
		},<commit_msg>Email autocomplete for link shares<commit_after>/*
 * Copyright (c) 2016
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */

(function() {
	if (!OC.Share) {
		OC.Share = {};
	}

	var TEMPLATE =
		'<form id="emailPrivateLink" class="emailPrivateLinkForm">' +
		'  <span class="emailPrivateLinkForm--send-indicator success-message-global absolute-center hidden">{{sending}}</span>' +
		'  <label class="public-link-modal--label" for="emailPrivateLinkField-{{cid}}">{{mailLabel}}</label>' +
		'  <input class="emailPrivateLinkForm--emailField full-width" id="emailPrivateLinkField-{{cid}}" />' +
		'  <div class="emailPrivateLinkForm--elements hidden">' +
		'    {{#if userHasEmail}}' +
		'    <label class="public-link-modal--bccSelf">' +
		'      <input class="emailPrivateLinkForm--emailBccSelf" type="checkbox"> {{bccSelf}}' +
		'    </label>' +
		'    {{/if}}' +
		'    <label class="public-link-modal--label" for="emailBodyPrivateLinkField-{{cid}}">{{mailMessageLabel}}</label>' +
		'    <textarea class="public-link-modal--input emailPrivateLinkForm--emailBodyField" id="emailBodyPrivateLinkField-{{cid}}" rows="3" placeholder="{{mailBodyPlaceholder}}"></textarea>' +
		'  </div>' +
		'</form>';

	/**
	 * @class OCA.Share.ShareDialogMailView
	 * @member {OC.Share.ShareItemModel} model
	 * @member {jQuery} $el
	 * @memberof OCA.Sharing
	 * @classdesc
	 *
	 * Represents the GUI of the share dialogue
	 *
	 */
	var ShareDialogMailView = OC.Backbone.View.extend({
		/** @type {string} **/
		id: 'shareDialogMailView',

		events: {
			"keydown .emailPrivateLinkForm--emailBodyField" : "expandMailBody"
		},

		/** @type {array} **/
		_addresses: [],

		/** @type {Function} **/
		_template: undefined,

		initialize: function(options) {
			_.bindAll(this, 'render', '_afterRender');
			var _this = this;
			this.render = _.wrap(this.render, function(render) {
				render();
				_this._afterRender();
				return _this;
			});

			if (!_.isUndefined(options.itemModel)) {
				this.itemModel = options.itemModel;
			} else {
				throw 'missing OC.Share.ShareItemModel';
			}
		},

		toggleMailElements: function() {
			var $emailElements = this.$el.find('.emailPrivateLinkForm--elements');

			if (this._addresses.length > 0 && $emailElements.is(":hidden")) {
				$emailElements.slideDown();
			} else if (this._addresses.length === 0 && $emailElements.is(":visible")) {
				$emailElements.slideUp();
			}
		},

		expandMailBody: function(event) {
			var $emailBody = this.$el.find('.emailPrivateLinkForm--emailBodyField');
			$emailBody.css('minHeight', $emailBody[0].scrollHeight - 12);

			if (event.keyCode == 13) {
				event.stopPropagation();
			}
		},

		/**
		 * Send the link share information by email
		 *
		 * @param {string} recipientEmail recipient email address
		 */
		_sendEmailPrivateLink: function(mail) {
			var deferred   = $.Deferred();
			var itemType   = this.itemModel.get('itemType');
			var itemSource = this.itemModel.get('itemSource');

			var params = {
				action      : 'email',
				toAddress   : this._addresses.join(','),
				emailBody   : mail.body,
				bccSelf     : mail.bccSelf,
				link        : this.model.getLink(),
				itemType    : itemType,
				itemSource  : itemSource,
				file        : this.itemModel.getFileInfo().get('name'),
				expiration  : this.model.get('expireDate') || ''
			};

			$.post(
				OC.generateUrl('core/ajax/share.php'), params,
				function(result) {
					if (!result || result.status !== 'success') {
						deferred.reject({
							message: result.data.message
						});
					} else {
						deferred.resolve();
					}
			}).fail(function(error) {
				return deferred.reject();
			});

			return deferred.promise();
		},

		validateEmail: function(email) {
			if (email.length === 0)
				return true

			return email.match(/([\w\.\-_]+)?\w+@[\w-_]+(\.\w+){1,}$/);
		},

		sendEmails: function() {
			var $formItems         = this.$el.find('.emailPrivateLinkForm input, .emailPrivateLinkForm textarea');
			var $formSendIndicator = this.$el.find('.emailPrivateLinkForm--send-indicator');
			var  mail = {
				 to      : this._addresses.join(','),
				 bccSelf : this.$el.find('.emailPrivateLinkForm--emailBccSelf').is(':checked'),
				 body    : this.$el.find('.emailPrivateLinkForm--emailBodyField').val()
			};

			if (mail.to !== '') {
				$formItems.prop('disabled', true);
				$formSendIndicator.removeClass('hidden');
				return this._sendEmailPrivateLink(mail).done(function() {
					setTimeout(function() {
						$formItems.prop('disabled', false);
						$formSendIndicator.addClass('hidden');
					}, 2000);
				}).fail(function(error) {
					OC.dialogs.info(error.message, t('core', 'An error occured'));
					$formSendIndicator.addClass('hidden');
					$formItems.prop('disabled', false);
				});
			}
			return $.Deferred().resolve();
		},

		render: function() {
			// make sure this is empty
			this._addresses = [];

			this.$el.html(this.template({
				cid                 : this.cid,
				userHasEmail        : !!OC.getCurrentUser().email,
				mailPlaceholder     : t('core', 'Email link to person'),
				bccSelf             : t('core', 'Send copy to self'),
				mailLabel           : t('core', 'Send link via email'),
				mailBodyPlaceholder : t('core', 'Add personal message'),
				sending             : t('core', 'Sending') + ' ...'
			}));

			this.delegateEvents();
			return this;

		},

		_afterRender: function () {
			var _this = this;

			this.$el.find('.emailPrivateLinkForm--emailField').select2({
				containerCssClass: 'emailPrivateLinkForm--dropDown',
				tags: true,
				tokenSeparators:[","],
/*
				query: function(query) {
					// directly from search
					query.callback({
						results: [{
							"id"       : query.term,
							"text"     : query.term,
							"disabled" : !_this.validateEmail(query.term)
						}]
					});
				},
*/
				minimumInputLength: OC.getCapabilities().files_sharing.search_min_length,
				ajax: {
					url: OC.generateUrl('core/ajax/share.php?fetch=getShareWithEmail'),
					dataType: 'json',
					quietMillis: 250,
					data: function (term) {
						return {
							search: term
						};
					},
					results: function (data, page, query) {

						if (data.status !== 'success') {
							return null;
						}

						// format results
						var fromQuery = (query.term.length) ? [{ id: query.term, text: query.term }] : [];
						var fromData  = _.map(data.data, function(item) {
							return {
								'id'   : item.email,
								'text' : item.displayname + ' (' + item.email + ')'
							};
						});

						return {
							results: fromQuery.concat(fromData)
						};
					},
					cache: true
			   }
			}).on("change", function(e) {
				if (e.added)
					_this._addAddress(e.added.id);

				if (e.removed)
					_this._removeAddress(e.removed.id);

				_this.toggleMailElements();
			});
		},

		_addAddress: function( email ) {
			this._addresses.push( email.toLowerCase() )
		},
{'fraction_non_alphanumeric': 0.17490321111364154, 'fraction_numerical': 0.0032642526379716087, 'mean_word_length': 2.980060422960725, 'pattern_counts': {'":': 4, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6061968', 'n_tokens_mistral': 5207, 'n_tokens_neox': 4185, 'n_words': 747}