Got rid of crazy user name split

instead decorate with <wbr> to hint on best
spot to split
This commit is contained in:
Sam 2013-12-23 11:11:41 +11:00
parent 90077ae98d
commit d4819c3a65
5 changed files with 29 additions and 38 deletions

View File

@ -9,7 +9,7 @@ Handlebars.registerHelper('breakUp', function(property, options) {
prop = Ember.Handlebars.get(this, property, options); prop = Ember.Handlebars.get(this, property, options);
if (!prop) return ""; if (!prop) return "";
return Discourse.Formatter.breakUp(prop, 13); return Discourse.Formatter.breakUp(prop);
}); });
/** /**

View File

@ -6,39 +6,30 @@ Discourse.Formatter = (function(){
relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase, relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase,
shortDate, shortDateNoYear, tinyDateYear, breakUp; shortDate, shortDateNoYear, tinyDateYear, breakUp;
breakUp = function(string, maxLength){ breakUp = function(str){
if(string.length <= maxLength) { var rval = [];
return string; var prev = str[0];
} var cur;
var firstPart = string.substr(0, maxLength); rval.push(prev);
for (var i=1;i<str.length;i++) {
// work backward to split stuff like ABPoop to AB Poop cur = str[i];
var i; if(prev.match(/[^0-9]/) && cur.match(/[0-9]/)){
for(i=firstPart.length-1;i>0;i--){ rval.push("<wbr>");
if(firstPart[i].match(/[A-Z]/)){ } else if(i>1 && prev.match(/[A-Z]/) && cur.match(/[a-z]/)){
break; rval.pop();
rval.push("<wbr>");
rval.push(prev);
} else if(prev.match(/[^A-Za-z0-9]/) && cur.match(/[a-zA-Z0-9]/)){
rval.push("<wbr>");
} }
rval.push(cur);
prev = cur;
} }
// work forwards to split stuff like ab111 to ab 111 return rval.join("");
if(i===0) {
for(i=1;i<firstPart.length;i++){
if(firstPart[i].match(/[^a-z]/)){
break;
}
}
}
if (i > 0 && i < firstPart.length) {
var offset = 0;
if(string[i] === "_") {
offset = 1;
}
return string.substr(0, i + offset) + " " + string.substring(i + offset);
} else {
return firstPart + " " + string.substr(maxLength);
}
}; };
shortDate = function(date){ shortDate = function(date){

View File

@ -4,7 +4,7 @@
<div> <div>
<a href='/users/{{unbound username}}'>{{avatar this imageSize="large"}}</a> <a href='/users/{{unbound username}}'>{{avatar this imageSize="large"}}</a>
</div> </div>
<h5 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}'>{{breakUp username}}</a></h5> <h5 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}'>{{{breakUp username}}}</a></h5>
</div> </div>
</div> </div>
<div class='span11 topic-body'> <div class='span11 topic-body'>

View File

@ -22,7 +22,7 @@
{{#unless userDeleted}} {{#unless userDeleted}}
<div {{bindAttr class=":contents byTopicCreator:topic-creator :trigger-expansion"}}> <div {{bindAttr class=":contents byTopicCreator:topic-creator :trigger-expansion"}}>
<a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{avatar this imageSize="large"}}</a> <a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{avatar this imageSize="large"}}</a>
<h3 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{breakUp username}}</a></h3> <h3 {{bindAttr class="staff new_user"}}><a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{{breakUp username}}}</a></h3>
{{#if showName}} {{#if showName}}
<h3><a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{breakUp name}}</a></h3> <h3><a href='{{unbound usernameUrl}}' {{action showPosterExpansion this}}>{{breakUp name}}</a></h3>

View File

@ -192,13 +192,13 @@ test("updateRelativeAge", function(){
test("breakUp", function(){ test("breakUp", function(){
var b = function(s){ return Discourse.Formatter.breakUp(s,5); }; var b = function(s){ return Discourse.Formatter.breakUp(s); };
equal(b("hello"), "hello"); equal(b("hello"), "hello");
equal(b("helloworld"), "hello world"); equal(b("helloworld"), "helloworld");
equal(b("HeMans"), "He Mans"); equal(b("HeMans11"), "He<wbr>Mans<wbr>11");
equal(b("he_man"), "he_ man"); equal(b("he_man"), "he_<wbr>man");
equal(b("he11111"), "he 11111"); equal(b("he11111"), "he<wbr>11111");
equal(b("HRCBob"), "HRC Bob"); equal(b("HRCBob"), "HRC<wbr>Bob");
}); });