🔰 Seule is a light-weight (20KB), blazing fast and feature-rich Javascript Framework. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers 🧙♂.
You can create an index.html file and include Seule with:
<script src="https://cdn.jsdelivr.net/gh/ElMehdiLebbar/SeuleJs/seule.0.0.1.dev.js"></script>
🔹 At the core of Seule.js is a system that enables us to declaratively render data to the DOM using straightforward template syntax:
<div id='app'>
{{message}}
</div>
const app = new Seule({
el: '#app',
data: {
message : 'hello Seule'
}
})
We have already created our very first Seule app! Click on the "Try it Yourself" 👆 button to see how it works.
Seule uses Shadow DOM, It allows us to ship self contained components along with their style and isolate the component from global style while "protecting" the host application from styles defined inside the component.
By Adding Style property in the main Object, there are two ways of inserting a style sheet to Seule Component:
- External
- Internal
style property can have like a value the Url of the Style sheet.
Each Seule Application must include a reference to the external style sheet file.
<div id='app'></div>
const app = new Seule({
el: '#app',
style: './css/mystyle.css',
data:{
message: "Hello It's App number 1"
}
})
🔹 By giving the style property the value of link about Attribute like a value we telling the program, the style sheet for the Application is the link element that has the about attribute with value equal the id of Seule application , inside the head section of an HTML page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css/main.css">
<link rel="stylesheet" href="./css/app.css" about="app-style">
</head>
<body>
<div id='app'>
{{message}}
</div>
</body>
</html>
const app = new Seule({
el: '#app',
style: 'app-style' //Style Sheet ./css/app.css applying for the app in this case,
data:{
message: "Hello World"
}
})
In addition to text interpolation, we can also bind element attributes like this:
<element attribute="{{[variable: <String>]}}"></element>
<div id='app'>
<p title="{{message}}"> Hover your mouse over me <br> for a few seconds </p>
</div>
const app = new Seule({
el: '#app',
data: {
message : 'You visited this page on ' + new Date().toLocaleString()
}
})
Magic Happens here by invoking Methods inside the handler(), using three parameters: $app, $scoop, $root and let users interact with your app
<div id='app'>
<p>{{message}}</p>
<button>click-me</button>
</div>
const app = new Seule({
el: '#app',
data: {
message: "Hello my Friend"
},
handler($app, $scoop, $root){
$scoop('button')
.Click(()=> $app.data.message =
$app.data.message.split('').reverse().join(''), true)
}
})
More "Try it Yourself" examples below.
For more security, Seule uses Shadow DOM. The problem in that, is you can't get access to the DOM element(s) directly unless you use Css selectors inside the handler() by invoking the $scoop parameter.
$scoop Selectors method returns all elements in the Seule app that matches a specified CSS selector(s), as a static Seule Object.
$scoop([CSS selectors])
Parameter --> CSS selectors
Type --> String
Tip: For a list of all CSS Selectors, look at w3schools !CSS Selectors Reference
🔹 You can select all <p> elements on app like this:
$scoop("p")
When a user clicks on a button, all <p> elements will be hidden:
<div id='app'>
<p>{{message}} 1</p>
<p>{{message}} 2</p>
<p>{{message}} 3</p>
<button title="{{title}}">Hide All!</button>
</div>
const app = new Seule({
el: '#app',
data: {
message: "This is paragraph",
title: "Click me to hide paragraphs"
},
handler($app, $scoop){
$scoop('button')
.Click(()=> $scoop('p').Hide())
}
})
What is an Event?
An event represents the precise moment when something happens. Examples:
- moving a mouse over an element
- selecting a radio button
- clicking on an element
for mor details about !Event reference
using the On Method to assign an event to an element.
$scoop([CSS selectors]).On([event:<String>], [handler:<Function>])
🔹 for exemple To assign a click event to all button on a Seule app, you can do this:
<div id='app'>
<img src="https://bit.ly/3mA0FbG" width="150">
<button>Change the picture</button>
<p>{{message}}</p>
</div>
const app = new Seule({
el: '#app',
data: {
message: "Click the button to see what happens!"
},
handler($app, $scoop){
$scoop('button')
.On("click", ()=> $scoop('img')
.Attr('src')
.set('http://bit.ly/3fjVxGC'))
}
})
The function is executed when the user clicks on the HTML element.
🔹 For example When a click event fires on an element show an alert box.
$scoop("button")
.Click(()=> alert('just a simple click'))
The function is executed when the user make a long presse on the HTML element.
$scoop([CSS selectors]).Hold([handler:<Function>], [time:<Integer>])
the time by default is 1500 => 1.5s
🔹 Show an alert box When user make a long presse on button for 3s.
<div id='app'>
<button>Hold Me for 3s</button>
</div>
const app = new Seule({
el: '#app',
handler($app, $scoop){
$scoop('button')
.Hold(()=> alert('Good Job!™ 🤩'), 3000)
}
})
Detecting a swipe (left, right, top or down) When a swipe event fires on an element.
$scoop([CSS selectors]).Swipe([event:<String>], [handler:<Function>])
- left
- right
- top
- bottom
🔹 Change the background When user swipe left 🤚 on screen
<div id='app'>
<img src="https://bit.ly/3m7nlzN" width="150" alt="Logo" title="{{tooltip}}">
<p>{{message}}</p>
</div>
const app = new Seule({
el: '#app',
data: {
message: "Swipe left to see what happens!",
tooltip : "See this demo on Mobile"
},
handler($app, $scoop){
$scoop(window)
.Swipe("left", ()=> $scoop('body')
.Css('background')
.set('#666'))
}
})
The focus event occurs when an element gets focus (when selected by a mouse click or by "tab-navigating" to it).
The focus() method triggers the focus event, or attaches a function to run when a focus event occurs.
Trigger the focus event for selected elements:
$scoop([CSS selectors]).Focus()
Attach a function to the focus event:
$scoop([CSS selectors]).Focus([handler:<Function>])
🔹 Attach a function to the focus event. The focus event occurs when the field gets focus:
<div id='app'>
<input placeholder="Focus on me!" type="text">
<p>{{message}}</p>
</div>
const app = new Seule({
el: '#app',
data: {
message: "Click in the input field to see what happens!"
},
handler($app, $scoop){
$scoop('input')
.Focus(e => {
e
.Css("border-bottom-color").set("#8755F2")
.Val("Great work keep it up!").set()
})
}
})
The blur event occurs when an element loses focus.
The blur() method triggers the blur event, or attaches a function to run when a blur event occurs.
Trigger the blur event for the selected elements:
$scoop([CSS selectors]).Blur()
Attach a function to the focus event:
$scoop([CSS selectors]).Blur([handler:<Function>])
🔹 Attach a function to the blur event. The blur event occurs when the field loses focus:
<div id='app'>
<input placeholder="Write something!" type="text">
<p>{{message}}</p>
</div>
const app = new Seule({
el: '#app',
data: {
message: "Click outside the field to lose focus (blur)."
},
handler($app, $scoop){
$scoop('input')
.Blur(()=> $scoop('p')
.Text("This input field has lost its focus!")
.set())
}
})
With HotKey Method in Seule you can now handling keyboard shortcuts easly.
$scoop([CSS selectors]).Hotkey([keys:<String>], [handler:<Function>], [prevent:<Boolean>]);
For modifier keys you can use shift
, ctrl
, alt
or meta
You can substitute option
for alt
and command
for meta
Other special keys are backspace
, tab
, enter
, return
, capslock
, esc
, escape
, space
, pageup
, pagedown
, end
, home
, left
, up
, right
, down
, ins
, del
, and plus
Any other key you should be able to reference by name like a
, /
, $
, *
, or =
.
🔹 Hotkey event with Single key:
$scoop('input').HotKey('m',
()=> alert('M button is pressed on the Keyboard!'));
🔹 Combination of keys:
$scoop('input').HotKey('ctrl s',
()=> alert('You pressed ctrl+s!'));
🔹 Sequence of keys like Konami Style (:
$scoop('input').HotKey('Left Right Left Right A C',
()=> alert('Now you can play with Orochi Iori'));
🔹 Or you can specify keyboard events that will work anywhere including inside textarea/input fields like:
$scoop(window).HotKey('ctrl+r',
()=> alert('Global keyboard shortcuts'));
// if you want prevent the default refresh event under WINDOWS system
$scoop(window).HotKey('ctrl+r',
()=> alert('Global keyboard shortcuts'), true);
There are so many things helpful in Seule like :
Do you want to simulate event with a single statement? Then you may use Fire Method for that:
$scoop([CSS selectors]).Fire([event:<String>]);
Simulate a mouse-click when moving the mouse pointer over a button:
<div id='app'>
<button onclick="alert('Clicked !')">Hover Me !</button>
</div>
const app = new Seule({
el: '#app',
handler($app, $scoop){
$scoop('button')
.On('mouseover', e=> e.Fire("click"))
}
});
The Copy() method triggers the specified event(s) and the default behavior of an event (like form submission) for the selected elements.
$scoop([CSS selectors]).Copy([target:<String> | <Object>], [events:<String>]);
Simulate onmouseout and onmouseover events the mouse pointer (out of/onto) a button:
<div id="app">
<p>{{initMessage}}</p>
<button id="original">Original</button>
<button id="copy">Copy</button>
</div>
const app = new Seule({
el: '#app',
data:{
message : "Woah! That's awesome",
initMessage : "moving the mouse pointer (out of/onto) a button to see what happens!"
},
handler($app, $scoop){
$scoop('#original')
.On("mouseover", e=> {
e.Css("background").set("rgba(0,0,0,1)");
$scoop("p").Text(this.data.message).set()
})
.On("mouseout", e=> {
e.Css("background").set("rgba(0,0,0,0.1)");
$scoop("p").Text(this.data.initMessage).set()
});
$scoop('#copy').Copy("#original", "mouseover : mouseout")
}
});
🔹 See the Example by clicking on the button below 👇
The Toggle() method toggles between two custom functions for the selected element(s).
$scoop([CSS selectors]).Toggle([event:<String>], [methods:<Object>]);
🔹 Toggle between adding border-radius and changing the background of a div:
<div id="app">
<p>Click on the square 👇🏻 to see what happens!</p>
<section class="square">{{grimace}}</section>
</div>
const app = new Seule({
el: '#app',
data:{
grimace : "^_^",
},
handler($app, $scoop){
$scoop(".square").Toggle("click",
{
handler(e) {
e
.Css({
"border-radius" : "50%",
background : "red",
duration: 300
}).set()
.Text('*_*').set()
},
callback(e){
e
.Css({
"border-radius" : 0,
background : "#2f2f2f",
duration: 300
}).set()
.Text($app.data.grimace).set()
}
})
}
});
Seule provides a variety of methods that allow us to traverse the App. Traversal methods in Seule help us to select elements in a Seule app randomly as well as in sequential method !
- Select () : Returns all child elements that matches a specified CSS selector(s)
- Each () : Specifies a function to run for each matched element.
- Element () : Returns an element with a specific index number of the selected elements.
- Parent () : Returns the direct parent element of the selected element.
- Child () >>>
- .first() : Returns the first child element of the selected element.
- .last() : Returns the last child element of the selected element.
- .number() : Returns a child element with a specific index number of the selected elements.
- .all() : Returns all direct children of the selected element.
- $() : Returns the root element of the selected element.
🔹 for exemple, we have a group of paragraphs inside a <div> and we want to change the background color of the second descendant <p>:
<section id="app">
<div class="group">
<p>My name is ElML</p>
<p class="intro">I live in EL Jadida.</p>
<p class="intro">I love EL Jadida.</p>
<p>Dad is my best friend...</p>
</div>
</section>
const app = new Seule({
el: '#app',
handler($app, $scope){
$scope('.group')
.Select('.intro') // returns all descendants <p> with class name equal "intro"
.Element(1) // returns the second element of the selected element.
.Css('background').set('#424cf7')
}
});
The Each() method specifies a function to run for each matched element.
$scoop([CSS selectors]).Each([handler:<Function>]);
🔹 Alert the text of each <li> element:
<div id="app">
<p>Click the button to see what happens!</p>
<ul style="text-align: left">
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>
<button title="{{title}}">Click-Me</button>
</div>
const app = new Seule({
el: '#app',
data: {
title: "Add the value of each list item"
},
handler($app, $scope){
$scope('button')
.Click(()=> $scope('li')
.Each(e => {
const item = e.Text().get() + " ";
$scope('.result')
.Text(item).append()
}))
}
});
Seule provides various methods to add, edit or delete element(s) in the Seule App.
Create() method creates the HTML element specified by tagName.
$scoop().Create([tagName:<String>]);
🔹 Create a <button> element:
<div id="app"></div>
const app = new Seule({
el: '#app',
handler($app, $scope){
const btn = $scope().Create('button')
}
});
🔹 Create a <button> with text:
<div id="app"></div>
const app = new Seule({
el: '#app',
handler($app, $scope){
const btn = $scope().Create('button');
btn.Text($app.data.text).set();
}
});
The Append() method appends a Seule Element as the last child of a Element.
$scoop([CSS selectors]).Append([element:<String> | <Object>]);
🔹 Create and append a <button> element in The Root Element:
<div id="app"></div>
const app = new Seule({
el: '#app',
handler($app, $scope){
const btn = $scope().Create('button');
btn.Text($app.data.text).set();
$scope().Append(btn)
}
});
- .before() : method inserts or moves a element as a child, right before an existing child, which you specify.
- .after() : method inserts or moves a element as a child, right after an existing child, which you specify.
$scoop([CSS selectors]).Move().method([element:<String> | <Object>]);
🔹 Insert a new <li> element before the Second child element of an <ul> element:
<div id="app">
<button title="Add Water Please!">Click-Me</button>
<ul>
<li>Tea</li>
<li>Coffee</li>
</ul>
</div>
const app = new Seule({
el: '#app',
data:{text: "Water"},
handler($app, $scope){
const
btn = $scope().Create('li'),
Coffee = $scope('li').Element(1);
btn.Text($app.data.text).set();
$scope('button').Click(()=> btn.Move().before(Coffee))
}
});