Classes In Javascript Using Prototype Library
Feb 9, 2006
I've been using the prototype.js library[1] to create classes. Something like:
var Sortable = Class.create();
Sortable.prototype = {
initialize: function(element) {
//...
},
// more methods...
}
All of my methods are instance methods. What is the tidiest way to
create class methods and variables?
View 6 Replies
Oct 29, 2010
I'm working on a unit test for a javascript library that has several handlers. I wish to fire those handlers in a "natural" way, by raising an event that gets caught. I am having trouble creating an event that gets caught in Firefox.I do development in Firefox to take advantage of Firebug, but the software is for a closed intranet and IE 7 is the required browser (we support IE 8 insofar as we force it to IE 8 mode). This policy predate my hire, I'm trying to get the software to a browser agnostic state - first I have to replace an ActiveX object known as MeadCo Script X - but that's a whole other issue.Anyway I need to raise events, specifically onchange events in Firefox. I have this so far...
Code javascript:
function fire(el, ev) {
el = $(el);
if (typeof(el.fireEvent) != 'undefined') { // IE
[code]...
Prototype library is in use for this project, though I haven't used it for this particular corner case.
View 1 Replies
View Related
Mar 25, 2006
I've been asking questions about library design over the last week and
would like to get feedback on my overall idea for a JavaScript GUI
library. I need a nice GUI library so there is a good chance I will
write this as I need new widgets. I haven't found anything like this
and I'm surprised/disapointed this doesn't already exist. My library
prototype works nicely. I think parts of these ideas are not commonly
used for JavaScript library development and before I dive in too deeply
any experienced advice might make my efforts a lot better.
While trying to develop a rich GUI for a web app backend I started
needing a large set of GUI widgets that behaved and were coded
similarly. Things like flyout menus, drop down menubars, tree menus,
adjustable table widths, spinners...all the things that are in Java
Swing or other GUI widget libraries. Yes I know all of these things
exist individually as JavaScripts but I would like to build a library
that uses a class hierarchy structure and inheritance to take advantage
of all the nice OOP ideas of encapsulation etc. Certainly this library
will be different than a desktop app GUI library since HTML, CSS and
the browser already provide a lot of what is needed (<ul>'s for menus,
<table> for tables, all sorts of font display stuff, some form
elements). In JavaScript we are adding functionality on top of what the
browser already gives us.
This could end up being a big library but if each class is in it's own
file and the class hierarchy is known then people could string together
only the pieces they need. I would like a develop a way to automate
this step. Something like compiling one JavaScript file with all you
need for your page from the big library.....
View 21 Replies
View Related
Nov 25, 2011
According to ECMAScript, the root of the prototype chain is Object.Prototype. Each object has an internal property [[Prototype]] that could be another object or NULL.... However, it also says that every function has the Function prototype object: Function.Prototype, it confused me, because a function is an object, for a function object, what is its function prototype and object prototype..For example:
var x = function (n) {return n+1;};
what is the relationships of x, Object.Prototype and Function.Prototype
View 5 Replies
View Related
Dec 14, 2009
I am trying to get to the bottom of javascript object, prototypes etc. I have a fairly good grasp of it, but I get confused the closer I get to the base object.prototype. FIrst of all, I was under the impression that all objects descend directly from Object. But some objects (like Array) seem to inherit properties and methods from the function.prototype. So does this mean that the chain is like this:
object -- function -- array Second, I noticed (on the mozilla javascript reference site that object.prototype inherits properties and methods from function.prototype and vice versa!? How can this be? I must be missing something important about understanding the chain?
View 24 Replies
View Related
Jun 28, 2006
I've generally built a JavaScript file for each "class" I want to build. So, each class would look something like:
Person.js
Code:
function Person()
{
var self = this;
// variables
this.XmlRequestPerson;
// methods
this.loginPerson = loginPerson;
function loginPerson(userName, password)
{
// do some stuff!
}
}
Having a class like this gives me the opportunity to do something like:
Code:
var person = new Person();
person.loginPerson("sup", "dude");
Are there better ways to do this? It just seems like the "var self = this" thing is stupid, because inside the Person.js file I have to reference everything local by "self" instead of "this".
View 2 Replies
View Related
Jan 11, 2007
I know nothing about javascript at the moment, but I've been hitting php very aggressively the past few months.
I've came across a challenge and I'm not sure how to go about fixing it.
Basically, I need to be able to drag an image into a box on my webpage, crop it, and then upload it. I've done basic image uploading and resizing with php. That's no big deal.
The problem is I may want to crop in different spots. The idea is to have some sort of box tool that is set at a given aspect ratio (like Photoshop does). Then, I could select the part of the image I wanted to keep, hit one button that would then crop, resize, and upload the image (storing the pathway to the MySQL database).
So does anyone know of any either standalone GUI's that work well for this sort of image manipulation or possibly a javascript that will allow me to select part of the image and spit out some co-ordinates that I could use with php's GD Library.
View 3 Replies
View Related
Jun 7, 2011
I started studying javascript when I decided that I wanted to create a dynamic webpage after viewing some books that were made to pageflip with Javascript and after viewing a ton of animations. So I figured since everyone keeps saying Javascript is simple that if all I wanted to do was to make a book extend outwards on mouseover and then open up on click and flip pages which they already have a code for that it wouldn't be so difficult. I've read everything I could practiced a little bit, I'm not fluent but I do have a goal and a time. The problem for me comes up with figuring out what I need to do in order to make it appear as if the book is coming out of a book shelf. There is a lot of information on animations where flat objects rise above clouds.
I am thinking that I would have to use some sort of sprite of a row of books both sorta sideways where you can see the side of the book as well as the edge, and both a row of books on a shelf, or perhaps a row of books with some of the books being pulled out and then on mouse over animate the scene so that it looks like the book is being pulled out. Would that work? Or is there a simpler way? Another way or I don't know. Is there a code that can make it look like a book is extending outwards without an animation or would I need to create an animation for that?
View 3 Replies
View Related
Jan 11, 2006
After a good deal of internal development, I've decided this library will do more good in the wild. It works in IE6, Safari 2, Opera 9, Firefox 1.5, and IE5.5/Mac (sort of buggy behavior with text selection in IE5.5/Mac though). It should also work in virtually any version of Gecko, Opera 6+, Safari 1, and IE5+ (anybody who wants to test those, feel free). The scripting is highly efficient, and only exposes two functions globally, Draggable and Droppable. The below code is licensed under the LGPL, though it is available under other licenses per request.
Let me know if you have any issues with it - this is something I'm interested in maintaining. Also, compared to other scripts, such as script.tac.ulo.us, this is much less code (lots of whitespace/comments could be taken out), and more widely supported across browsers.
Other than below, it can be found here: http://www.jasonkarldavis.com/scripts/dragdropable/dragdropable.html
// dragdropable.js (1.0.1)
//Copyright (C) 2006 Jason Davis, www.jasonkarldavis.com
//This code is licensed under the LGPL:
//http://www.gnu.org/licenses/lgpl.html
//
// Documentation:
//This script exposes two global variables, Draggable and Droppable. To make an
//element draggable, simply call `Draggable(element)`. This sets the necessary
//CSS properties and event handlers. It also overrides the addEventListener and
//and attachEvent methods (if present) to include the following events:
//onstart(When the element begins dragging)
//ondrag(When the element is being dragged)
//onstop(When the element has stopped dragging)
//You may also assign element.onstart, element.ondrag, and element.onstart to
//add event listeners.
//Lastly, if you need to physically prevent an element from dragging, you may
//call event.preventDefault() from within the onstart handler.
//
//To make an element droppable (have things dragged onto it), call
//`Droppable(element)`. This sets the necessary event handlers and coordinate
//searches. Once again, it overrides addEventListener and attachEvent to
//include the following events:
//onhover(When another element has been dragged over the element)
//ondrop(When another element has been dropped onto the element)
//onunhover(When another element has been dragged off the element)
//As before, you may also set element.onhover, element.ondrop, and
//element.onunhover directly.
//To remove all drop points from the current session, call `Droppable.reset()`.
//
//The event object is also extended with the following properties:
//dragTarget(The element being dragged)
//dropTarget(The element being dropped onto)
//
// Known bugs:
//* removeEventListener and detachEvent do not work with the custom events.
//Workaround: Use direct assignment (element.onevent) instead.
//* There is no way to remove a single drop point.
//Workaround: Call Droppable.reset(), then reinitialize the other drop points.
//* There is no way to remove dragging from an element.
//Workaround: element.onstart = function(event) { event.preventDefault() }
//* Changing position of droppable elements after initialization breaks drop
//Workaround: Static droppables are much faster than moving ones. Either call
//Droppable.reset() and reinitialize often, or ask the author to modify the
//script to account for this (and accept the resulting slowdown)
function Draggable(element) {
// initialize drag variable
var x = 0, y = 0, oldMouseMove, oldMouseUp;
// get style properties of element
var computedStyle;
if (typeof document.defaultView != "undefined" && typeof document.defaultView.getComputedStyle != "undefined")
computedStyle = document.defaultView.getComputedStyle(element, "");
else if (typeof element.currentStyle != "undefined")
computedStyle = element.currentStyle;
else
computedStyle = element.style;
// prep element for dragging
if (computedStyle.position == "static" || computedStyle.position == "")
element.style.position = "relative";
if (computedStyle.zIndex == "auto" || computedStyle.zIndex == "")
element.style.zIndex = 1;
element.style.left = isNaN(parseInt(computedStyle.left)) ? "0" : computedStyle.left;
element.style.top = isNaN(parseInt(computedStyle.top )) ? "0" : computedStyle.top;
// default event listeners
function onstart(event) {
event.dragTarget = element;
x = event.clientX;
y = event.clientY;
// Override preventDefault
event.preventDefault = (function(original) {
return function() {
element.ownerDocument.onmousemove = oldMouseMove;
element.ownerDocument.onmouseup = oldMouseUp;
original.call(event);
}
})(event.preventDefault || function() {});
if (element.onstart) element.onstart(event);
}
function ondrag(event) {
event.dragTarget = element;
element.style.left = parseInt(element.style.left) + event.clientX - x + "px";
element.style.top = parseInt(element.style.top) + event.clientY - y + "px";
x = event.clientX;
y = event.clientY;
Droppable.query(event);
if (element.ondrag) element.ondrag(event);
}
function onstop(event) {
event.dragTarget = element;
Droppable.query(event);
if (element.onstop) element.onstop(event);
}
// make listeners active
element.onmousedown = (function(oldMouseDown) {
return function() {
// Call old listener
if (oldMouseDown) oldMouseDown.apply(this, arguments);
// Store old event handlers
oldMouseMove = this.ownerDocument.onmousemove;
oldMouseUp = this.ownerDocument.onmouseup;
// Setup events
this.ownerDocument.onmousemove = function() {
// Call old listener
if (oldMouseMove) oldMouseMove.apply(this, arguments);
// Call ondrag
ondrag.call(element, arguments[0] || event);
return false;
}
this.ownerDocument.onmouseup = function() {
// Call old listener
if (oldMouseUp) oldMouseUp.apply(this, arguments);
// Call onstop
onstop.call(element, arguments[0] || event);
// Restore old event listeners
this.onmousemove = oldMouseMove;
this.onmouseup = oldMouseUp;
return false;
}
// Call onstart
onstart.call(this, arguments[0] || event);
return false;
}
})(element.onmousedown);
// override event attachers
if (element.addEventListener)
element.addEventListener = (function(original) {
return function(event, listener, useCapture) {
switch (event) {
case "start":
onstart = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(onstart);
break;
case "drag":
ondrag = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(ondrag);
break;
case "stop":
onstop = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(onstop);
break;
default:
original.call(element, event, listener, useCapture);
}
}
})(element.addEventListener);
if (element.attachEvent)
element.attachEvent = (function(original) {
return function(event, listener) {
switch (event) {
case "onstart":
onstart = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(onstart);
break;
case "ondrag":
ondrag = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(ondrag);
break;
case "onstop":
onstop = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(onstop);
break;
default:
original.call(element, event, listener);
}
}
})(element.attachEvent);
if (window.attachEvent)
window.attachEvent("onbeforeunload", function() {
onstart = ondrag = onstop = element.onmousedown = element.addEventListener = element.attachEvent = null;
element = null;
});
}
// initialize Droppable in the global scope
var Droppable;
(function() {
if (document.getBoxObjectFor) {
function getOffset(element) {
var box = document.getBoxObjectFor(element);
return { x: box.x, y: box.y };
}
}
else if (document.all && navigator.appName == "Microsoft Internet Explorer" && !window.opera) {
function getOffset(element) {
var range = document.body.createTextRange();
range.moveToElementText(element);
var rect = range.getBoundingClientRect();
return { x: rect.left, y: rect.top };
}
}
else {
function getOffset(element) {
var accumulator = arguments[1] || { x: 0, y: 0 };
if (element && element != document.body) {
accumulator.x += element.offsetLeft;
accumulator.y += element.offsetTop;
return getOffset(element.offsetParent, accumulator);
}
else {
return accumulator;
}
}
}
// initialize private pointers to current target information
var cTarget = null, cHover = null, cUnhover = null, cDrop = null;
function hotSpots(x,y) {
cTarget = cHover = cUnhover = cDrop = null;
}
// declare Droppable within the private scope
Droppable = function(element) {
// Calculate offset
var offset = getOffset(element);
// Calculate other edge offset
var edge = { x: offset.x + element.offsetWidth, y: offset.y + element.offsetHeight };
// Assign a finder function
hotSpots = (function(old) {
return function(x,y) {
if (offset.x <= x && x <= edge.x && offset.y <= y && y <= edge.y) {
cTarget = element;
cHover = onhover;
cUnhover = onunhover;
cDrop = ondrop;
}
else {
old(x,y);
}
}
})(hotSpots);
// default event listeners
function onhover(event) {
event.dropTarget = element;
if (element.onhover) element.onhover(event);
}
function onunhover(event) {
event.dropTarget = element;
if (element.onunhover) element.onunhover(event);
}
function ondrop(event) {
event.dropTarget = element;
if (element.ondrop) element.ondrop(event);
}
// override event attachers
if (element.addEventListener)
element.addEventListener = (function(original) {
return function(event, listener, useCapture) {
switch (event) {
case "hover":
onhover = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(onhover);
break;
case "unhover":
onunhover = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(onunhover);
break;
case "drop":
ondrop = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(ondrop);
break;
default:
original.call(element, event, listener, useCapture);
}
}
})(element.addEventListener);
if (element.attachEvent)
element.attachEvent = (function(original) {
return function(event, listener) {
switch (event) {
case "onhover":
onhover = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(onhover);
break;
case "onunhover":
onunhover = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(onunhover);
break;
case "ondrop":
ondrop = (function(old) {
return function(event) { old.call(element,event); listener.call(element,event); }
})(ondrop);
break;
default:
original.call(element, event, listener);
}
}
})(element.attachEvent);
if (window.attachEvent)
window.attachEvent("onbeforeunload", function() {
hotSpots = onhover = onunhover = ondrop = element.addEventListener = element.attachEvent = cTarget = cHover = cUnhover = cDrop = null;
element = null;
});
}
// Setup a query method
Droppable.query = function(event) {
var oTarget = cTarget,
oHover = cHover,
oUnhover = cUnhover,
oDrop = cDrop;
var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft,
scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
hotSpots(event.clientX + scrollLeft, event.clientY + scrollTop);
switch (event.type) {
case "mousemove": // onhover & onunhover
if (oTarget != null && oTarget != cTarget)
oUnhover.call(oTarget, event);
if (oTarget == null && cTarget != null)
cHover.call(cTarget, event);
break;
case "mouseup": // ondrop
if (cTarget != null) {
cUnhover.call(cTarget, event);
cDrop.call(cTarget, event);
}
break;
}
}
Droppable.reset = function() {
hotSpots = function(x,y) {
cTarget = cHover = cUnhover = cDrop = null;
}
hotSpots();
}
})();
// Fix the function prototype for IE5/Mac
if (typeof Function.prototype.apply == "undefined")
Function.prototype.apply = function(scope, args) {
if (!args) args = [];
var index = 0, result;
do { -- index } while (typeof scope[index] != "undefined");
scope[index] = this;
switch (args.length) {
case 0:
result = scope[index]();
break;
case 1:
result = scope[index](args[0]);
break;
case 2:
result = scope[index](args[0], args[1]);
break;
case 3:
result = scope[index](args[0], args[1], args[2]);
break;
case 4:
result = scope[index](args[0], args[1], args[2], args[3]);
break;
default:
result = scope[index](args[0], args[1], args[2], args[3], args[4]);
break;
}
delete scope[index];
return result;
}
if (typeof Function.prototype.call == "undefined")
Function.prototype.call = function(scope) {
var args = new Array(Math.max(arguments.length-1, 0));
for (var i = 1; i < arguments.length; i++)
args[i-1] = arguments[i];
return this.apply(scope, args);
}
View 7 Replies
View Related