y5.require(['{Friends}.YauserSearch', '{Friends}.CommonConstructor'], function() { var Yauser = Friends.CommonConstructor(Friends.YauserSearch); Yauser.prototype.init = function() { Friends.YauserSearch.prototype.init.apply(this, arguments); this.insertError_b = y5.Dom.getDescendants(this.control, 'b', 'b-note-attention')[1]; this.yauser_span = y5.Dom.getDescendant(this.control, 'b', 'b-user'); this.yauserPreview_div = y5.Dom.getDescendant(this.control, 'div', 'b-friend-name-preview'); this.toChange_link = y5.Dom.getDescendant(this.control, 'a', 'to-change'); this.yauserName_input = y5.cssQuery('input[name="yauser-name"]', this.control)[0]; this.dialogBtns = y5.Dom.getDescendant(this.control, 'div', 'y5-w-buttons'); this.ajaxMethod_search = 'ajax/search_users_by_name'; this.ajaxMethod_get = 'ajax/get_friends'; // навешиваем события y5.Events.observe('y5:userIsSelected', this.showPreviewBlock, this.friendControl, true, this); y5.Events.observe('y5:usersAreFound', this.showSearchResult, this.friendControl, true, this); y5.Events.observe('y5:resetSearchResults', this.resetSearchResults, this.friendControl, true, this); y5.Events.observe('y5:hideErrorMessages', function() { y5.Classes.add(this.insertError_b, 'i-hidden'); }, this.friendControl, true, this); y5.Events.observe('click', function(evt) { this.hideErrorMessages(); this.yauserName_input.value = this.yauserName; y5.Classes.add(this.yauserPreview_div, 'b-friend-name-preview-edit'); this.yauserName_input.focus(); this.yauserName_input.select(); evt.preventDefault(); }, this.toChange_link, true, this); if (!this.has_been_inited) { y5.Events.observe('y5:show', function(evt) { if (typeof evt.caller === "object") { this.caller = evt.caller; this.caller_params = evt.caller_params || {}; this.title = this.caller_params.title || 'Вставить ссылку на страницу пользователя Я.ру'; this.showWidget(); } }, Yauser, true, this); y5.Events.observe('y5:make', function(widget) { this.dialog = widget; this.control = widget.window; var el = this.dialog.element; this.init(this.control); y5.ShortCut.down([{key: y5.ShortCut.ENTER}], function(e) { // dispatch'им click по кнопке 'Вставить' y5.Events.notify('click', this.dialog.button_ok); }, this.yauserName_input, {checkTarget: false, context: this}); y5.Events.notify('y5:yauser_make', this.caller, true, {caller:this}); this.initDialogEvents(); }, this.getWidgetInitElement(), true, this); this.has_been_inited = true; } y5.loaded('{Friends}.YauserSearchInPostInstance'); }; // показываем результат поиска: список найденных пользователей и карточку выбранного пользователя Yauser.prototype.showSearchResult = function() { y5.Classes.remove(this.usersList_table, 'i-hidden'); }; // сбрасываем результаты поиска Yauser.prototype.resetSearchResults = function() { this.yauserLogin = ''; this.yauserName = ''; this.yauser_span.innerHTML = ''; // скрываем реузальтат поиска: список, карточку, preview-блок "Что получается", кнопки y5.Classes.add(this.usersList_table, 'i-hidden'); y5.Classes.add(this.yauserPreview_div, 'i-hidden'); if (this.dialogBtns) { y5.Classes.add(this.dialogBtns, 'i-hidden'); } }; // показываем preview-блок "Что получается" Yauser.prototype.showPreviewBlock = function() { y5.Classes.add(this.insertError_b, 'i-hidden'); if (!this.caller_params.dont_preview) { y5.Classes.remove(this.yauserPreview_div, 'i-hidden'); y5.Classes.remove(this.yauserPreview_div, 'b-friend-name-preview-edit'); } var s = this.users_select; $(".b-friend-name-preview").yhide(); if( s.options && s.options.length ){ var selectedOption = s.options[this.users_select.selectedIndex]; this.yauserLogin = selectedOption.id; // т.к. в selectedOption.text выводится сокращенное имя, то полное имя было записано как параметр в onclick var data = selectedOption.onclick(); this.yauserName = data.title; this.data = data; this.data.id = selectedOption.value; this.setYauserName(); $(".b-friend-name-preview").yshow(); y5.Classes.remove(this.dialogBtns, 'i-hidden'); } }; // записываем в span preview-блока полное имя выбранного пользователя Yauser.prototype.setYauserName = function() { this.yauser_span.innerHTML = this.createYauserLink(this.yauserLogin, this.yauserName); }; // TODO: заменить глобальной функцией yauser(user, needQu), когда в y5.Dom появится функция replaceNode Yauser.prototype.createYauserLink = function(login, name) { var firstCharacter = name.substring(0, 1); var otherCharacters = name.substring(1); return '' + '' + ya.xmlEscape(firstCharacter) + '' + ya.xmlEscape(otherCharacters) + ''; }; // определяем имя пользователя с учетом значения поля "Что получается" Yauser.prototype.defineYauserName = function() { if (!y5.Strings.isVoid(this.yauserName_input.value)) { this.yauserName = this.yauserName_input.value; this.setYauserName(); } }; Yauser.prototype.getWidgetInitElement = function() { var res = this.dialog ? this.dialog.element : this.control; return res; }; Yauser.prototype.showWidget = function() { var make_params = { title: this.title }, params = this.caller_params; if ("valign" in params) { make_params.valign = params.valign; } if ("halign" in params) { make_params.halign = params.halign; } var dialog = y5.Widget.make( this.getWidgetInitElement(), 'Widget.Dialog.Standard', make_params ); }; Yauser.prototype.showErrorMessage = function(errorNode, msg) { if (msg) { errorNode.innerHTML = msg; } y5.Classes.remove(errorNode, 'i-hidden'); }; Yauser.prototype.initDialogEvents = function() { var el = this.dialog.element; y5.Events.observe('y5:ok', this.onDialogOK, el, true, this); y5.Events.observe('y5:esc', this.onDialogEsc, el, true, this); y5.Events.observe('y5:cancel', this.onDialogCancel, el, true, this); this.initDialogEvents = function() {}; }; Yauser.prototype.onDialogOK = function() { //console.log('onDialogOk'); this.hideErrorMessages(); this.defineYauserName(); if (this.yauserName.length) { y5.Events.notify('y5:yauser_defined', this.caller, true, {login:this.yauserLogin, name:this.yauserName, inflects: this.data.inflects, id: this.data.id}); } else { this.showErrorMessage(this.insertError_b, 'Не задано имя пользователя'); return false; } return true; }; Yauser.prototype.onDialogEsc = function() { //console.log('onDialogEsc'); y5.Events.notify('y5:yauser_src', this.caller, true, {caller: this}); return true; }; Yauser.prototype.onDialogCancel = function() { //console.log('onDialogCancel'); y5.Events.notify('y5:yauser_cancel', this.caller, true, {caller: this}); return true; }; Friends.YauserSearchInPost = Yauser; y5.loaded('{Friends}.YauserSearchInPost'); });