ASP MVC - CSS Image Sprites into a View - css

I have a huge css file (Content/Site.css) which includes also more than 200 lines of spirits.
I wanna make a View/PartialView which shows all my sprites images ( many divs ) .
How I do it?
How to read and parse css file in code behind from a controller and then put my result into a ViewBag?
Or there is no need to parse from server side and just show it from client side?
div.feedImgPop,
div.feedImg,
div.feedImgList { background: url("http://xxx/Sprite.jpg") top left no-repeat; }
div.a{ background-position:0 0; }
div.b{ background-position:0 -43px; }
... 200 lines..
asp net mvc 4.5
Update :
Splited CSS file into 2 files , sprite and the rest
Used CSS Parser to read the sprite css file from a controller
Show the sprite images as shown below

I used this css parser
Controller :
public ActionResult Admin()
{
parser.ReadCSSFile(Server.MapPath("~/Content/Sprite.css"));
var cssItems = new List<String>(cssList.Count);
foreach (var item in cssList) cssItems.Add(item.Key.Replace("div.", ""));
ViewBag.items = cssItems;
return View();
}
View : ( CSS Solution - I use this )
<div class="wrapper">
<div class="container">
#foreach (var item in ViewBag.items)
{
string className = "imgContainer imgHover feedImgPop " + #item;
<div class="#className" onClick="adminChooseSpriteImage('#className')"></div>
}
</div>
</div>
View : (Table Solution - 10 items each row ; previous test )
<div id="feedsContainer">
#{
int rowNumber = 1;
<table>
<tr>
#foreach(var item in ViewBag.items)
{
if (rowNumber % 11 == 0)
{
<tr></tr>
}
else
{
string className = "imgContainer imgHover feedImgPop " + #item;
<td><div class="#className"></div></td>
}
rowNumber++;
}
</tr>
</table>
}
</div>

Related

Protractor: Locating element that contains multiple divs and no unique css to identify them

