Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNUM-7: Add "GO" under "Caller ID Numbers" tab #1048

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion src/apps/common/submodules/numbers/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ define(function(require) {
viewType: data.viewType,
canAddExternalCids: monster.util.getCapability('caller_id.external_numbers').isEnabled,
canAddExternalNumbers: monster.util.canAddExternalNumbers(),
listAccounts: []
listAccounts: [],
listExternalNumbersAccounts: []
};

/* Initializing accounts metadata */
Expand Down Expand Up @@ -173,6 +174,19 @@ define(function(require) {
}
});

/* Save a list of the acccounts that have an associated external number */
_.each(mapAccounts, function(value) {
self.listAccountExternalNumbers(value.id, function(data) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The numbers should not be pre-loaded, because one request will be done per sub-account, and if the current account has a lot of sub-accounts, it will hit the server a lot of times. Also this is unnecessary, because these numbers are not displayed until the account is expanded in the view.

See how this works in the "Numbers" app, in the "Spare numbers" or "Numbers in use" tab.

if (data.length > 0) {
_.each(data, function(element) {
element.accountId = value.id;
});
templateData.listExternalNumbersAccounts.push(data);
templateData.listExternalNumbersAccounts = _.flatten(templateData.listExternalNumbersAccounts);
}
});
});

/* Order the subaccount list by name */
templateData.listAccounts.sort(function(a, b) {
return a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1;
Expand Down Expand Up @@ -811,6 +825,47 @@ define(function(require) {
});
};

var searchExternalNumber = function(searchString, parent) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the sub-accounts' numbers will no longer be pre-loaded, this needs to be refactored. Check how the function searchListNumbers that is a few lines above works and use it as an example, or if possible use that same function, as it should work in a similar way for the 3 number tabs.

var viewList = parent,
searchString = monster.util.unformatPhoneNumber(searchString),
foundNumber = _.find(dataNumbers.listExternalNumbersAccounts, { number: searchString }),
accountId = foundNumber ? foundNumber.accountId : '',
numberId = foundNumber ? foundNumber.id : '';

if (accountId) {
var section = viewList.find('[data-id="' + accountId + '"]'),
toggle = function() {
section.addClass('open');

var numberBox = parent.find('.number-box[data-id="' + numberId + '"]');

if (!section.hasClass('open')) {
section.find('input[type="checkbox"]:checked').prop('checked', false);
section.find('.number-box.selected').removeClass('selected');
}

monster.ui.highlight(numberBox, {
timer: 5000
});

_.each(dataNumbers.listAccounts, function(account) {
if (account.id === accountId) {
account.open = section.hasClass('open') ? 'open' : '';
}
});
};

displayNumberList(accountId, function() {
toggle();
});
} else {
monster.ui.toast({
type: 'error',
message: self.i18n.active().numbers.numberNotFound
});
}
};

parent.on('click', '.list-numbers[data-type="spare"] button.search-numbers', function(e) {
var spareList = parent.find('.list-numbers[data-type="spare"]'),
searchString = spareList.find('.search-custom input[type="text"]').val().toLowerCase();
Expand Down Expand Up @@ -847,6 +902,24 @@ define(function(require) {
}
});

parent.on('click', '.list-numbers[data-type="external"] button.search-numbers', function(e) {
var externalList = parent.find('.list-numbers[data-type="external"]'),
searchString = externalList.find('.search-custom input[type="text"]').val().toLowerCase();

searchExternalNumber(searchString, externalList);
});

parent.on('keyup', '.list-numbers[data-type="external"] .search-custom input[type="text"]', function(e) {
if (e.keyCode === 13) {
var val = e.target.value.toLowerCase(),
externalList = parent.find('.list-numbers[data-type="external"]');

if (val) {
searchExternalNumber(val, externalList);
}
}
});

self.numbersBindExternalNumbersEvents(parent, dataNumbers);
},

Expand Down Expand Up @@ -1539,6 +1612,21 @@ define(function(require) {
});
},

listAccountExternalNumbers: function(accountId, success) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the functions in a submodule should have the submodule name as prefix. In this case it would be something like numbersListExternalNumbers. Also, having the API call for externalNumbers.list as a separate function, it would be a good time to use it in here: https://github.com/2600hz/monster-ui/blob/master/src/apps/common/submodules/numbers/numbers.js#L1729

var self = this;

self.callApi({
resource: 'externalNumbers.list',
data: {
accountId: accountId,
generateError: false
},
success: function(_data, status) {
success && success(_data.data);
}
});
},

numbersDelete: function(args, callback) {
var self = this;

Expand Down
7 changes: 7 additions & 0 deletions src/apps/common/submodules/numbers/views/external.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<div class="actions-wrapper">
<div class="form-inline">
<div class="input-append pull-right search-custom">
<input class="span2" type="text" placeholder="{{ i18n.numbers.searchNumbers }}"/>
<button class="monster-button monster-button-success non-fixed search-numbers" type="button">
{{ i18n.numbers.go }}
</button>
</div>

<label>
{{ i18n.numbers.numbers }}
</label>
Expand Down