').text(message).html();
showConfirm(message_html, load_fn, l('WEB_LEAVE_PAGE', 'Leave'));
return false;
} else {
load_fn();
return true;
}
},
previewPopup: function() {
var state = Aj.state;
if (!state.previewData) {
return false;
}
var $previewPopup = $('');
state.$previewPopup = $previewPopup;
var $form = state.$form;
var text = $form.field('text').value();
var promote_url = $form.field('promote_url').value();
var button = $form.field('button').data('value');
var website_name = $form.field('website_name').value();
var website_photo = $form.field('website_photo').value();
var picture_checked = $form.field('picture').prop('checked');
var previewPictureChange = function() {
$('.js-preview', $previewPopup).toggleClass('picture', !!$(this).prop('checked'));
};
var website_name_hidden = $('.js-website-name-wrap', $form).isSlideHidden();
var custom_button_hidden = $('.js-custom-button-wrap', $form).isSlideHidden();
var $previewForm = $('.pr-new-form', $previewPopup);
Ads.formInit($previewForm);
$previewForm.on('submit', preventDefault);
state.previewTextField = $previewForm.field('text');
state.previewTextField.on('change.curPage', NewAd.onTextChange);
state.previewTextField.value(text);
state.previewTextField.on('input.curPage', NewAd.onTextInput);
state.previewPromoteUrlField = $previewForm.field('promote_url');
state.previewPromoteUrlField.on('change.curPage', NewAd.onPromoteUrlChange);
state.previewPromoteUrlField.value(promote_url);
state.previewWebsiteNameField = $previewForm.field('website_name');
state.previewWebsiteNameField.on('change.curPage', NewAd.onWebsiteNameChange);
state.previewWebsiteNameField.value(website_name);
state.previewWebsitePhotoField = $previewForm.field('website_photo');
state.previewWebsitePhotoField.value(website_photo);
state.previewButtonField = $previewForm.field('button');
state.previewButtonField.on('ddchange.curPage', NewAd.onButtonChange);
state.previewButtonField.trigger('selectval', [button]);
state.previewPictureCheckbox = $previewForm.field('picture');
state.previewPictureCheckbox.on('change.curPage', previewPictureChange);
state.previewPictureCheckbox.prop('checked', picture_checked);
$('.js-preview', $previewPopup).toggleClass('picture', !!picture_checked);
$('.js-website-name-wrap', $previewPopup).toggleClass('shide', website_name_hidden);
$('.js-custom-button-wrap', $previewPopup).toggleClass('shide', custom_button_hidden);
NewAd.updateAdPreview($previewForm, state.previewData);
NewAd.adPostCheck($previewForm);
var previewSave = function() {
var text = state.previewTextField.value();
var promote_url = state.previewPromoteUrlField.value();
var button = state.previewButtonField.data('value');
var website_name = state.previewWebsiteNameField.value();
var website_photo = state.previewWebsitePhotoField.value();
var picture_checked = state.previewPictureCheckbox.prop('checked');
$form.field('text').value(text).updateAutosize();
$form.field('promote_url').value(promote_url);
$form.field('button').trigger('selectval', [button]);
$form.field('website_name').value(website_name);
$form.field('website_photo').value(website_photo);
$form.field('picture').prop('checked', picture_checked);
NewAd.updateAdPreview($form, state.popupPreviewData);
NewAd.adPostCheck($form);
delete state.popupPreviewData;
delete state.$previewPopup;
state.initPreviewFormData = NewAd.getPreviewFormData();
closePopup($previewPopup);
}
var $submitBtn = $('.submit-form-btn', $previewPopup);
$submitBtn.on('click', previewSave);
var previewCancel = function() {
closePopup($previewPopup);
}
var $cancelBtn = $('.cancel-form-btn', $previewPopup);
$cancelBtn.on('click', previewCancel);
$previewPopup.one('popup:open', function() {
$('.pr-preview-ad-message .ad-msg-date', $previewPopup).each(function() {
Ads.updateTextShadow(this, '.ad-msg-text', '.label', 10);
});
state.previewTextField.updateAutosize();
state.initPreviewFormData = NewAd.getPreviewFormData();
});
$previewPopup.one('popup:close', function() {
Ads.formDestroy($previewForm);
$previewForm.off('submit', preventDefault);
delete state.$previewPopup;
$submitBtn.off('click', previewSave);
$cancelBtn.off('click', previewCancel);
$previewPopup.remove();
state.initPreviewFormData = NewAd.getPreviewFormData();
});
openPopup($previewPopup, {
closeByClickOutside: '.popup-no-close',
onBeforeClose: function($popup) {
var unloaded = NewAd.checkBeforePreviewPopupUnload(function() {
var options = $popup.data('options');
options.onBeforeClose = null;
closePopup($popup);
});
return unloaded;
}
});
return $previewPopup;
},
isAudienceTargetOnly: function(len) {
if (!len.locations &&
!len.countries &&
!len.user_langs &&
!len.user_topics &&
!len.exclude_user_topics &&
len.audiences > 0) {
return true;
}
return false;
},
updateAdTargetOverview: function() {
var len = {}, lang_params = {}, need_outside_cb = false;
var target_type = Aj.state.$form.field('target_type').value();
var joinTargets = function(list, or, repeat) {
var lk = or ? 'WEB_AD_TARGET_OR' : 'WEB_AD_TARGET_AND';
if (repeat && list.length > 2) {
var last_item = list.pop();
return l(lk, {item1: joinTargets(list, or, repeat), item2: last_item});
}
if (list.length > 1) {
var last_item = list.pop();
list[list.length - 1] = l(lk, {item1: list[list.length - 1], item2: last_item});
}
return list.join(', ');
};
for (var i = 0; i < Aj.state.selectList.length; i++) {
var selectData = Aj.state.selectList[i];
var field = selectData.field;
var $field = Aj.state.$form.field(field);
var value = $field.data('value') || [];
var valueFull = $field.data('valueFull') || {};
len[field] = value.length;
if (value.length) {
var list = [];
for (var j = 0; j < value.length; j++) {
var val = value[j], valFull = valueFull[val] || {};
list.push(valFull.username ? '
' + valFull.name + '' : '
' + valFull.name + '');
if (valFull.ask_outside) {
need_outside_cb = true;
}
}
var list_or = (field == 'langs' || field == 'topics' || field == 'countries' || field == 'locations' || field == 'user_langs' || field == 'user_topics' && !Aj.state.intersectTopicsCheckbox.prop('checked') || field == 'user_channels' || field == 'audiences')
lang_params[field] = joinTargets(list, list_or);
} else {
lang_params[field] = '';
}
Ads.hideFieldError($field);
}
len.device = Aj.state.$form.field('device').data('value');
lang_params.device = '
' + Aj.state.$form.field('device').html() + '';
var overview = '';
if (target_type == 'channels') {
if ((len.langs || len.topics) && len.channels) {
if (len.langs) {
Ads.showFieldError(Aj.state.$form.field('langs'), l('ADS_ERROR_LANG_AND_CHANNEL_NOT_ALLOWED'));
} else if (len.topics) {
Ads.showFieldError(Aj.state.$form.field('topics'), l('ADS_ERROR_TOPIC_AND_CHANNEL_NOT_ALLOWED'));
}
} else if (!len.langs && len.topics) {
Ads.showFieldError(Aj.state.$form.field('langs'), l('ADS_ERROR_LANGUAGE_REQUIRED'));
}
if (!len.langs && !len.topics && !len.channels ||
(len.langs || len.topics) && len.channels ||
!len.langs && len.topics) {
overview += '
' + l('WEB_AD_TARGET_NOTHING') + '
';
} else {
if (len.langs > 0) {
if (len.topics > 0) {
overview += '
' + l('WEB_AD_TARGET_TOPICS', lang_params) + '
';
} else {
overview += '
' + l('WEB_AD_TARGET_LANGS', lang_params) + '
';
}
}
if (len.channels > 0) {
overview += '
' + l('WEB_AD_TARGET_CHANNELS', lang_params) + '
';
}
if (len.exclude_topics > 0) {
overview += '
' + l('WEB_AD_TARGET_EXCLUDE_TOPICS', lang_params) + '
';
}
if (len.exclude_channels > 0) {
overview += '
' + l('WEB_AD_TARGET_EXCLUDE_CHANNELS', lang_params) + '
';
}
}
$('.js-exclude-outside').toggleClass('hide', !need_outside_cb);
} else if (target_type == 'users') {
if (!len.locations && !len.countries && !(NewAd.isAudienceTargetOnly(len) &&
Aj.state.audienceTargetOnlyAvailable)) {
overview += '
' + l('WEB_AD_TARGET_NOTHING') + '
';
} else {
var user_targets = [];
if (len.locations > 0) {
user_targets.push(l('WEB_AD_TARGET_USER_LOCATIONS', lang_params));
} else if (len.countries > 0) {
lang_params.locations = lang_params.countries;
user_targets.push(l('WEB_AD_TARGET_USER_LOCATIONS', lang_params));
}
if (len.user_langs > 0) {
user_targets.push(l('WEB_AD_TARGET_USER_LANGS', lang_params));
}
if (len.user_topics > 0) {
user_targets.push(l('WEB_AD_TARGET_USER_TOPICS', lang_params));
}
if (len.user_channels > 0) {
user_targets.push(l('WEB_AD_TARGET_USER_CHANNELS', lang_params));
}
if (len.audiences > 0) {
user_targets.push(l('WEB_AD_TARGET_AUDIENCES', lang_params));
}
if (len.device) {
user_targets.push(l('WEB_AD_TARGET_DEVICE', lang_params));
}
user_targets = joinTargets(user_targets, false, true);
if (Aj.state.onlyPoliticCheckbox.prop('checked')) {
overview += '
' + l('WEB_AD_TARGET_USERS_ONLY_POLITIC', {target: user_targets}) + '
';
} else {
overview += '
' + l('WEB_AD_TARGET_USERS', {target: user_targets}) + '
';
}
if (len.exclude_user_topics > 0) {
overview += '
' + l('WEB_AD_TARGET_USER_EXCLUDE_TOPICS', lang_params) + '
';
}
if (len.exclude_user_channels > 0) {
overview += '
' + l('WEB_AD_TARGET_USER_EXCLUDE_CHANNELS', lang_params) + '
';
}
if (len.exclude_audiences > 0) {
overview += '
' + l('WEB_AD_TARGET_EXCLUDE_AUDIENCES', lang_params) + '
';
}
if (Aj.state.excludePoliticCheckbox.prop('checked')) {
overview += '
' + l('WEB_AD_TARGET_EXCLUDE_POLITIC') + '
';
}
}
$('.js-exclude-outside').addClass('hide');
} else {
overview += '
' + l('WEB_AD_TARGET_NOTHING') + '
';
}
$('.pr-target-overview', Aj.ajContainer).html(overview);
},
getFormData: function($form) {
var form = $form.get(0);
if (!form) return false;
var values = [
$form.field('title').value(),
$form.field('text').value(),
$form.field('button').data('value'),
$form.field('promote_url').value(),
$form.field('website_name').value(),
$form.field('website_photo').value(),
$form.field('ad_info').value(),
$form.field('cpm').value(),
$form.field('views_per_user').value(),
$form.field('budget').value(),
$form.field('target_type').value(),
$form.field('device').data('value')
];
if ($form.field('picture').prop('checked')) {
values.push('picture');
}
for (var i = 0; i < Aj.state.selectList.length; i++) {
var selectData = Aj.state.selectList[i];
var vals = $form.field(selectData.field).data('value') || [];
values.push(vals.join(';'));
}
if ($form.field('intersect_topics').prop('checked')) {
values.push('intersect_topics');
}
if ($form.field('exclude_politic').prop('checked')) {
values.push('exclude_politic');
}
if ($form.field('only_politic').prop('checked')) {
values.push('only_politic');
}
if ($form.field('exclude_outside').prop('checked')) {
values.push('exclude_outside');
}
return values.join('|');
},
getPreviewFormData: function($form) {
if (Aj.state.$previewPopup) {
var $previewPopup = Aj.state.$previewPopup;
var $previewForm = $('.pr-new-form', $previewPopup);
if (!$previewForm.get(0)) return false;
var values = [
$previewForm.field('text').value(),
$previewForm.field('button').data('value'),
$previewForm.field('promote_url').value(),
$previewForm.field('website_name').value(),
$previewForm.field('website_photo').value()
];
if ($previewForm.field('picture').prop('checked')) {
values.push('picture');
}
return values.join('|');
}
return false;
},
eSubmitForm: function(e) {
e.preventDefault();
var $form = Aj.state.$form;
var $button = $(this);
var title = $form.field('title').value();
var text = $form.field('text').value();
var button = $form.field('button').data('value');
var promote_url = $form.field('promote_url').value();
var website_name = $form.field('website_name').value();
var website_photo = $form.field('website_photo').value();
var ad_info = $form.field('ad_info').value();
var cpm = Ads.amountFieldValue($form, 'cpm');
var views_per_user = $form.field('views_per_user').value();
var budget = Ads.amountFieldValue($form, 'budget');
var target_type = $form.field('target_type').value();
var device = $form.field('device').data('value');
if (!title.length) {
$form.field('title').focus();
return false;
}
if (!text.length) {
$form.field('text').focus();
return false;
}
if (!promote_url.length) {
$form.field('promote_url').focus();
return false;
}
if (cpm === false) {
$form.field('cpm').focus();
return false;
}
if (budget === false) {
$form.field('budget').focus();
return false;
}
var params = {
owner_id: Aj.state.ownerId,
title: title,
text: text,
button: button,
promote_url: promote_url,
website_name: website_name,
website_photo: website_photo,
ad_info: ad_info,
cpm: cpm,
views_per_user: views_per_user,
budget: budget,
target_type: target_type,
device: device
};
if ($form.field('picture').prop('checked')) {
params.picture = 1;
}
for (var i = 0; i < Aj.state.selectList.length; i++) {
var selectData = Aj.state.selectList[i];
var values = $form.field(selectData.field).data('value') || [];
params[selectData.field] = values.join(';');
}
if ($form.field('intersect_topics').prop('checked')) {
params.intersect_topics = 1;
}
if ($form.field('exclude_politic').prop('checked')) {
params.exclude_politic = 1;
}
if ($form.field('only_politic').prop('checked')) {
params.only_politic = 1;
}
if ($form.field('exclude_outside').prop('checked')) {
params.exclude_outside = 1;
}
NewAd.saveDraftAuto(true);
$button.prop('disabled', true);
Aj.apiRequest('createAd', params, function(result) {
if (result.error) {
$button.prop('disabled', false);
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
Aj.state.initFormData = NewAd.getFormData($form);
NewAd.saveDraftAuto(true);
if (result.redirect_to) {
Aj.location(result.redirect_to);
}
});
return false;
},
eClearDraft: function(e) {
e.preventDefault();
NewAd.clearDraft();
},
saveDraftAuto: function(with_delay) {
if (!with_delay) {
NewAd.saveDraft();
}
clearTimeout(Aj.state.saveDraftTo);
Aj.state.saveDraftTo = setTimeout(NewAd.saveDraftAuto, 3000, false);
},
saveDraft: function() {
var $form = Aj.state.$form;
var title = $form.field('title').value();
var text = $form.field('text').value();
var button = $form.field('button').data('value');
var promote_url = $form.field('promote_url').value();
var website_name = $form.field('website_name').value();
var website_photo = $form.field('website_photo').value();
var ad_info = $form.field('ad_info').value();
var cpm = Ads.amountFieldValue($form, 'cpm');
var views_per_user = $form.field('views_per_user').value();
var budget = Ads.amountFieldValue($form, 'budget');
var target_type = $form.field('target_type').value();
var device = $form.field('device').data('value');
var curFormData = NewAd.getFormData($form);
if (Aj.state.initFormData == curFormData) {
return false;
}
var params = {
owner_id: Aj.state.ownerId,
title: title,
text: text,
button: button,
promote_url: promote_url,
website_name: website_name,
website_photo: website_photo,
ad_info: ad_info,
cpm: cpm,
views_per_user: views_per_user,
budget: budget,
target_type: target_type,
device: device
};
if ($form.field('picture').prop('checked')) {
params.picture = 1;
}
for (var i = 0; i < Aj.state.selectList.length; i++) {
var selectData = Aj.state.selectList[i];
var values = $form.field(selectData.field).data('value') || [];
params[selectData.field] = values.join(';');
}
if ($form.field('intersect_topics').prop('checked')) {
params.intersect_topics = 1;
}
if ($form.field('exclude_politic').prop('checked')) {
params.exclude_politic = 1;
}
if ($form.field('only_politic').prop('checked')) {
params.only_politic = 1;
}
if ($form.field('exclude_outside').prop('checked')) {
params.exclude_outside = 1;
}
Aj.apiRequest('saveAdDraft', params, function(result) {
if (result.error) {
return showAlert(result.error);
}
Aj.state.initFormData = curFormData;
NewAd.saveDraftAuto(true);
$('.pr-draft-btn-wrap').toggleClass('active', !!result.has_draft);
// $('.pr-draft-btn-wrap').addClass('saved');
// setTimeout(function() {
// $('.pr-draft-btn-wrap').removeClass('saved');
// }, 1500);
});
return false;
},
clearDraft: function(callback) {
var $form = Aj.state.$form;
$form.field('title').value('');
$form.field('text').value('');
$form.field('button').trigger('selectval', ['']);
$form.field('promote_url').value('');
$form.field('website_name').value('');
$form.field('website_photo').value('');
$form.field('ad_info').value('');
$form.field('cpm').value('');
$form.field('budget').value('');
$form.field('picture').prop('checked', false);
for (var i = 0; i < Aj.state.selectList.length; i++) {
var selectData = Aj.state.selectList[i];
var values = $form.field(selectData.field).trigger('reset');
}
$form.field('exclude_politic').prop('checked', false);
$form.field('only_politic').prop('checked', false);
$form.field('exclude_outside').prop('checked', false);
$form.field('device').trigger('selectval', ['']);
Aj.state.titleField.focusAndSelect();
NewAd.updateAdPreview(Aj.state.$form, false);
var curFormData = NewAd.getFormData($form);
var params = {
owner_id: Aj.state.ownerId
};
$('.pr-draft-btn-wrap').removeClass('active');
NewAd.saveDraftAuto(true);
Aj.apiRequest('clearAdDraft', params, function(result) {
if (result.error) {
return showAlert(result.error);
}
Aj.state.initFormData = curFormData;
NewAd.saveDraftAuto(true);
callback && callback();
});
return false;
}
};
var Upload = {
uploadFile: function(file, onSuccess, onProgress, onError) {
var data = new FormData();
data.append('file', file, file.name);
return $.ajax({
url: '/file/upload',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false,
contentType: false,
xhr: function() {
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', function(event) {
if (event.lengthComputable) {
onProgress && onProgress(event.loaded, event.total);
}
});
return xhr;
},
beforeSend: function(xhr) {
onProgress && onProgress(0, 1);
},
success: function (result) {
if (result.error) {
return onError && onError(result.error);
}
onSuccess && onSuccess(result);
},
error: function (xhr) {
return onError && onError('Network error');
}
});
}
};
var Account = {
formFields: [
'full_name',
'email',
'phone_number',
'country',
'city',
'ad_info'
],
addFundsFormFields: [
'adv_type',
'products_desc',
'ads_language',
'annual_budget',
'additional_comment',
],
optFields: {
ad_info: 1
},
init: function() {
var cont = Aj.ajContainer;
Aj.onLoad(function(state) {
state.$form = $('.account-edit-form', cont);
Ads.formInit(state.$form);
state.$form.on('submit', preventDefault);
cont.on('click.curPage', '.save-info-btn', Account.eSubmitForm);
cont.on('change.curPage', '.pr-form-control', Account.onFieldChange);
Ads.initSelect(state.$form, 'channel', {
items: Aj.state.channelItems || [],
noMultiSelect: true,
renderSelectedItem: function(val, item) {
return '
' + (item.photo ? '
' + item.photo + '
' : '') + '
' + item.name + '
';
},
onChange: Account.onChannelChange
});
Aj.onLoad(function(state) {
state.initFormData = Account.getFormData(state.$form);
Aj.onBeforeUnload(function() {
var curFormData = Account.getFormData(state.$form);
if (Aj.state.initFormData != curFormData) {
return l('WEB_LEAVE_PAGE_CONFIRM_TEXT');
}
return false;
});
});
});
Aj.onUnload(function(state) {
Ads.formDestroy(state.$form);
state.$form.off('submit', preventDefault);
});
},
onFieldChange: function() {
if (!$(this).hasClass('js-amount-input')) {
Ads.hideFieldError($(this));
}
},
onChannelChange: function(field, value, valueFull) {
$accPhoto = $('.pr-account-button-wrap.current .pr-account-button-photo');
$accTitle = $('.pr-account-button-wrap.current .pr-account-button-title');
if (!$accPhoto.data('def-val')) {
$accPhoto.data('def-val', $accPhoto.html());
}
if (!$accTitle.data('def-val')) {
$accTitle.data('def-val', $accTitle.html());
}
$accPhoto.html(valueFull ? valueFull.photo : $accPhoto.data('def-val'));
$accTitle.html(valueFull ? valueFull.name : $accTitle.data('def-val'));
var $form = Aj.state.$form;
for (var i = 0; i < Account.formFields.length; i++) {
var field = Account.formFields[i];
var value = $form.field(field).prop('disabled', !valueFull);
}
},
getFormData: function($form) {
var form = $form.get(0);
if (!form) return false;
var values = [];
if (!Aj.state.ownerId) {
var value = $form.field('channel').data('value');
values.push(value);
}
for (var i = 0; i < Account.formFields.length; i++) {
var field = Account.formFields[i];
var value = $form.field(field).value();
values.push(value);
}
return values.join('|');
},
eSubmitForm: function(e) {
e.preventDefault();
var $form = Aj.state.$form;
var $button = $(this);
var params = {};
if (Aj.state.ownerId) {
params.owner_id = Aj.state.ownerId;
} else {
params.owner_id = $form.field('channel').data('value');
if (!params.owner_id) {
$form.field('channel').trigger('click');
return false;
}
}
for (var i = 0; i < Account.formFields.length; i++) {
var field = Account.formFields[i];
var value = $form.field(field).value();
if (!value.length && !Account.optFields[field]) {
$form.field(field).focus();
return false;
}
params[field] = value;
}
$button.prop('disabled', true);
Aj.apiRequest('saveAccountInfo', params, function(result) {
if (result.error) {
$button.prop('disabled', false);
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
Aj.state.initFormData = Account.getFormData($form);
if (result.redirect_to) {
Aj.location(result.redirect_to);
}
});
return false;
},
initAddFunds: function() {
var cont = Aj.ajContainer;
Aj.onLoad(function(state) {
state.$form = $('.add-funds-req-form', cont);
Ads.formInit(state.$form);
state.$form.on('submit', preventDefault);
cont.on('click.curPage', '.send-request-btn', Account.eSendAddFundsRequest);
cont.on('change.curPage', '.pr-form-control', Account.onFieldChange);
Ads.initSelect(state.$form, 'ads_language', {
items: Aj.state.langItems || [],
noMultiSelect: true,
l_no_items_found: l('WEB_AD_NO_LANGS_FOUND'),
onChange: Account.onFieldChange
});
state.confirmedCheckbox = state.$form.field('confirmed');
state.confirmedCheckbox.on('change.curPage', Account.onConfirmedChange);
Aj.onLoad(function(state) {
state.initFormData = Account.getAddFundsFormData(state.$form);
Aj.onBeforeUnload(function() {
var curFormData = Account.getAddFundsFormData(state.$form);
if (Aj.state.initFormData != curFormData) {
return l('WEB_LEAVE_PAGE_CONFIRM_TEXT');
}
return false;
});
});
});
Aj.onUnload(function(state) {
Ads.formDestroy(state.$form);
state.$form.off('submit', preventDefault);
});
},
onConfirmedChange: function() {
$('.send-request-btn', Aj.ajContainer).prop('disabled', !$(this).prop('checked'));
},
getAddFundsFormData: function($form) {
var form = $form.get(0);
if (!form) return false;
var values = [];
for (var i = 0; i < Account.formFields.length; i++) {
var field = Account.formFields[i];
var value = $form.field(field).value();
values.push(value);
}
for (var i = 0; i < Account.addFundsFormFields.length; i++) {
var field = Account.addFundsFormFields[i];
var value = $form.field(field).value();
values.push(value);
}
return values.join('|');
},
eSendAddFundsRequest: function(e) {
e.preventDefault();
var $form = Aj.state.$form;
var $button = $(this);
var params = {
owner_id: Aj.state.ownerId
};
for (var i = 0; i < Account.formFields.length; i++) {
var field = Account.formFields[i];
var value = $form.field(field).value();
if (!value.length && !Account.optFields[field]) {
$form.field(field).focus();
return false;
}
params[field] = value;
}
params.adv_type = $form.field('adv_type').value();
params.products_desc = $form.field('products_desc').value();
if (!params.products_desc.length) {
$form.field('products_desc').focus();
return false;
}
params.ads_language = $form.field('ads_language').data('value');
if (!params.ads_language.length) {
$form.field('ads_language').trigger('click');
return false;
}
params.annual_budget = Ads.amountFieldValue($form, 'annual_budget');
if (params.annual_budget === false) {
$form.field('annual_budget').focus();
return false;
}
params.additional_comment = $form.field('additional_comment').value();
$button.prop('disabled', true);
Aj.apiRequest('sendAddFundsRequest', params, function(result) {
if (result.error) {
$button.prop('disabled', false);
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
Aj.state.initFormData = Account.getAddFundsFormData($form);
if (result.redirect_to) {
Aj.location(result.redirect_to);
}
if (result.ok_msg) {
showAlert(result.ok_msg);
}
});
return false;
},
initToken: function() {
var cont = Aj.ajContainer;
Aj.onLoad(function(state) {
cont.on('click.curPage', '.js-copy-token-btn', Account.eCopyToken);
cont.on('click.curPage', '.js-revoke-token-btn', Account.eRevokeToken);
state.$form = $('.api-edit-form', cont);
Ads.formInit(state.$form);
state.$form.on('submit', preventDefault);
cont.on('click.curPage', '.js-save-settings-btn', Account.eApiSubmitForm);
cont.on('change.curPage', '.pr-form-control', Account.onFieldChange);
});
},
eCopyToken: function(e) {
e.preventDefault();
copyToClipboard(Aj.state.token);
showToast(l('WEB_TOKEN_COPIED', 'Copied.'));
},
revokeTokenPopup: function (onConfirm) {
var $confirm = $('');
var confirm = function() {
onConfirm && onConfirm($confirm);
closePopup($confirm);
}
var $primaryBtn = $('.popup-primary-btn', $confirm);
$primaryBtn.on('click', confirm);
$confirm.one('popup:close', function() {
$primaryBtn.off('click', confirm);
$confirm.remove();
});
openPopup($confirm, {
closeByClickOutside: '.popup-no-close',
});
return $confirm;
},
eRevokeToken: function(e) {
e.preventDefault();
var $btn = $(this);
if ($btn.data('disabled')) {
return false;
}
Account.revokeTokenPopup(function() {
$btn.data('disabled', true);
Aj.apiRequest('revokeToken', {
owner_id: Aj.state.ownerId
}, function(result) {
$btn.data('disabled', false);
if (result.error) {
return showAlert(result.error);
}
if (result.new_token) {
Aj.state.token = result.new_token;
}
if (result.new_token_value) {
$('.js-token-value', Aj.ajContainer).value(result.new_token_value);
}
if (result.toast) {
showToast(result.toast);
}
});
});
return false;
},
eApiSubmitForm: function(e) {
e.preventDefault();
var $form = Aj.state.$form;
var $button = $(this);
var params = {
owner_id: Aj.state.ownerId,
ip_list: $form.field('ip_list').value()
};
$button.prop('disabled', true);
Aj.apiRequest('saveApiSettings', params, function(result) {
$button.prop('disabled', false);
if (result.error) {
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
if (result.new_ip_list) {
$form.field('ip_list').value(result.new_ip_list).updateAutosize();
}
if (result.toast) {
showToast(result.toast);
}
});
return false;
}
};
var OwnerAds = {
init: function() {
var cont = Aj.ajContainer;
Aj.onLoad(function(state) {
state.$searchField = $('.pr-search-input');
state.$adsListTable = $('.pr-table');
state.$searchResults = $('.pr-table tbody');
Ads.fieldInit(state.$searchField);
cont.on('click.curPage', '.pr-cell-sort', OwnerAds.eSortList);
cont.on('click.curPage', '.pr-table-settings', OwnerAds.eSettingsOpen);
cont.on('click.curPage', '.js-clone-ad-btn', EditAd.eCloneAd);
cont.on('click.curPage', '.delete-ad-btn', EditAd.deleteAd);
state.$tableColumnsPopup = $('.table-columns-popup');
state.$tableColumnsForm = $('.table-columns-form');
state.$tableColumnsForm.on('change.curPage', 'input.checkbox', OwnerAds.eColumnChange);
state.$tableColumnsForm.on('submit.curPage', preventDefault);
state.$searchField.initSearch({
$results: state.$searchResults,
emptyQueryEnabled: true,
updateOnInit: true,
resultsNotScrollable: true,
itemTagName: 'tr',
enterEnabled: function() {
return false;
},
renderItem: function(item, query) {
var status_attrs = ' href="' + item.base_url + item.status_url + '" ' + (item.status_attrs || 'data-layer');
var title_class = 'pr-trg-type-' + item.trg_type;
if (item.tme_path) {
var promote_url = 'https://t.me/' + item.tme_path;
var promote_url_text = 't.me/' + item.tme_path;
var promote_link = '
' + promote_url_text + '';
} else if (item.promote_url) {
var promote_url = item.promote_url;
var promote_url_text = promote_url.replace(/^https?:\/\//, '').replace(/\/$/, '');
var promote_link = '
' + promote_url_text + '';
} else {
var promote_url = '#';
var promote_url_text = l('WEB_ADS_NO_TME_LINK');
var promote_link = '
' + promote_url_text + '';
}
var joins = item.joins !== false ? formatNumber(item.joins) : '–';
var clicks = item.clicks !== false ? formatNumber(item.clicks) : '–';
var ctr = item.ctr !== false ? item.ctr + '%' : '–';
var cpc = item.cpc !== false ? Ads.wrapAmount(item.cpc) : '–';
var cps = item.cps !== false ? Ads.wrapAmount(item.cps) : '–';
return '
| | | | | | | | | | | | | ' + Aj.state.adsDropdownTpl.replace(/\{ad_id\}/g, item.ad_id).replace(/\{promote_url\}/g, promote_url).replace(/\{promote_url_text\}/g, promote_url_text).replace(/\{ad_text\}/g, item.text) + ' | ';
},
renderLoading: function() {
return '
' + l('WEB_OWNER_ADS_LOADING') + ' |
';
},
renderNoItems: function(query) {
if (Aj.state.adsListIsLoading) {
return '
' + l('WEB_OWNER_ADS_LOADING') + ' |
';
}
return '
' + l('WEB_OWNER_NO_ADS') + ' |
';
},
appendToItems: function(query, result_count) {
if (Aj.state.adsListIsLoading && result_count > 0) {
return '
' + l('WEB_OWNER_ADS_LOADING') + ' |
';
}
return '';
},
getData: function() {
return OwnerAds.getAdsList();
}
});
});
Aj.onUnload(function(state) {
Ads.fieldDestroy(state.$searchField);
state.$searchField.destroySearch();
state.$tableColumnsForm.off('.curPage');
});
},
eSortList: function(e) {
var $sortEl = $(this);
var sortBy = $sortEl.attr('data-sort-by');
var sortAsc = $sortEl.hasClass('sort-asc');
if (sortBy == Aj.state.adsListSortBy) {
Aj.state.adsListSortAsc = !sortAsc;
} else {
Aj.state.adsListSortBy = sortBy;
Aj.state.adsListSortAsc = false;
}
OwnerAds.updateAdsList();
Aj.state.$searchField.trigger('datachange');
},
eSettingsOpen: function() {
openPopup(Aj.state.$tableColumnsPopup, {
closeByClickOutside: '.popup-no-close',
});
},
eColumnChange: function() {
var column = $(this).prop('name');
var checked = $(this).prop('checked');
Aj.state.$adsListTable.cssProp('--coldp-' + column, checked ? '' : 'none');
OwnerAds.submitColumns();
},
submitColumns: function() {
var $form = Aj.state.$tableColumnsForm;
var active_columns = [];
for (var i = 0; i < Aj.state.adsListAllColumns.length; i++) {
var column = Aj.state.adsListAllColumns[i];
if ($form.field(column).prop('checked')) {
active_columns.push(column);
}
}
Aj.apiRequest('saveAdsColumns', {
columns: active_columns.join(';')
});
return false;
},
updateAdsList: function() {
if (Aj.state.adsList) {
var sortBy = Aj.state.adsListSortBy;
var sortAsc = Aj.state.adsListSortAsc;
$('.pr-cell-sort').each(function() {
var $sortEl = $(this);
var curSortBy = $sortEl.attr('data-sort-by');
$sortEl.toggleClass('sort-active', sortBy == curSortBy);
$sortEl.toggleClass('sort-asc', sortAsc && sortBy == curSortBy);
});
Aj.state.adsList.sort(function(ad1, ad2) {
var v1 = sortAsc ? ad1 : ad2;
var v2 = sortAsc ? ad2 : ad1;
return (v1[sortBy] - v2[sortBy]) || (v1.date - v2.date);
});
}
},
processAdsList: function(result, opts) {
opts = opts || {};
if (result.items) {
if (!Aj.state.adsList) {
Aj.state.adsList = [];
}
for (var i = 0; i < result.items.length; i++) {
var item = result.items[i];
item.base_url = '/account/ad/' + item.ad_id;
item._values = [
item.title.toLowerCase(),
item.tme_path.toLowerCase(),
];
Aj.state.adsList.push(item);
}
OwnerAds.updateAdsList();
Aj.state.$searchField.trigger('contentchange');
}
if (result.next_offset_id) {
opts.offset = result.next_offset_id;
OwnerAds.loadAdsList(opts);
} else {
Aj.state.adsListIsLoading = false;
Aj.state.$searchField.trigger('dataready');
}
},
loadAdsList: function(opts) {
opts = opts || {};
Aj.apiRequest('getAdsList', {
owner_id: Aj.state.ownerId,
offset_id: opts.offset
}, function(result) {
if (result.error) {
if (!opts.retry) opts.retry = 1;
else opts.retry++;
setTimeout(function(){ OwnerAds.loadAdsList(opts); }, opts.retry * 1000);
} else {
if (opts.retry) {
opts.retry = 0;
}
OwnerAds.processAdsList(result, opts);
}
});
},
getAdsList: function() {
var _data = Aj.state.adsList;
if (_data === false) {
return false;
} else if (_data) {
return _data;
}
Aj.state.adsList = false;
Aj.state.adsListIsLoading = true;
if (Aj.state.initialAdsList) {
setTimeout(function() {
OwnerAds.processAdsList(Aj.state.initialAdsList);
}, 10);
} else {
OwnerAds.loadAdsList({offset: 0});
}
return false;
},
updateAd: function(ad) {
if (!Aj.state || !Aj.state.adsList) {
return;
}
var adsList = Aj.state.adsList;
for (var i = 0; i < adsList.length; i++) {
if (ad.owner_id == adsList[i].owner_id &&
ad.ad_id == adsList[i].ad_id) {
ad.base_url = '/account/ad/' + ad.ad_id;
ad._values = [
ad.title.toLowerCase(),
ad.tme_path.toLowerCase(),
];
adsList[i] = ad;
OwnerAds.updateAdsList();
Aj.state.$searchField.trigger('contentchange');
return;
}
}
}
};
var ReviewAds = {
init: function() {
var cont = Aj.ajContainer;
Aj.onLoad(function(state) {
state.$form = $('.pr-search-form', cont);
state.$form.on('submit', ReviewAds.onSubmit);
state.$searchField = $('.pr-search-input', cont);
Ads.fieldInit(state.$searchField);
cont.on('click.curPage', '.pr-search-reset', ReviewAds.eClearSearch);
cont.on('click.curPage', '.ad-approve-btn', ReviewAds.eApproveAd);
cont.on('click.curPage', '.ad-decline-btn', ReviewAds.eDeclineAd);
$(window).on('scroll resize', ReviewAds.onScroll);
ReviewAds.onScroll();
});
Aj.onUnload(function(state) {
state.$form.off('submit', ReviewAds.onSubmit);
Ads.fieldDestroy(state.$searchField);
$(window).off('scroll resize', ReviewAds.onScroll);
});
},
onScroll: function() {
$('.js-load-more').each(function() {
var $loadMore = $(this);
var top = $loadMore.offset().top - $(window).scrollTop();
if (top < $(window).height() * 2) {
ReviewAds.load($loadMore);
}
});
},
load: function($loadMore) {
var offset = $loadMore.attr('data-offset');
if (!offset) {
$loadMore.remove();
}
if ($loadMore.data('loading')) {
return;
}
var params = Aj.state.filterParams;
params.offset_id = offset;
var $loadMoreBtn = $('.pr-load-more', $loadMore);
$loadMoreBtn.data('old-text', $loadMoreBtn.text()).text($loadMoreBtn.data('loading')).addClass('dots-animated');
$loadMore.data('loading', true);
Aj.apiRequest('loadReviewedAds', params, function(result) {
$loadMore.data('loading', false);
if (result.ads_html) {
var $loadMoreCont = $loadMore.closest('.pr-review-list');
if ($loadMoreCont.size()) {
$loadMore.remove();
$loadMoreCont.append(result.ads_html);
Ads.updateAdMessagePreviews($loadMoreCont);
} else {
var $loadMoreBtn = $('.pr-load-more', $loadMore);
$loadMoreBtn.text($loadMoreBtn.data('old-text')).removeClass('dots-animated');
}
ReviewAds.onScroll();
}
});
},
onSubmit: function(e) {
e.preventDefault();
var href = this.action;
if (this.query.value) {
href += href.indexOf('?') >= 0 ? '&' : '?';
href += 'query=' + encodeURIComponent(this.query.value);
}
Aj.location(href);
},
eClearSearch: function(e) {
Aj.state.$form.submit();
},
eApproveAd: function(e) {
e.preventDefault();
var $ad = $(this).parents('.js-review-item');
var owner_id = $ad.attr('data-owner-id');
var ad_id = $ad.attr('data-ad-id');
var $buttons = $ad.find('.pr-btn');
if ($buttons.prop('disabled')) {
return false;
}
$buttons.prop('disabled', true);
Aj.apiRequest('approveAd', {
owner_id: owner_id,
ad_id: ad_id
}, function(result) {
$buttons.prop('disabled', false);
if (result.error) {
return showAlert(result.error);
}
if (result.status_html) {
$ad.find('.js-review-ad-status').html(result.status_html);
}
if (result.buttons_html) {
$ad.find('.js-review-buttons').html(result.buttons_html);
}
});
return false;
},
eDeclineAd: function(e) {
e.preventDefault();
var $ad = $(this).parents('.js-review-item');
var owner_id = $ad.attr('data-owner-id');
var ad_id = $ad.attr('data-ad-id');
var $buttons = $ad.find('.pr-btn');
var reason_id = $(this).attr('data-reason-id');
if ($buttons.prop('disabled')) {
return false;
}
$buttons.prop('disabled', true);
Aj.apiRequest('declineAd', {
owner_id: owner_id,
ad_id: ad_id,
reason_id: reason_id
}, function(result) {
$buttons.prop('disabled', false);
if (result.error) {
return showAlert(result.error);
}
if (result.status_html) {
$ad.find('.js-review-ad-status').html(result.status_html);
}
if (result.buttons_html) {
$ad.find('.js-review-buttons').html(result.buttons_html);
}
});
return false;
}
};
var ReviewTargets = {
init: function() {
var cont = Aj.ajContainer;
Aj.onLoad(function(state) {
state.$form = $('.pr-search-form', cont);
state.$form.on('submit', ReviewTargets.onSubmit);
state.$searchField = $('.pr-search-input', cont);
Ads.fieldInit(state.$searchField);
cont.on('click.curPage', '.pr-search-reset', ReviewTargets.eClearSearch);
cont.on('click.curPage', '.ad-approve-btn', ReviewTargets.eApproveAd);
cont.on('click.curPage', '.ad-decline-btn', ReviewTargets.eDeclineAd);
$(window).on('scroll resize', ReviewTargets.onScroll);
ReviewTargets.onScroll();
});
Aj.onUnload(function(state) {
state.$form.off('submit', ReviewTargets.onSubmit);
Ads.fieldDestroy(state.$searchField);
$(window).off('scroll resize', ReviewTargets.onScroll);
});
},
onScroll: function() {
$('.js-load-more').each(function() {
var $loadMore = $(this);
var top = $loadMore.offset().top - $(window).scrollTop();
if (top < $(window).height() * 2) {
ReviewTargets.load($loadMore);
}
});
},
load: function($loadMore) {
var offset = $loadMore.attr('data-offset');
if (!offset) {
$loadMore.remove();
}
if ($loadMore.data('loading')) {
return;
}
var params = Aj.state.filterParams;
params.offset_id = offset;
var $loadMoreBtn = $('.pr-load-more', $loadMore);
$loadMoreBtn.data('old-text', $loadMoreBtn.text()).text($loadMoreBtn.data('loading')).addClass('dots-animated');
$loadMore.data('loading', true);
Aj.apiRequest('loadReviewedTargets', params, function(result) {
$loadMore.data('loading', false);
if (result.targets_html) {
var $loadMoreCont = $loadMore.closest('.pr-review-list');
if ($loadMoreCont.size()) {
$loadMore.remove();
$loadMoreCont.append(result.targets_html);
Ads.updateAdMessagePreviews($loadMoreCont);
} else {
var $loadMoreBtn = $('.pr-load-more', $loadMore);
$loadMoreBtn.text($loadMoreBtn.data('old-text')).removeClass('dots-animated');
}
ReviewTargets.onScroll();
}
});
},
onSubmit: function(e) {
e.preventDefault();
var href = this.action;
if (this.query.value) {
href += href.indexOf('?') >= 0 ? '&' : '?';
href += 'query=' + encodeURIComponent(this.query.value);
}
Aj.location(href);
},
eClearSearch: function(e) {
Aj.state.$form.submit();
},
eApproveAd: function(e) {
e.preventDefault();
var $target = $(this).parents('.js-review-item');
var target = $target.attr('data-target');
var $buttons = $target.find('.pr-btn');
if ($buttons.prop('disabled')) {
return false;
}
$buttons.prop('disabled', true);
Aj.apiRequest('approveTarget', {
target: target
}, function(result) {
$buttons.prop('disabled', false);
if (result.error) {
return showAlert(result.error);
}
if (result.status_html) {
$target.find('.js-review-target-status').html(result.status_html);
}
if (result.buttons_html) {
$target.find('.js-review-buttons').html(result.buttons_html);
}
});
return false;
},
eDeclineAd: function(e) {
e.preventDefault();
var $target = $(this).parents('.js-review-item');
var target = $target.attr('data-target');
var $buttons = $target.find('.pr-btn');
var reason_id = $(this).attr('data-reason-id');
if ($buttons.prop('disabled')) {
return false;
}
$buttons.prop('disabled', true);
Aj.apiRequest('declineTarget', {
target: target,
reason_id: reason_id
}, function(result) {
$buttons.prop('disabled', false);
if (result.error) {
return showAlert(result.error);
}
if (result.status_html) {
$target.find('.js-review-target-status').html(result.status_html);
}
if (result.buttons_html) {
$target.find('.js-review-buttons').html(result.buttons_html);
}
});
return false;
}
};
var EditAd = {
init: function() {
var cont = Aj.ajContainer;
Aj.onLoad(function(state) {
state.$form = $('.pr-new-form', cont);
Ads.formInit(state.$form);
state.$form.on('submit', preventDefault);
cont.on('click.curPage', '.js-preview-link', NewAd.ePreviewAd);
cont.on('click.curPage', '.js-promote-photo', NewAd.eReplacePromotePhoto);
cont.on('change.curPage', '.js-promote-photo > .file-upload', NewAd.eUploadPromotePhoto);
cont.on('click.curPage', '.edit-ad-btn', EditAd.eSubmitForm);
cont.on('click.curPage', '.js-clone-ad-btn', EditAd.eCloneAd);
cont.on('click.curPage', '.js-send-to-review-btn', EditAd.eSendToReview);
cont.on('click.curPage', '.delete-ad-btn', EditAd.deleteAd);
cont.on('click.curPage', '.pr-form-select', EditAd.eSelectPlaceholder);
state.titleField = state.$form.field('title');
state.titleField.on('change.curPage', NewAd.onTitleChange);
state.textField = state.$form.field('text');
state.textField.on('change.curPage', NewAd.onTextChange);
state.textField.on('input.curPage', NewAd.onTextInput);
state.promoteUrlField = state.$form.field('promote_url');
state.promoteUrlField.on('change.curPage', NewAd.onPromoteUrlChange);
state.websiteNameField = state.$form.field('website_name');
state.websiteNameField.on('change.curPage', NewAd.onWebsiteNameChange);
state.websitePhotoField = state.$form.field('website_photo');
state.buttonField = state.$form.field('button');
state.buttonField.on('ddchange.curPage', NewAd.onButtonChange);
state.adInfoField = state.$form.field('ad_info');
state.adInfoField.on('change.curPage', NewAd.onAdInfoChange);
state.pictureCheckbox = state.$form.field('picture');
state.pictureCheckbox.on('change.curPage', NewAd.onPictureChange);
NewAd.updateAdPreview(state.$form, state.previewData);
Aj.onLoad(function(state) {
state.initFormData = EditAd.getFormData(state.$form);
state.initPreviewFormData = NewAd.getPreviewFormData();
Aj.onBeforeUnload(function() {
var curPreviewFormData = NewAd.getPreviewFormData();
if (Aj.state.initPreviewFormData != curPreviewFormData) {
return l('WEB_LEAVE_PAGE_CONFIRM_TEXT');
}
var curFormData = EditAd.getFormData(state.$form);
if (Aj.state.initFormData != curFormData) {
return l('WEB_LEAVE_PAGE_CONFIRM_TEXT');
}
return false;
});
});
});
Aj.onUnload(function(state) {
Ads.formDestroy(state.$form);
state.$form.off('submit', preventDefault);
state.titleField.off('.curPage');
state.textField.off('.curPage');
state.promoteUrlField.off('.curPage');
state.websiteNameField.off('.curPage');
state.buttonField.off('.curPage');
});
},
getFormData: function($form) {
var form = $form.get(0);
if (!form) return false;
var values = [
$form.field('title').value(),
$form.field('text').value(),
$form.field('button').data('value'),
$form.field('promote_url').value(),
$form.field('website_name').value(),
$form.field('website_photo').value(),
$form.field('cpm').value(),
$form.field('views_per_user').value()
];
return values.join('|');
},
eSelectPlaceholder: function() {
Ads.showHint($('.pr-form-control-hint', this), 50, 2000);
},
initEditTitlePopup: function() {
var cont = Aj.layer;
Aj.onLayerLoad(function(layerState) {
layerState.$form = $('.pr-popup-edit-form', cont);
Ads.formInit(layerState.$form);
layerState.titleField = layerState.$form.field('title');
layerState.titleField.on('change.curPage', NewAd.onTitleChange);
Aj.layer.one('popup:open', function() {
layerState.titleField.focusAndSelect(true);
});
layerState.$form.on('submit', EditAd.eSubmitEditTitlePopupForm);
cont.on('click.curLayer', '.submit-form-btn', EditAd.eSubmitEditTitlePopupForm);
});
Aj.onLayerUnload(function(layerState) {
Ads.formDestroy(layerState.$form);
layerState.$form.off('submit', EditAd.eSubmitEditTitlePopupForm);
layerState.titleField.off('.curPage');
});
},
eSubmitEditTitlePopupForm: function(e) {
e.preventDefault();
var $form = Aj.layerState.$form;
var owner_id = $form.field('owner_id').value();
var ad_id = $form.field('ad_id').value();
var title = $form.field('title').value();
if ($form.data('disabled')) {
return false;
}
if (!title.length) {
$form.field('title').focus();
return false;
}
var params = {
owner_id: owner_id,
ad_id: ad_id,
title: title
};
$form.data('disabled', true);
Aj.apiRequest('editAdTitle', params, function(result) {
$form.data('disabled', false);
if (result.error) {
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
closePopup(Aj.layer);
if (result.ad) {
OwnerAds.updateAd(result.ad);
}
});
return false;
},
initEditCPMPopup: function() {
var cont = Aj.layer;
Aj.onLayerLoad(function(layerState) {
layerState.$form = $('.pr-popup-edit-form', cont);
Ads.formInit(layerState.$form);
layerState.cpmField = layerState.$form.field('cpm');
Aj.layer.one('popup:open', function() {
layerState.cpmField.focusAndSelect(true);
});
layerState.$form.on('submit', EditAd.eSubmitEditCPMPopupForm);
cont.on('click.curLayer', '.submit-form-btn', EditAd.eSubmitEditCPMPopupForm);
});
Aj.onLayerUnload(function(layerState) {
Ads.formDestroy(layerState.$form);
layerState.$form.off('submit', EditAd.eSubmitEditCPMPopupForm);
});
},
eSubmitEditCPMPopupForm: function(e) {
e.preventDefault();
var $form = Aj.layerState.$form;
var owner_id = $form.field('owner_id').value();
var ad_id = $form.field('ad_id').value();
var cpm = Ads.amountFieldValue($form, 'cpm');
if ($form.data('disabled')) {
return false;
}
if (cpm === false) {
$form.field('cpm').focus();
return false;
}
var params = {
owner_id: owner_id,
ad_id: ad_id,
cpm: cpm
};
$form.data('disabled', true);
Aj.apiRequest('editAdCPM', params, function(result) {
$form.data('disabled', false);
if (result.error) {
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
closePopup(Aj.layer);
if (result.ad) {
OwnerAds.updateAd(result.ad);
}
});
return false;
},
initEditBudgetPopup: function() {
var cont = Aj.layer;
Aj.onLayerLoad(function(layerState) {
layerState.$form = $('.pr-popup-edit-form', cont);
Ads.formInit(layerState.$form);
layerState.budgetField = layerState.$form.field('budget');
Aj.layer.one('popup:open', function() {
layerState.budgetField.focusAndSelect(true);
});
layerState.$form.on('submit', EditAd.eSubmitEditBudgetPopupForm);
cont.on('click.curLayer', '.submit-form-btn', EditAd.eSubmitEditBudgetPopupForm);
});
Aj.onLayerUnload(function(layerState) {
Ads.formDestroy(layerState.$form);
layerState.$form.off('submit', EditAd.eSubmitEditBudgetPopupForm);
});
},
eSubmitEditBudgetPopupForm: function(e) {
e.preventDefault();
var $form = Aj.layerState.$form;
var owner_id = $form.field('owner_id').value();
var ad_id = $form.field('ad_id').value();
var budget = Ads.amountFieldValue($form, 'budget');
if ($form.data('disabled')) {
return false;
}
if (budget === false) {
$form.field('budget').focus();
return false;
}
var params = {
owner_id: owner_id,
ad_id: ad_id,
budget: budget,
popup: 1
};
$form.data('disabled', true);
Aj.apiRequest('incrAdBudget', params, function(result) {
$form.data('disabled', false);
if (result.error) {
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
closePopup(Aj.layer);
if (result.ad) {
OwnerAds.updateAd(result.ad);
}
if (result.owner_budget) {
$('.js-owner_budget').html(result.owner_budget);
}
if (result.ad_budget_val) {
$('.js-ad_budget_val').value(result.ad_budget_val);
}
});
return false;
},
initEditStatusPopup: function() {
var cont = Aj.layer;
Aj.onLayerLoad(function(layerState) {
layerState.$form = $('.pr-popup-edit-form', cont);
Ads.formInit(layerState.$form);
layerState.$form.on('submit', EditAd.eSubmitEditStatusForm);
cont.on('click.curLayer', '.submit-form-btn', EditAd.eSubmitEditStatusForm);
});
Aj.onLayerUnload(function(layerState) {
Ads.formDestroy(layerState.$form);
layerState.$form.off('submit', EditAd.eSubmitEditStatusForm);
});
},
eSubmitEditStatusForm: function(e) {
e.preventDefault();
var $form = Aj.layerState.$form;
var owner_id = $form.field('owner_id').value();
var ad_id = $form.field('ad_id').value();
var active = $form.field('active').value();
if ($form.data('disabled')) {
return false;
}
var params = {
owner_id: owner_id,
ad_id: ad_id,
active: active
};
$form.data('disabled', true);
Aj.apiRequest('editAdStatus', params, function(result) {
$form.data('disabled', false);
if (result.error) {
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
closePopup(Aj.layer);
if (result.ad) {
OwnerAds.updateAd(result.ad);
}
});
return false;
},
initShareStatsPopup: function() {
var cont = Aj.layer;
Aj.onLayerLoad(function(layerState) {
layerState.$urlField = $('.js-share-url', cont);
layerState.$copyBtn = $('.js-copy-link', cont);
layerState.$revokeBtn = $('.js-revoke-link', cont);
layerState.$urlField.on('click', EditAd.eSelectUrl);
layerState.$copyBtn.on('click', EditAd.eCopyUrl);
layerState.$revokeBtn.on('click', EditAd.eRevokeUrl);
});
Aj.onLayerUnload(function(layerState) {
layerState.$urlField.off('click', EditAd.eSelectUrl);
layerState.$copyBtn.off('click', EditAd.eCopyUrl);
layerState.$revokeBtn.off('click', EditAd.eRevokeUrl);
});
},
eSelectUrl: function() {
Aj.layerState.$urlField.focusAndSelectAll();
},
eCopyUrl: function(copy) {
Aj.layerState.$urlField.focusAndSelectAll();
document.execCommand('copy');
showToast(l('WEB_AD_STATS_LINK_COPIED', 'Copied.'));
},
eRevokeUrl: function(e) {
e.preventDefault();
var $btn = $(this);
if ($btn.data('disabled')) {
return false;
}
var params = {
owner_id: Aj.layerState.ownerId,
ad_id: Aj.layerState.adId
};
$btn.data('disabled', true);
Aj.apiRequest('revokeStatsUrl', params, function(result) {
$btn.data('disabled', false);
if (result.error) {
return showAlert(result.error);
}
if (result.new_url) {
Aj.layerState.$urlField.value(result.new_url);
}
if (result.toast) {
showToast(result.toast);
}
});
return false;
},
initIncrBudget: function() {
var cont = Aj.ajContainer;
Aj.onLoad(function(state) {
state.$form = $('.pr-incr-budget-form', cont);
Ads.formInit(state.$form);
state.budgetField = state.$form.field('budget');
state.$form.on('submit', EditAd.eSubmitIncrBudgetForm);
cont.on('click.curPage', '.submit-form-btn', EditAd.eSubmitIncrBudgetForm);
});
Aj.onUnload(function(state) {
Ads.formDestroy(state.$form);
state.$form.off('submit', EditAd.eSubmitIncrBudgetForm);
});
},
eSubmitIncrBudgetForm: function(e) {
e.preventDefault();
var $form = Aj.state.$form;
var owner_id = $form.field('owner_id').value();
var ad_id = $form.field('ad_id').value();
var budget = Ads.amountFieldValue($form, 'budget');
if ($form.data('disabled')) {
return false;
}
if (budget === false) {
$form.field('budget').focus();
return false;
}
var params = {
owner_id: owner_id,
ad_id: ad_id,
budget: budget
};
$form.data('disabled', true);
Aj.apiRequest('incrAdBudget', params, function(result) {
$form.data('disabled', false);
if (result.error) {
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
Aj.state.$form.reset();
if (result.owner_budget) {
$('.js-owner_budget').html(result.owner_budget);
}
if (result.ad_budget) {
$('.js-ad_budget').html(result.ad_budget);
}
if (result.history) {
$('.js-history').html(result.history);
}
});
return false;
},
eSubmitForm: function(e) {
e.preventDefault();
var $form = Aj.state.$form;
var $button = $(this);
var title = $form.field('title').value();
var text = $form.field('text').value();
var button = $form.field('button').data('value');
var promote_url = $form.field('promote_url').value();
var website_name = $form.field('website_name').value();
var website_photo = $form.field('website_photo').value();
var ad_info = $form.field('ad_info').value();
var cpm = Ads.amountFieldValue($form, 'cpm');
var views_per_user = $form.field('views_per_user').value();
if (!title.length) {
$form.field('title').focus();
return false;
}
if (!text.length) {
$form.field('text').focus();
return false;
}
if (!promote_url.length) {
$form.field('promote_url').focus();
return false;
}
if (cpm === false) {
$form.field('cpm').focus();
return false;
}
var params = {
owner_id: Aj.state.ownerId,
ad_id: Aj.state.adId,
title: title,
text: text,
button: button,
promote_url: promote_url,
website_name: website_name,
website_photo: website_photo,
ad_info: ad_info,
cpm: cpm,
views_per_user: views_per_user
};
if ($form.field('picture').prop('checked')) {
params.picture = 1;
}
$button.prop('disabled', true);
Aj.apiRequest('editAd', params, function(result) {
if (result.error) {
$button.prop('disabled', false);
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
Aj.state.initFormData = EditAd.getFormData($form);
if (result.redirect_to) {
Aj.location(result.redirect_to);
}
});
return false;
},
eSendToReview: function(e) {
e.preventDefault();
var $button = $(this);
if ($button.prop('disabled')) {
return false;
}
var $item = $button.parents('li');
var ad_id = Aj.state.adId;
if ($item.size()) {
$item.parents('.open').find('.dropdown-toggle').dropdown('toggle');
ad_id = $(this).parents('[data-ad-id]').attr('data-ad-id');
}
var params = {
owner_id: Aj.state.ownerId,
ad_id: ad_id
};
$button.prop('disabled', true);
Aj.apiRequest('sendTargetToReview', params, function(result) {
$button.prop('disabled', false);
if (result.error) {
return showAlert(result.error);
}
if (result.toast) {
showToast(result.toast);
}
$button.parents('.pr-decline-block').slideHide('remove');
});
return false;
},
eCloneAd: function(e) {
e.preventDefault();
var $button = $(this);
if ($button.prop('disabled')) {
return false;
}
var $item = $button.parents('li');
var ad_id = Aj.state.adId;
if ($item.size()) {
$item.parents('.open').find('.dropdown-toggle').dropdown('toggle');
ad_id = $(this).parents('[data-ad-id]').attr('data-ad-id');
}
var params = {
owner_id: Aj.state.ownerId,
ad_id: ad_id
};
var onSuccess = function(result) {
$button.prop('disabled', false);
if (result.error) {
return showAlert(result.error);
}
if (result.confirm_text && result.confirm_hash) {
showConfirm(result.confirm_text, function() {
params.confirm_hash = result.confirm_hash;
$button.prop('disabled', true);
Aj.apiRequest('createDraftFromAd', params, onSuccess);
}, result.confirm_btn);
} else if (result.redirect_to) {
Aj.location(result.redirect_to);
}
};
$button.prop('disabled', true);
Aj.apiRequest('createDraftFromAd', params, onSuccess);
return false;
},
deletePopup: function (confirm_text, onConfirm) {
var $confirm = $('');
var confirm = function() {
onConfirm && onConfirm($confirm);
closePopup($confirm);
}
$('.pr-layer-text', $confirm).html(confirm_text);
var $primaryBtn = $('.popup-primary-btn', $confirm);
$primaryBtn.on('click', confirm);
$confirm.one('popup:close', function() {
$primaryBtn.off('click', confirm);
$confirm.remove();
});
openPopup($confirm, {
closeByClickOutside: '.popup-no-close',
});
return $confirm;
},
deleteAd: function(e) {
e.preventDefault();
var $button = $(this);
if ($button.prop('disabled')) {
return false;
}
var $item = $button.parents('li');
var ad_id = Aj.state.adId;
if ($item.size()) {
$item.parents('.open').find('.dropdown-toggle').dropdown('toggle');
ad_id = $(this).parents('[data-ad-id]').attr('data-ad-id');
}
var params = {
owner_id: Aj.state.ownerId,
ad_id: ad_id
};
var onSuccess = function(result) {
$button.prop('disabled', false);
if (result.error) {
return showAlert(result.error);
}
if (result.confirm_text && result.confirm_hash) {
EditAd.deletePopup(result.confirm_text, function() {
params.confirm_hash = result.confirm_hash;
$button.prop('disabled', true);
Aj.apiRequest('deleteAd', params, onSuccess);
});
} else if (result.redirect_to) {
Aj.location(result.redirect_to);
}
};
$button.prop('disabled', true);
Aj.apiRequest('deleteAd', params, onSuccess);
return false;
}
};
var TransferFunds = {
init: function() {
var cont = Aj.ajContainer;
Aj.onLoad(function(state) {
state.$form = $('form.add-funds-form', cont);
Ads.formInit(state.$form);
state.$form.on('submit', preventDefault);
cont.on('click.curPage', '.js-toggle-sign', TransferFunds.onToggleAmountSign);
cont.on('click.curPage', '.transfer-funds-btn', TransferFunds.eSubmitForm);
state.submitBtn = $('.transfer-funds-btn', cont);
state.amountField = state.$form.field('amount');
state.amountField.on('keyup.curPage change.curPage input.curPage', TransferFunds.onAmountChange);
state.decrAmountField = state.$form.field('decr_amount');
state.decrAmountField.on('keyup.curPage change.curPage input.curPage', TransferFunds.onAmountChange);
state.curAmountField = state.$form.hasClass('decr') ? 'decr_amount' : 'amount';
Ads.initSelect(state.$form, 'account', {
items: Aj.state.accountItems || [],
noMultiSelect: true,
renderSelectedItem: function(val, item) {
return '
' + (item.photo ? '
' + item.photo + '
' : '') + '
' + item.name + '
';
},
onChange: TransferFunds.onAccountChange
});
});
Aj.onUnload(function(state) {
Ads.formDestroy(state.$form);
state.$form.off('submit', preventDefault);
state.amountField.off('.curPage');
clearTimeout(Aj.state.transferTo);
});
},
onToggleAmountSign: function(e) {
e.preventDefault();
Aj.state.$form.toggleClass('decr');
Aj.state.curAmountField = Aj.state.$form.hasClass('decr') ? 'decr_amount' : 'amount';
TransferFunds.onAmountChange();
Aj.state.$form.field(Aj.state.curAmountField).focusAndSelectAll();
},
onAccountChange: function(field, value, valueFull) {
if (valueFull.budget) {
$('.js-sel_account_budget', Aj.state.$form).removeClass('disabled').html(valueFull.budget);
} else {
$('.js-sel_account_budget', Aj.state.$form).addClass('disabled').html(Ads.wrapAmount(0));
}
},
onAmountChange: function() {
var decr = Aj.state.curAmountField == 'decr_amount';
var amount = Ads.amountFieldValue(Aj.state.$form, Aj.state.curAmountField) || 0;
if (amount) {
var button_label = l(decr ? 'WEB_WITHDRAW_AMOUNT_BUTTON' : 'WEB_TRANSFER_AMOUNT_BUTTON', {amount: Ads.wrapAmount(amount)});
} else {
var button_label = l(decr ? 'WEB_WITHDRAW_FUNDS_BUTTON' : 'WEB_TRANSFER_FUNDS_BUTTON');
}
Aj.state.submitBtn.prop('disabled', !amount).html(button_label);
},
eSubmitForm: function(e) {
e.preventDefault();
var $form = Aj.state.$form;
var $button = $(this);
var decr = $form.hasClass('decr');
var account_id = $form.field('account').data('value');
var amount_field = decr ? 'decr_amount' : 'amount';
var amount = Ads.amountFieldValue($form, amount_field);
if ($button.prop('disabled')) {
return false;
}
if (!account_id) {
$form.field('account').trigger('click');
return false;
}
if (amount === false) {
$form.field(amount_field).focus();
return false;
}
var method = decr ? 'transferWithdrawFunds' : 'transferFunds';
var params = {
owner_id: Aj.state.ownerId,
account_id: account_id,
amount: amount
};
var onSuccess = function(result) {
$button.prop('disabled', false);
if (result.error) {
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
if (result.budget) {
$('.js-owner_budget').html(result.budget);
}
return showAlert(result.error);
}
if (result.confirm_text && result.confirm_hash) {
showConfirm(result.confirm_text, function() {
params.confirm_hash = result.confirm_hash;
$button.prop('disabled', true);
Aj.apiRequest(method, params, onSuccess);
}, result.confirm_btn);
} else if (result.request_id) {
$button.prop('disabled', true);
Aj.state.transferTo = setTimeout(function() {
params.request_id = result.request_id;
Aj.apiRequest(method, params, onSuccess);
}, 400);
} else if (result.redirect_to) {
Aj.location(result.redirect_to);
}
};
$button.prop('disabled', true);
Aj.apiRequest(method, params, onSuccess);
return false;
}
};
var Audiences = {
init: function() {
var cont = Aj.ajContainer;
Aj.onLoad(function(state) {
state.$searchField = $('.pr-search-input');
state.$searchResults = $('.pr-table tbody');
Ads.fieldInit(state.$searchField);
cont.on('click.curPage', '.pr-cell-sort', Audiences.eSortList);
cont.on('click.curPage', '.js-create-audience-ad-btn', Audiences.createAudienceAd);
cont.on('click.curPage', '.delete-audience-btn', Audiences.deleteAudience);
state.$searchResults.on('mouseover mouseout click', '.js-hint-tooltip', Ads.eHintEvent);
$(document).on('touchstart click', Ads.eHideAllHints);
state.listInited = false;
state.needUpdateState = false;
state.$searchField.initSearch({
$results: state.$searchResults,
emptyQueryEnabled: true,
updateOnInit: true,
resultsNotScrollable: true,
itemTagName: 'tr',
enterEnabled: function() {
return false;
},
renderItem: function(item, query) {
return '
' + item.title + ' | ' + Ads.formatTableDate(item.date) + ' | ' + item.used + ' | ' + item.users + (item.processing_hint ? ' ' : '') + ' | ' + (item.need_update ? '' : Aj.state.audienceDropdownTpl.replace(/{audience_id}/g, item.audience_id)) + ' | ';
},
getData: function() {
if (!state.listInited) {
state.listInited = true;
var items = Aj.state.audiencesList;
for (var i = 0; i < items.length; i++) {
var item = items[i];
item.base_url = '/account/audience/' + item.audience_id;
item._values = [item.title.toLowerCase()];
if (item.need_update) {
state.needUpdateState = true;
}
}
Audiences.updateAudiencesState();
}
return Aj.state.audiencesList;
}
});
});
Aj.onUnload(function(state) {
state.$searchResults.off('mouseover mouseout click', '.js-hint-tooltip', Ads.eHintEvent);
$(document).off('touchstart click', Ads.eHideAllHints);
clearTimeout(Aj.state.updateStateTo);
Ads.fieldDestroy(state.$searchField);
state.$searchField.destroySearch();
});
},
eSortList: function(e) {
var $sortEl = $(this);
var sortBy = $sortEl.attr('data-sort-by');
var sortAsc = $sortEl.hasClass('sort-asc');
if (sortBy == Aj.state.audiencesListSortBy) {
Aj.state.audiencesListSortAsc = !sortAsc;
} else {
Aj.state.audiencesListSortBy = sortBy;
Aj.state.audiencesListSortAsc = false;
}
Audiences.updateAudiencesList();
Aj.state.$searchField.trigger('datachange');
},
updateAudiencesList: function() {
if (Aj.state.audiencesList) {
var sortBy = Aj.state.audiencesListSortBy;
var sortAsc = Aj.state.audiencesListSortAsc;
$('.pr-cell-sort').each(function() {
var $sortEl = $(this);
var curSortBy = $sortEl.attr('data-sort-by');
$sortEl.toggleClass('sort-active', sortBy == curSortBy);
$sortEl.toggleClass('sort-asc', sortAsc && sortBy == curSortBy);
});
Aj.state.audiencesList.sort(function(ad1, ad2) {
var v1 = sortAsc ? ad1 : ad2;
var v2 = sortAsc ? ad2 : ad1;
return (v1[sortBy] - v2[sortBy]) || (v1.date - v2.date);
});
}
},
updateAudiencesState: function() {
if (!Aj.state || !Aj.state.audiencesList || !Aj.state.needUpdateState) {
return;
}
Aj.state.needUpdateState = false;
Aj.state.updateStateTo = setTimeout(function() {
Aj.apiRequest('updateAudiencesState', {
owner_id: Aj.state.ownerId
}, function(result) {
if (result.error) {
return showAlert(result.error);
}
if (result.audiences) {
for (var i = 0; i < result.audiences.length; i++) {
Audiences.updateAudience(result.audiences[i], true);
}
Audiences.updateAudiencesList();
Aj.state.$searchField.trigger('contentchange');
Audiences.updateAudiencesState();
}
});
}, 400);
},
updateAudience: function(audience, no_update) {
if (!Aj.state || !Aj.state.audiencesList) {
return;
}
var audiencesList = Aj.state.audiencesList;
for (var i = 0; i < audiencesList.length; i++) {
if (audience.owner_id == audiencesList[i].owner_id &&
audience.audience_id == audiencesList[i].audience_id) {
audience.base_url = '/account/audience/' + audience.audience_id;
audience._values = [audience.title.toLowerCase()];
audiencesList[i] = audience;
if (audience.need_update) {
Aj.state.needUpdateState = true;
}
if (!no_update) {
Audiences.updateAudiencesList();
Aj.state.$searchField.trigger('contentchange');
Audiences.updateAudiencesState();
}
return;
}
}
},
initCreatePopup: function() {
var cont = Aj.layer;
Aj.onLayerLoad(function(layerState) {
layerState.$form = $('.pr-popup-edit-form', cont);
Ads.formInit(layerState.$form);
layerState.titleField = layerState.$form.field('title');
layerState.titleField.on('change.curPage', NewAd.onTitleChange);
Aj.layer.one('popup:open', function() {
layerState.titleField.focusAndSelect(true);
});
layerState.$form.on('submit', Audiences.eSubmitCreatePopupForm);
cont.on('click.curLayer', '.submit-form-btn', Audiences.eSubmitCreatePopupForm);
});
Aj.onLayerUnload(function(layerState) {
if (Aj.layerState.uploadRequestXhr) {
Aj.layerState.uploadRequestXhr.abort();
}
Ads.formDestroy(layerState.$form);
layerState.$form.off('submit', Audiences.eSubmitCreatePopupForm);
layerState.titleField.off('.curPage');
});
},
eSubmitCreatePopupForm: function(e) {
e.preventDefault();
var $form = Aj.layerState.$form;
var owner_id = $form.field('owner_id').value();
var title = $form.field('title').value();
var $fileEl = $form.field('file');
var file = $fileEl.data('file');
if ($form.data('disabled')) {
return false;
}
if (!title.length) {
$form.field('title').focus();
return false;
}
if (!file) {
$form.field('file').focus();
return false;
}
var params = {
owner_id: owner_id,
title: title
};
var $formGroup = $fileEl.parents('.form-group');
$formGroup.addClass('field-loading');
$form.addClass('disabled').data('disabled', true);
Aj.layerState.uploadRequestXhr = Aj.uploadRequest('createAudience', file, params, function(result) {
Aj.layerState.uploadRequestXhr = null;
$form.removeClass('disabled').data('disabled', false);
$formGroup.removeClass('field-loading');
if (result.error) {
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
closePopup(Aj.layer);
if (result.audience && Aj.state.audiencesList) {
Aj.state.audiencesList.push(result.audience);
Audiences.updateAudience(result.audience);
}
if (result.audience_opt && Aj.state.audienceItems) {
Aj.state.audienceItems.push(result.audience_opt);
for (var i = 0; i < Aj.state.selectList.length; i++) {
var selectData = Aj.state.selectList[i];
if (selectData.items_key == 'audienceItems') {
var $fieldEl = Aj.state.$form.field(selectData.field);
$fieldEl.trigger('datachange');
if (selectData.add_new_audience) {
$fieldEl.trigger('selectval', [result.audience_opt, true]);
}
}
}
}
}, function(loaded, total) {
var progress = total ? loaded / total : 0;
$('.js-progress-value', $formGroup).html(Math.round(progress * 100) + '%');
$formGroup.each(function() {
this.style.setProperty('--upload-progress', progress);
});
});
return false;
},
initEditTitlePopup: function() {
var cont = Aj.layer;
Aj.onLayerLoad(function(layerState) {
layerState.$form = $('.pr-popup-edit-form', cont);
Ads.formInit(layerState.$form);
layerState.titleField = layerState.$form.field('title');
layerState.titleField.on('change.curPage', NewAd.onTitleChange);
Aj.layer.one('popup:open', function() {
layerState.titleField.focusAndSelect(true);
});
layerState.$form.on('submit', Audiences.eSubmitEditTitlePopupForm);
cont.on('click.curLayer', '.submit-form-btn', Audiences.eSubmitEditTitlePopupForm);
});
Aj.onLayerUnload(function(layerState) {
Ads.formDestroy(layerState.$form);
layerState.$form.off('submit', Audiences.eSubmitEditTitlePopupForm);
layerState.titleField.off('.curPage');
});
},
eSubmitEditTitlePopupForm: function(e) {
e.preventDefault();
var $form = Aj.layerState.$form;
var owner_id = $form.field('owner_id').value();
var audience_id = $form.field('audience_id').value();
var title = $form.field('title').value();
if ($form.data('disabled')) {
return false;
}
if (!title.length) {
$form.field('title').focus();
return false;
}
var params = {
owner_id: owner_id,
audience_id: audience_id,
title: title
};
$form.data('disabled', true);
Aj.apiRequest('editAudienceTitle', params, function(result) {
$form.data('disabled', false);
if (result.error) {
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
closePopup(Aj.layer);
if (result.audience) {
Audiences.updateAudience(result.audience);
}
});
return false;
},
initUpdateUsersPopup: function() {
var cont = Aj.layer;
Aj.onLayerLoad(function(layerState) {
layerState.$form = $('.pr-popup-edit-form', cont);
Ads.formInit(layerState.$form);
layerState.$form.on('submit', Audiences.eSubmitUpdateUsersPopupForm);
cont.on('click.curLayer', '.submit-form-btn', Audiences.eSubmitUpdateUsersPopupForm);
});
Aj.onLayerUnload(function(layerState) {
if (Aj.layerState.uploadRequestXhr) {
Aj.layerState.uploadRequestXhr.abort();
}
Ads.formDestroy(layerState.$form);
layerState.$form.off('submit', Audiences.eSubmitUpdateUsersPopupForm);
});
},
eSubmitUpdateUsersPopupForm: function(e) {
e.preventDefault();
var $form = Aj.layerState.$form;
var owner_id = $form.field('owner_id').value();
var audience_id = $form.field('audience_id').value();
var $fileEl = $form.field('file');
var file = $fileEl.data('file');
if ($form.data('disabled')) {
return false;
}
if (!file) {
$form.field('file').focus();
return false;
}
var params = {
owner_id: owner_id,
audience_id: audience_id
};
var $formGroup = $fileEl.parents('.form-group');
$formGroup.addClass('field-loading');
$form.addClass('disabled').data('disabled', true);
var method = Aj.layerState.updateMethod;
Aj.layerState.uploadRequestXhr = Aj.uploadRequest(method, file, params, function(result) {
Aj.layerState.uploadRequestXhr = null;
$form.removeClass('disabled').data('disabled', false);
$formGroup.removeClass('field-loading');
if (result.error) {
if (result.field) {
var $field = $form.field(result.field);
if ($field.size()) {
Ads.showFieldError($field, result.error, true);
return false;
}
}
return showAlert(result.error);
}
closePopup(Aj.layer);
if (result.audience) {
Audiences.updateAudience(result.audience);
}
}, function(loaded, total) {
var progress = total ? loaded / total : 0;
$('.js-progress-value', $formGroup).html(Math.round(progress * 100) + '%');
$formGroup.each(function() {
this.style.setProperty('--upload-progress', progress);
});
});
return false;
},
createAudienceAd: function(e) {
e.preventDefault();
var $button = $(this);
if ($button.prop('disabled')) {
return false;
}
var $item = $button.parents('li');
var audience_id = Aj.state.audienceId;
if ($item.size()) {
$item.parents('.open').find('.dropdown-toggle').dropdown('toggle');
audience_id = $(this).parents('[data-audience-id]').attr('data-audience-id');
}
var params = {
owner_id: Aj.state.ownerId,
audience_id: audience_id
};
var onSuccess = function(result) {
$button.prop('disabled', false);
if (result.error) {
return showAlert(result.error);
}
if (result.confirm_text && result.confirm_hash) {
showConfirm(result.confirm_text, function() {
params.confirm_hash = result.confirm_hash;
$button.prop('disabled', true);
Aj.apiRequest('createDraftFromAudience', params, onSuccess);
}, result.confirm_btn);
} else if (result.redirect_to) {
Aj.location(result.redirect_to);
}
};
$button.prop('disabled', true);
Aj.apiRequest('createDraftFromAudience', params, onSuccess);
return false;
},
deletePopup: function (confirm_text, onConfirm) {
var $confirm = $('');
var confirm = function() {
onConfirm && onConfirm($confirm);
closePopup($confirm);
}
$('.pr-layer-text', $confirm).html(confirm_text);
var $primaryBtn = $('.popup-primary-btn', $confirm);
$primaryBtn.on('click', confirm);
$confirm.one('popup:close', function() {
$primaryBtn.off('click', confirm);
$confirm.remove();
});
openPopup($confirm, {
closeByClickOutside: '.popup-no-close',
});
return $confirm;
},
deleteAudience: function(e) {
e.preventDefault();
var $button = $(this);
if ($button.prop('disabled')) {
return false;
}
var $item = $button.parents('li');
var audience_id = Aj.state.audienceId;
if ($item.size()) {
$item.parents('.open').find('.dropdown-toggle').dropdown('toggle');
audience_id = $(this).parents('[data-audience-id]').attr('data-audience-id');
}
var params = {
owner_id: Aj.state.ownerId,
audience_id: audience_id
};
var onSuccess = function(result) {
$button.prop('disabled', false);
if (result.error) {
return showAlert(result.error);
}
if (result.confirm_text && result.confirm_hash) {
Audiences.deletePopup(result.confirm_text, function() {
params.confirm_hash = result.confirm_hash;
$button.prop('disabled', true);
Aj.apiRequest('deleteAudience', params, onSuccess);
});
} else if (result.redirect_to) {
Aj.location(result.redirect_to);
}
};
$button.prop('disabled', true);
Aj.apiRequest('deleteAudience', params, onSuccess);
return false;
}
};
(function(d){var c=function(a){this._options={checkOnLoad:!1,resetOnEnd:!1,loopCheckTime:50,loopMaxNumber:5,baitClass:"pub_300x250 pub_300x250m pub_728x90 text-ad textAd text_ad text_ads text-ads text-ad-links ads-header ads-content",baitStyle:"width: 1px !important; height: 1px !important; position: absolute !important; left: -10000px !important; top: -1000px !important;"};this._var={version:"3.2.1",bait:null,checking:!1,loop:null,loopNumber:0,event:{detected:[],notDetected:[]}};void 0!==a&&this.setOption(a);var b=this;a=function(){setTimeout(function(){!0===b._options.checkOnLoad&&(null===b._var.bait&&b._creatBait(),setTimeout(function(){b.check()},1))},1)};void 0!==d.addEventListener?d.addEventListener("load",a,!1):d.attachEvent("onload",a)};c.prototype._options=null;c.prototype._var=null;c.prototype._bait=null;c.prototype.setOption=function(a,b){if(void 0!==b){var e=a;a={};a[e]=b}for(var f in a)this._options[f]=a[f];return this};c.prototype._creatBait=function(){var a=document.createElement("div");a.setAttribute("class",this._options.baitClass);a.setAttribute("style",this._options.baitStyle);this._var.bait=d.document.body.appendChild(a);this._var.bait.offsetParent;this._var.bait.offsetHeight;this._var.bait.offsetLeft;this._var.bait.offsetTop;this._var.bait.offsetWidth;this._var.bait.clientHeight;this._var.bait.clientWidth};c.prototype._destroyBait=function(){d.document.body.removeChild(this._var.bait);this._var.bait=null};c.prototype.check=function(a){void 0===a&&(a=!0);this._var.checking=!0;null===this._var.bait&&this._creatBait();var b=this;this._var.loopNumber=0;!0===a&&(this._var.loop=setInterval(function(){b._checkBait(a)},this._options.loopCheckTime));setTimeout(function(){b._checkBait(a)},1);return!0};c.prototype._checkBait=function(a){var b=!1;null===this._var.bait&&this._creatBait();if(null!==d.document.body.getAttribute("abp")||null===this._var.bait.offsetParent||0==this._var.bait.offsetHeight||0==this._var.bait.offsetLeft||0==this._var.bait.offsetTop||0==this._var.bait.offsetWidth||0==this._var.bait.clientHeight||0==this._var.bait.clientWidth)b=!0;if(void 0!==d.getComputedStyle){var e=d.getComputedStyle(this._var.bait,null);!e||"none"!=e.getPropertyValue("display")&&"hidden"!=e.getPropertyValue("visibility")||(b=!0)}!0===a&&(this._var.loopNumber++,this._var.loopNumber>=this._options.loopMaxNumber&&this._stopLoop());if(!0===b)this._stopLoop(),this._destroyBait(),this.emitEvent(!0),!0===a&&(this._var.checking=!1);else if(null===this._var.loop||!1===a)this._destroyBait(),this.emitEvent(!1),!0===a&&(this._var.checking=!1)};c.prototype._stopLoop=function(a){clearInterval(this._var.loop);this._var.loop=null;this._var.loopNumber=0};c.prototype.emitEvent=function(a){a=this._var.event[!0===a?"detected":"notDetected"];for(var b in a)if(a.hasOwnProperty(b))a[b]();!0===this._options.resetOnEnd&&this.clearEvent();return this};c.prototype.clearEvent=function(){this._var.event.detected=[];this._var.event.notDetected=[]};c.prototype.on=function(a){this._var.event.detected.push(a);return this};d.ABC=c;void 0===d.AB&&(d.AB=new c({checkOnLoad:!0,resetOnEnd:!0}))})(window);
AB.on(function() {
openPopup('');
});