Just wanted to share: If you are in a situation where there are no classes within a certain section and the value of that particular div cannot be hardcoded and changes, using protractor e2e framework I have managed to locate the specific div using this method:
Adding an example of html that does not have a class for every element
<div class="row">
<div class="page_banner">A Dude's Profile</div>
<div class="profile_details">
<div class="profile_name">
<h3>Tony Adams</h3>
</div>
<div>ta#bogus.com</div>
<div>0883424324</div>
</div>
</div>
In the case where you need to say identify that there is a unique mobile number, so the value is not consistent.
function mobileNumberAssert() {
element.all(by.css('.profile_details'))
.get(1) // number of divs in css
.getText()
.then(function(textFoundInCss) {
if(textFoundInCss > 10) {
return true;
console.log('there is a mobile number present with 10 digits');
} else {
return false;
}
});
}
For Debugging you can console log the "textFoundInCss" working your way to locate that particular div.
Since it seems like you are looking for an element based on the text you could use by.cssContainingText() to make this a lot easier.
const el = element(by.cssContainingText('div', `Text I'm looking for`);
el.isPresent().then(isPresent => {
if (isPresent) {
// do something ...
} else {
return false;
}
}

Kendo UI Grid Anuglar Cell Color

I have an Angular 2 App using the Kendo UI Grid. There I have a Grid showing some data (integer values). Is it possible, to colorize each cell according to it's type? Maybe adding css class to each cell based on the type?
right now, the data looks like this [{"a":4,"b"=35,...},{...},....] I also have types for each element but not yet saved in the data grid.
I have a suggestion it's still in form of pure js kendo (but you should be able to do it in angular 2 kendo), by using schema.parse or in angular 2 : after getting data from backend you could add additionals field in the after retrieving your data from rest endpoint. add your logic inside the looping in my case i just assign color at random
schema: {
parse : function(response){
var colors = [
'red',
'green',
'blue',
'yellow'
];
//loop through all you data, add adding aditional field.
//also here i randomize the color for each cell
for(var i = 0; i< response.d.results.length; i++){
response.d.results[i].cell1 = colors[ Math.floor(Math.random()*colors.length)];
response.d.results[i].cell2 = colors[ Math.floor(Math.random()*colors.length)];
response.d.results[i].cell3 = colors[ Math.floor(Math.random()*colors.length)];
response.d.results[i].cell4 = colors[ Math.floor(Math.random()*colors.length)];
}
return response
}
}
Then on the row template you could use it as class like this (look at the cell1,cell2,cell3,cell4 attribute) in kendo-angular2 reference detail row template :
<script id="rowTemplate" type="text/x-kendo-tmpl">
<tr data-uid="#: uid #">
<td class="photo #=data.cell1#">
<img src="../content/web/Employees/#:data.EmployeeID#.jpg" alt="#: data.EmployeeID #" />
</td>
<td class="details #=data.cell2#">
<span class="name">#: FirstName# #: LastName# </span>
<span class="title">Title: #: Title #</span>
</td>
<td class="country #=data.cell3#">
#: Country #
</td>
<td class="employeeID #=data.cell4#">
#: EmployeeID #
</td>
</tr>
</script>
then add the css
<style>
.red {
background-color: red;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
.yellow {
background-color: yellow;
}
</style>
Working example in dojo

Aplying style to a list item dynamically in view

I have got a list and I want to apply a different style to each item depending on it's Id. Below is my attempt to do that but unfortunately something is wrong. Any help will be very appreciated.
#foreach (var item in Model)
{
#if (#item.Id==0)
{
var css = "background-color:Aqua";
}
else if (#item.Id==1)
{
var css = "background-color:Red";
}
else
{
var css = "background-color:Green";
}
<div style="#css" class="box">
#item.Info
</div>
<div>
#item.Name
</div>
}
Your if condition is already in a code block(foreach code..). So no need of # . Also define the css varibale outside of your if-else blocks
#foreach (var item in Model)
{
var css="background-color:Green";
#if (#item.Id==0)
{
css = "background-color:Aqua";
}
else if (#item.Id==1)
{
css = "background-color:Red";
}
<div style="#css" class="box"> #item.Info </div>
<div> #item.Name </div>
}
Another solution is creating a css class name string inside your loop using your item.Id or item.Code(if that exists) and using that in your markup. With this approach, you may completely eliminate the if condition checking in your razor code, thus making this a much cleaner solution than the previous one.
#foreach (var item in Model)
{
<div class="box myClass-#item.Id">
#item.Name
</div>
<div> #item.Name </div>
}
And in your css class add the css classes as needed
.myClass-0
{
background-color:aqua;
}
.myClass-1
{
background-color:red;
}
.myClass-2
{
background-color:green;
}
Id's might change, so i recommend using some other properties like Code/Name etc.
First of all try setting a default style and bring var css out of the if block, then set the value according to your needs.
But you can also try a helper for that:
#helper styler(int id, String value){
var bg = "background-color:white" // as default
if(id == 1){
bg = "Another color";
}
else if (id == 2) {
// continue setting values
}
<span style="#bg">#value</span>
}
However, this is not really necessary as you can set the style inside of the for loop, but this approach reduce some code duplication.

I want the ID of the element as the background of the div

I am trying to add a background image to each element on my page.
The background images name is same as the id of the page (without the.png)
for example:
<div class="ClassOne" id='1'> /* i want this to have a bg of: background-image:url('1.png')*/
</div>
<div class="ClassOne" id='2'> /* i want this to have a bg of: background-image:url('2.png')*/
</div>
<div class="ClassOne" id='3'> /* i want this to have a bg of: background-image:url('3.png')*/
</div>
Try this with jQuery.
Code Modified
var element = $(".ClassOne");
for(var i=0; i<element.length; i++)
{
var id = element.eq(i).attr("id");
element.eq(i).css({
"background": "url("+ id +".png)"
});
}
Do you have a set number of possibilities? If you do you could use CSS:
#1 {
background-image: url('1.png');
}
Now the element with an id of 1 will have that background image.
If you have multiple/more dynamic ids you might consider a javascript solution along the lines of: (edit: tailored to suit your comment)
function setBackgroundFromId(targetEl) {
targetEl.style.backgroundImage('url(' + targetEl.id + '.png');
}
var imageDivs = document.querySelectorAll('ClassOne');
for (var i = 0; i < imageDivs.length; i++) {
setBackgroundFromId(imageDivs[i]);
}
Can't you just add the attribute:
style="background-image:url('x.png')"
To each element in the same way you generated the id attributes?

MooTools 1.1, how to get id of class and apply a style

I need to get the id attribute of a class and apply a style based on that id.
So for instance, 3 list items each with the class "typo", one id is "application", another id is "application_osx", and the final id is "application_osx_terminal"
The class "typo" is handled by CSS, but I would need to assign a background image based on the ID name.
So if the id happens to be "application_osx" or "someotherid", it would automatically have this CSS applied to it
#application_osx { background: url(/path/to/image/application_osx.png) }
#someotherid { background: url(/path/to/image/someotherid.png) }
I'm trying to use MooTools 1.1 for this.
I guess it would look like this barebones
<html>
<head>
<title>Blah</title>
<script src="path/to/mootools.js"></script>
<script>
A SCRIPT THAT PRINTS OUT:
#application_osx { background: url(/path/to/image/application_osx.png) }
#someotherid { background: url(/path/to/image/someotherid.png) }
BASED ON THE CLASS "TYPO"
</script>
</head>
<body>
<ul>
<li id="application_osx" class="typo">Application OSX</li>
<li id="someotherid" class="typo">Someotherid</li>
</ul>
</body>
</html>
$$('.typo').each(function(el){
el.setStyle('background-image', 'url(/path/to/'+el.id+'.png)')
});
Should do the trick, if I understand well…
why can't you define the rules in your css file? if you need to dynamically produce rules and append to the document head then you need something like this
for mootools 1.2.x
http://www.jsfiddle.net/dimitar/G5ywF/1/
var StyleWriter = new Class({
// css classes on the fly, based on by Aaaron Newton's version
createStyle: function(css, id) {
try {
if (document.id(id) && id) return;
var style = new Element('style', {id: id||'',type:'text/css'}).inject(document.getElement('head'));
if (Browser.Engine.trident)
style.styleSheet.cssText = css;
else
style.set('text', css);
} catch(e) {
console.log("failed:", e);
}
}
});
use:
new StyleWriter().createStyle("#rule { blah; }\n#rule2 { blah... }\n", "mystyles");
edit it was just brought to my attention you are on mootools 1.11 so
http://www.jsfiddle.net/G5ywF/4/
class version for 1.11 with slight changes:
var StyleWriter = new Class({
// css classes on the fly, based on by Aaaron Newton's version
createStyle: function(css, id) {
try {
if ($(id) && id) return;
var style = new Element('style', {id: id||'',type:'text/css'}).inject(document.getElement('head'));
if (window.ie)
style.styleSheet.cssText = css;
else
style.setHTML(css);
} catch(e) {
console.log("failed:", e.message);
}
}
});
tested the 1.11 class in FF 3.6x, Chromium 5 and IE7

Resources